Code-Level Best Practices

When developing SuiteScript for your Commerce customizations, you should follow these code-level guidelines:

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

Record.getField(options), Record.setValue(options)

Line Item Count

nlapiGetLineItemCount, nlapiSetLineItemCount

Line Item Value

nlapiSetLineItemCount, nlapiSetLineItemValue (when working on fields for current record)

Related Topics

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

General Notices