Governance Best Practices

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

If a script goes over its governance limits, the system throws a usage limit error and stops the script. The script can't be resumed, so any processes that depend on it might get cut off mid-stream. When this happens, you risk the integrity of your data and downstream tasks.

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 issues they have will affect your users. Whenever you can, avoid making these calls (like nlapiRequestURL) and use inline or global variables instead.

Use Scheduled Scripts for High Volume I/O Tasks

Scheduled scripts have a much higher governance usage limit (10,000 units per script) than other scripts, so they give you more resources for processing. Because of this higher limit, try to write your user event scripts and Suitelets so 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'll need to add a function that checks how many governance units you have left and yields if the number is low.

Yielding in SuiteScript 1.0 creates a recovery point for a script and schedules it to finish later. With yielding, a scheduled script might not finish all its inputs and outputs at once, but it'll process as many as it can and handle the rest later. Using a checker function to yield ensures that all processed data stays accurate.

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 made for scripts that handle large amounts of data. It's best suited for situations where you can break the data into small, independent parts. When you run the script, the framework automatically creates enough jobs to process all the parts. You don't have to manage this process yourself. Another advantage is that these jobs can run in parallel. You choose the level of parallelism when you deploy the script.

If a map/reduce job goes over certain NetSuite limits, the framework automatically makes the job yield and reschedules its work for later, without disrupting the script. However, some map/reduce governance can't be handled by 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

General Notices