Creating Work Orders for Configured Items Using Scripts

Note:

If your sales orders contain a high number of configured items to convert to work orders, switch from the scheduled script to the CPQM-MR-WOC map/reduce script. Don't use the scheduled script in new implementations.

NetSuite CPQ Manufacturing lets you create work orders for configured items using scripts. 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 using a script:

  1. In SuiteScript 2.x, create one of these server-side script types to launch the CPQM-MR-WOC map/reduce script:

    • 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.

  2. 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.

  3. Pass the internal ID of the sales order to the custscript_cpqm_tranid parameter.

  4. To update existing work orders, use the custscript_cpqm_force parameter and set it to true. 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

A common use case is launching the work order creation process from a user event script when users or other scripts save or edit 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. For more information about user event scripts, see SuiteScript 2.x User Event Script Type.

To create the user event script:

  1. Pass the internal ID of the sales order to the custscript_cpqm_tranid parameter.

  2. The submit event type can be create, edit or both. If you want to:

    • Create work orders only for new sales orders, run the script for the create event only.

    • Create work orders after editing and resaving a sales order, run the script for both the create and edit events.

  3. To define when work orders are created, set up a custom condition. Use a custom function that verifies sales order fields such as status, customer, and so on.

Example

This user event script launches the CPQM-MR-WOC script to create work orders for configured items. The script runs under these conditions:

  1. When creating or editing sales orders (create and edit events).

  2. When the Memo field on the sales order contains auto-create-wo.

              /**
 * @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 a server-side script, launches it, the CPQM-MR-WOC map/reduce script performs these tasks:

  1. Loads the sales order.

  2. Makes a list of configured line items to process.

    Note:

    This list excludes configured line items with up-to-date work orders.

  3. 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.

  4. Updates the sales order by marking configured line items as converted and saves it.

    When users submit the configuration, the Config status field (custcol_cpqc_item_cfg_status) has new as a value. After creating the work order, the value changes to converted.

  5. If the update event launches the map/reduced script, the script runs a second time. However, no new 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:

  1. Include the custscript_cpqm_force parameter.

  2. Set the custscript_cpqm_force parameter to true.

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 won't update the sales order. Because work orders are separate records, they'll be saved, and you'll 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 using scripts:

  • If work orders aren't created, verify the logs of your server-side script to make sure the custom criteria was met and the CPQM-MR-WOC map/reduce script was launched.

  • If work orders aren't 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.

Related Topics

General Notices