Code-Level Best Practices
When developing SuiteScript for your Commerce customizations, you should follow these code-level guidelines:
-
Use only supported and documented APIs, methods, and objects.
-
Keep in mind that DOM references in SuiteScript are not supported.
-
Use SuiteScript 2.0 for new scripts that you develop, and consider converting any SuiteScript 1.0 scripts to SuiteScript 2.0.
-
Note that you cannot use:
-
SuiteScript 2.x user event scripts and client scripts at the same time for scriptable cart. In most cases, it is better to use SuiteScript 1.0 for user event scripts and SuiteScript 2.x for client event scripts, but the reverse is also supported.
-
SuiteScript 2.x within SuiteCommerce SSPs.
To compare SuiteScript 1.0 to SuiteScript 2.x, see SuiteScript 1.0 to SuiteScript 2.x API Map – Functions (nlapi) and SuiteScript 1.0 to SuiteScript 2.x API Map – Objects (nlobj).
-
-
Use caching as follows:
In 2.x, the cache API enables you to load data into a cache and make it available to one or more scripts. This feature reduces the amount of time required to retrieve data.
Load the cache module to enable temporary, short-term storage of data, which is stored according to its specified time to live (TTL). The cache module is supported by all server-side script types, and can improve performance by eliminating the need for scripts to retrieve the same piece of data more than a single time. For more information, see N/cache Module.
-
Be mindful of limitation of 1,000 rows.
-
Results are limited to the first 1000 results when using the
N/search
module in SuiteScript 2.x. For more information, see Search Result Limits and N/search Module. -
SuiteScript 2.x has a search pagination API that enables you to page through search results, which improves performance and gives you an intuitive way to efficiently traverse search result data.
-
-
Consolidate
afterSubmit
scripts that perform similar record operation calls. -
Use asynchronous processing with scheduled scripts.
If your script performs numerous record updates or POST operations after submission, consider moving it to a scheduled script.
For example, if you have a sales order
afterSubmit
script that sends an email to a sales rep, creates a custom record for tracking, and then performs a call to an external site, ask yourself whether this needs to happen immediately after the user has hit submit, or whether it can be performed outside of the process at another time. -
Convert
afterSubmit
logic tobeforeSubmit
to remove overhead from load, set, and submit records onafterSubmit
.
The following sections describe more code-level best practices to follow when developing SuiteScript for your Commerce customizations.
Branch Scripts Depending on the Metadata or Context of Execution
Before they run, each of your scripts should check where they are being accessed from to determine their context. Your scripts should run different code, depending on whether they are run from the web store or the backend user interface. You can use the following functions and methods to determine a script’s context:
-
For SuiteScript 2.x, use
runtime.getCurrentScript()
,runtime.getCurrentSession()
,runtime.getCurrentUser()
Create a branch at the start of a function call, and then determine the succeeding logic per the script’s context. Doing so is particularly important for client scripts that are executing through the scriptable cart or in the checkout.
Use Set and Get Methods
For better performance when setting and getting values, use SuiteScript set and get methods. These methods remove overhead when loading entire record objects.
Value Type |
Methods |
---|---|
Field |
|
Line Item Count |
nlapiGetLineItemCount, nlapiSetLineItemCount |
Line Item Value |
nlapiSetLineItemCount, nlapiSetLineItemValue (when working on fields for current record) |