SuiteScript 2.x Scheduled Script Type

Scheduled scripts are server scripts processed by SuiteCloud Processors. You can set up scheduled scripts to run one time in the future or on a recurring schedule. You can also run scheduled scripts on-demand, either from the deployment record or another script using the task.ScheduledScriptTask API.

Note:

To handle large datasets, use map/reduce scripts instead—these are newer and support parallel processing and better governance. See SuiteScript 2.x Map/Reduce Script Type.

For additional information about SuiteScript 2.x scheduled scripts, see the following:

You can manage scheduled scripts with SuiteCloud Development Framework (SDF) as part of file-based projects. To learn more, see SuiteCloud Development Framework. Use Copy to Account to copy a scheduled script to another account—just click Copy to Account at the top right of the script page. For details, see Copy to Account.

Use SuiteScript Analysis (part of the Application Performance Management SuiteApp) to see when a script was installed and how it’s performed. For more, see Analyzing Scripts.

For best practices, check the Scheduled Script Best Practices in the SuiteScript Developer Guide.

Scheduled Script Use Cases

Use scheduled scripts for tasks that need to run on a set schedule or on-demand. Don’t use them for big data jobs or long-running operations.

Here are a few good reasons to use a scheduled script:

  • You need to log basic information about a recurring schedule

  • You want to schedule a maintenance script to run later

  • You need to create and then purge temporary records

  • You need to asynchronously execute logic within another server script

Note:

If you previously used SuiteScript 1.0 scheduled scripts, many of the use cases for those scripts now apply to the SuiteScript 2.x SuiteScript 2.x Map/Reduce Script Type.

Scheduled Script Governance

Each scheduled script run can use up to 10,000 usage units. For more info, see SuiteScript Governance and Limits.

SuiteScript 2.x scheduled scripts don’t let you set recovery points or yield—there’s no direct replacement for nlapiYieldScript() or nlapiSetRecoverPoint(). If you need to process lots of records, switch to the SuiteScript 2.x Map/Reduce Script Type, which supports yielding and works much like scheduled scripts.

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—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
        };
    }); 

          

Related Support Article

Related Topics

General Notices