Set default values to fields when creating new and follow-up work orders
About this task
Set default values for fields on new work orders and follow-up work orders. This exercise guides you through defining priority = 1 and work type = CM (Correction Maintenance) when creating new and follow-up work orders.
Procedure
Step 1: Create constants that indicate the default values for priority and work type. Avoid inserting static values directly into the code to facilitate changes, readability, and tracking.
const DEFAULT_WORKTYPE = 'CM';const DEFAULT_WORKPRIORITY = 1;
Step 2: Create conditions inside the page summary handler method (pageResumed) to set the default values.
pageResumed(page, app) {// Check creation of Follow-up Workif (app.currentPage.name === 'woedit' && page.name=== 'woedit' && page?.params?.followup) {if (page?.params?.workorder) {// Replace workorder attribute from URL paramspage.params.workorder.worktype = DEFAULT_WORKTYPE;page.params.workorder.wopriority = DEFAULT_WORKPRIORITY;}}
Step 3: Set default value to its attribute in datasource schema.
<attribute name="wopriority" id="ye34g" default-value="1"/>[...]<attribute name="worktype" id="y6nm_" default-value="CM"/>
Setting default value when creating a new record of a datasource can be achieved declaring default-value property on the datasource schema attribute. Alternatively, it can be set programatically for a particular situation using onAddNewRecord.
Step 3(alt): Include the new record creation event handler to datasource. Include conditions to identify the correct datasource - in this case “dsCreateWo”. To create new records, use properties from the DS schema, this avoids conflicts with other code that is defining the same property.
/*** Add new record handler* @param {Object} object* @param {Datasource} obj.datasource*/onAddNewRecord({datasource, item}) {// Set default values for schema dsCreateWo when creating Work Orderif (datasource?.name === 'dsCreateWo' && datasource?.schema) {datasource.schema.properties.worktype.defaultValue = DEFAULT_WORKTYPE;