Creating Work Orders for Configured Items By Scripting
If your sales orders contain a high number of configured items to be converted to work orders, switch from the scheduled script to the CPQM-MR-WOC map/reduce script. Do not use the scheduled script in new implementations.
With NetSuite CPQ Manufacturing, you can create work orders for configured items by scripting. In this case, the CPQM-MR-WOC map/reduce script (customscript_cpqm_mr_woc) handles the work order creation process. For more information about map/reduce scripts, see SuiteScript 2.x Map/Reduce Script Type.
To create work orders for sales orders by scripting:
-
In SuiteScript 2.x, create one of the following server-side script types:
-
Suitelet
-
RESTlet
-
User-event
-
Scheduled
Note:Because map/reduce scripts were introduced with SuiteScript 2.0, the triggering script must be written in SuiteScript 2.x.
-
-
Launch the CPQM-MR-WOC script from the created server-side script. As an example, see Launching the Work Order Creation Process from a User-Event Script.
-
Pass the internal ID of the sales order to the
custscript_cpqm_tranid
parameter. -
To convert up-to-date work orders, use the
custscript_cpqm_force
parameter and set it totrue
. For more information, see Converting Up-To-Date Work Orders.
For more information about the CPQM-MR-WOC map/reduce script, see Work Order Creation Script.
Launching the Work Order Creation Process from a User-Event Script
The most common use case scenario is launching the work order creation process from a user-event script. The event is saving the sales order. When working with user-event scripts, launch the work order creation on the afterSubmit
event. Work orders can only be created after the sales order has been saved.
To create the user-event script:
-
Pass the internal ID of the sales order to the
custscript_cpqm_tranid
parameter. -
The submit event type can be
create
,edit
or both. If you want to:-
Create work orders only for newly created sales orders, run the script for the
create
event only. -
Create work orders after editing and resaving a sales order, run the script for
create
andedit
events.
-
-
Define a custom condition to determine whether work orders are created. To do this, use a custom function, which may rely on sales order fields, such as status, customer, and so on.
Example
The following piece of code shows a user-event script that launches the CPQM-MR-WOC script to create work orders for configured items. The script will run on the following conditions:
-
When creating and editing sales orders (
create
andedit
events). -
The Memo field of the sales order has auto-create-wo as a value.
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define(['N/task', 'N/runtime'], function(_nTask, _nRuntime) {
function afterSubmit(context) {
const type = context.type,
record = context.newRecord,
recId = record?.id;
log.debug({ title: `afterSubmit (${type})`, details: recId });
if (allowedEventType(type) && meetLaunchCriteria(record)) {
try {
log.debug({ title: 'Scheduling WO creation for SO', details: recId });
const scriptTask = _nTask.create({
taskType: _nTask.TaskType.MAP_REDUCE,
scriptId: 'customscript_cpqm_mr_woc',
params: {
custscript_cpqm_tranid: recId // Pass the sales order ID
}
});
const taskId = scriptTask.submit();
log.debug({ title: 'WO creation scheduled', details: 'Task ID: ' + taskId });
} catch (e) {
log.error({ title: 'Error scheduling WO creation', details: e.message });
}
}
}
// Work orders are created for new and edited sales orders
function allowedEventType(type) {
return type == 'create' || type == 'update';
}
// Custom condition to define whether work orders are created for the sales order
function meetLaunchCriteria(record) {
return record?.getValue('memo') === 'auto-create-wo';
}
return {
afterSubmit: afterSubmit
};
});
Work Order Creation Script
After being launched by a server-side script, the CPQM-MR-WOC map/reduce script:
-
Loads the sales order.
-
Runs through all the configured line items.
-
Creates a list of lines to be processed.
This list excludes configured line items with up-to-date work orders.
-
Creates a work order for each:
-
Configured line item without a work order.
-
Configured line item with an outdated work order. Outdated work orders are deleted. A work order becomes outdated when users edit and save the corresponding configuration.
-
-
Updates the sales order by marking configured line items as converted and saves it.
When you submit the configuration, the Config status field (custcol_cpqc_item_cfg_status) has new as a value. After creating the work order, the value becomes converted.
-
If the
update
event launches the map/reduced script, the script runs a second time. However, no work order will be created because all configured line items already have up-to-date work orders.
Converting Up-To-Date Work Orders
The CPQM-MR-WOC map/reduce script can reconvert configured line items with up-to-date work orders.
To reconvert up-to-date work orders, the server-side script that launches the CPQM-MR-WOC script must:
-
Include the
custscript_cpqm_force
parameter. -
Set the
custscript_cpqm_force
parameter totrue
.
See the following example to force the conversion:
const scriptTask = _nTask.create({
taskType: _nTask.TaskType.MAP_REDUCE,
scriptId: 'customscript_cpqm_mr_woc',
params: {
custscript_cpqm_tranid: recId,
custscript_cpqm_force: true,
}
});
By setting this parameter to true:
-
All configured line items will be converted to work orders —including those with up-to-date work orders. Existing work orders will be deleted.
-
When forcing the work order conversion, the CPQM-MR-WOC script will run one time only and will not update the sales order. Because work orders are separate records, they will be saved anyway, and you will see the link to the work order in the Create WO line field on the sales order.
Solving Possible Issues
If you experience issues when creating work orders by scripting:
-
If work orders are not created, verify the logs of your server-side script to make sure the custom criteria was met map and the CPQM-MR-WOC map/reduce script was launched.
-
If work orders are not created or are created with errors, verify the logs of the CPQM-MR-WOC map/reduce script.
To view script logs, go to Customization > Scripting > Scripts. Open the script record and click the Execution Log subtab. To delete logs, click the Remove All button.