SuiteScript 2.x Scheduled Script Type Code Sample

This script sample finds and fulfills sales orders. You'll find more schedule script samples in the SuiteScript 2.x Code Samples Catalog.

Before you submit this script:

  1. Create a saved search for sales orders. Use search.create(options) and the search.Type enum to set it up with the right search id and search type.

  2. Add a script parameter on the script record's Parameters subtab. The sample expects a search id from a script parameter that was set up with the script record.

    • Set the id to custscript_searchid.

    • Set Type to Free-Form Text.

    • Assign the saved search id to the parameter on the deployment record's Parameters subtab.

This sample uses log.debug(), but you don't have to load the N/log module because log is loaded by default for all script types. For more, see log Object.

This script uses SuiteScript 2.0, but SuiteScript 2.1 is also available and includes new ES2019 features. You can write scheduled scripts with either version.

This sample is set to run on demand, not at a scheduled time. For details about script context, see context.InvocationType.

          /**
 *@NApiVersion 2.x
 *@NScriptType ScheduledScript
 */
define(['N/search', 'N/record', 'N/email', 'N/runtime'],
    function(search, record, email, runtime) {
        function execute(context) {
            if (context.type !== context.InvocationType.ON_DEMAND)
                return;
            var searchId = runtime.getCurrentScript().getParameter("custscript_searchid");
            try {
                search.load({
                    id: searchId
                }).run().each(function(result) {
                    log.debug({
                        details: 'transforming so :' + result.id + ' to item fulfillment'
                    });
                    var fulfillmentRecord = record.transform({
                        fromType: record.Type.SALES_ORDER,
                        fromId: result.id,
                        toType: record.Type.ITEM_FULFILLMENT,
                        isDynamic: false
                    });
                    var lineCount = fulfillmentRecord.getLineCount('item');
                    for (var i = 0; i < lineCount; i++) {
                        fulfillmentRecord.setSublistValue('item', 'location', i, 1);
                    }
                    var fulfillmentId = fulfillmentRecord.save();
                    var so = record.load({
                        type: record.Type.SALES_ORDER,
                        id: result.id
                    });
                    so.setValue('memo', fulfillmentId);
                    so.save();
                    return true;
                });
            } catch (e) {
                var subject = 'Fatal Error: Unable to transform salesorder to item fulfillment!';
                var authorId = -5;
                var recipientEmail = 'notify@example.com';
                email.send({
                    author: authorId,
                    recipients: recipientEmail,
                    subject: subject,
                    body: 'Fatal error occurred in script: ' + runtime.getCurrentScript().id + '\n\n' + JSON.stringify(e)
                });
            }
        }
        return {
            execute: execute
        };
    }); 

        

General Notices