SuiteScript 2.x Scheduled Script Type

Scheduled scripts are server scripts that are executed (processed) with SuiteCloud Processors. You can deploy scheduled scripts so they are submitted for processing at a future time, or at future times on a recurring basis. You can also submit scheduled scripts on demand either from the deployment record or from another script with the task.ScheduledScriptTask API.

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

You can use SuiteCloud Development Framework (SDF) to manage scheduled scripts as part of file-based customization projects. For information about SDF, see SuiteCloud Development Framework. You can use the Copy to Account feature to copy an individual scheduled script to another of your accounts. Each scheduled script page has a clickable Copy to Account option in the upper right corner. For information about Copy to Account, see Copy to Account.

You can use SuiteScript Analysis to learn about when the script was installed and how it performed in the past. For more information, see Analyzing Scripts.

Also see the Scheduled Script Best Practices section in the SuiteScript Developer Guide for a list of best practices to follow when using scheduled scripts.

Scheduled Script Use Cases

Use the scheduled script type for scheduled or on demand tasks. Your SuiteScript 2.x scheduled script should not process a large amount of data or a large number of records. It should also not be used for operations that are long running.

For example, you could use a scheduled script, if:

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 instance can use a maximum of 10,000 usage units. For additional information about governance and usage units, see SuiteScript Governance and Limits.

With SuiteScript 2.x scheduled scripts, you cannot set recovery points and you do not have the ability to yield. There is no SuiteScript 2.x equivalent to the SuiteScript 1.0 nlapiYieldScript() and nlapiSetRecoverPoint() APIs. If you need to process a large amount of data or a large number of records, use the SuiteScript 2.x Map/Reduce Script Type instead. The map/reduce script type has built in yielding and can be submitted for processing in the same ways as scheduled scripts.

Scheduled Script Entry Points

Script Entry Point

 

execute

Defines the scheduled script trigger point.

Scheduled Script API

API

 

context.InvocationType

Enumeration that holds the string values for scheduled script execution contexts.

Scheduled Script Sample

The following script sample finds and fulfills sales orders. Additional schedule script samples are available in the SuiteScript 2.x Code Samples Catalog.

Before you submit this script:

  1. Create a saved search for sales orders. You can use search.create(options) and the search.Type enum to set up a saved search with the correct search id and search type.

  2. Create a script parameter on the script record Parameters subtab. The sample accepts a search id from a script parameter that it assumes was created with the script record.

    • Set the id to custscript_searchid.

    • Set Type to Free-Form Text.

    • Assign the saved search id to the parameter. This is done on the deployment record Parameters subtab.

Note that this script sample uses the log.debug() method but does not load the N/log module. A log object is loaded by default for all script types, and you do not need to load the N/log module explicitly. For more information, see log Object.

This script sample uses SuiteScript 2.0. A newer version, SuiteScript 2.1, is also available and supports new language features that are included in the ES2019 specification. You can write scheduled scripts using either SuiteScript 2.0 or SuiteScript 2.1.

Note that this script sample is designed to execute on demand and not at any scheduled time. For more information about script context for scheduled scripts, 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

Schedule Script Failing In Custom Role

Related Topics

SuiteScript Versioning Guidelines
SuiteScript 2.1
SuiteScript 2.x Script Types
SuiteScript 2.x Bundle Installation Script Type
SuiteScript 2.x Client Script Type
SuiteScript 2.x Map/Reduce Script Type
SuiteScript 2.x Mass Update Script Type
SuiteScript 2.x Portlet Script Type
SuiteScript 2.x RESTlet Script Type
SuiteScript 2.x Suitelet Script Type
SuiteScript 2.x User Event Script Type
SuiteScript 2.x Workflow Action Script Type

General Notices