Governance Best Practices

You need to be aware of the governance usage limits of executing SuiteScript to ensure that your scripts function as expected.

When a script exceeds governance limits, the system throws a usage limit error, and the script is halted by the platform. The script cannot be resumed and, consequently, any processes that depend on that script can be terminated mid-stream. When this happens, you threaten the integrity of your data and downstream tasks and transactions.

The following sections describe the best ways to manage SuiteScript governance for your Commerce website customizations. For more information, see SuiteScript Governance and Limits.

Minimize Calls to Third-Party URLs

Third-party services can be unreliable, or slow, and any performance problems they have will be transferred to the user. Where possible, avoid making these calls (for example, through nlapiRequestURL) and, if possible, use inline or global variables instead.

Use Scheduled Scripts for High Volume I/O Tasks

Because scheduled scripts have a much higher governance usage limit (10,000 units per script) than other scripts, they provide more resources to handle processing. Due to this higher usage limit, when possible, you should write your user event scripts and Suitelets so that the high-volume I/O calls are handled by a scheduled script. For more information about usage limits, see Script Type Usage Unit Limits.

The caller script can call the scheduled script with nlapiScheduleScript. After the caller script makes the call, control immediately returns to that originating script, thereby improving the user experience by ensuring that users do not have to wait for the logic to finish executing. The invoked scheduled script is then placed in a queue and executed asynchronously.

Note:

The nlapiScheduleScript method is not currently supported within SSP applications. But, it can be called within Suitelet, RESTlet, user event, and portlet scripts.

Check Governance Units (SuiteScript 1.0)

If you use SuiteScript 1.0 scheduled scripts, you need to add a function that checks how many governance units you have left and then yields if the number of units is low.

Yielding is a SuiteScript 1.0 capability that creates a recovery point for a script, and then schedules a later time for the script to complete. With yielding, a scheduled script may not finish running all inputs and outputs, but it will process as many as possible and run the rest later. Using a checker function to yield ensures that all processed data retains its integrity.

Note:

SuiteScript 2.x does not have a method to allow you to set recovery points or provide a yield to avoid exceeding the allowed governance for a scheduled script. But, SuiteScript 2.x map/reduce scripts provide built-in yielding and can be submitted for processing in the same ways as a scheduled script.

The following sample shows how to use a checker function to yield with a SuiteScript 1.0 scheduled script:

            if (context.getRemainingUsage() <= 50)
{
  var stateMain = nlapiYieldScript();
  if( stateMain.status == 'FAILURE')
  {
    nlaplogExecution("debug", "Failed to yield script (do-while), exiting: Reason = "+ stateMain.reason + " / Size = "+ stateMain.size);
    throw "Failed to yield script";
  }
  else if ( stateMain.status = 'RESUME')
  {
    nlaplogExecution ("debug", "Resuming script (do-while) because of " + stateMain.reason+" .Size = "+ stateMain.size);

  }
} 

          

Process Large Amounts of Data with Map/Reduce (SuiteScript 2.0)

The map/reduce script type is designed for scripts that need to handle large amounts of data. It is best suited for situations where the data can be divided into small, independent parts. When the script is executed, a structured framework automatically creates enough jobs to process all of these parts. You as the user not have to manage this process. Another advantage of map/reduce scripts is that these jobs can work in parallel. You choose the level of parallelism when you deploy the script.

If a map/reduce job violates certain aspects of NetSuite governance, the map/reduce framework automatically causes the job to yield and its work to be rescheduled for later, without disruption to the script. However, be aware that some aspects of map/reduce governance cannot be handled through automatic yielding. For more information, see Map/Reduce Governance.

Because the new Map/Reduce script type automatically handles governance usage and yielding, nlapiSetRecoveryPoint and nlapiYieldScript are not included in SuiteScript 2.x.

Related Topics

Tips for SuiteScript Configuration and Code
Configuration Best Practices
Code-Level Best Practices
Search Best Practices
Standardization

General Notices