Execution Contexts

Execution contexts provide information about how or when a SuiteScript script or SuiteFlow workflow is triggered to execute. For example, a script can be triggered in response to an action in the NetSuite application, or an action occurring in another context, such as a web services integration. You can use execution context filtering to ensure that your scripts or workflows are triggered only when necessary. You can specify that a script or workflow should execute only in certain contexts, and this filtering can improve performance in contexts where the script or workflow is not required. You can also determine the execution context for a running script programmatically and use different logic depending on the context.

There are many execution contexts available in NetSuite. The Scheduled context indicates that a script or workflow was triggered in response to a scheduled script. The Debugger context indicates that the script was triggered using deployed or on-demand debugging in the SuiteScript Debugger. The following contexts were added recently to provide more control over script and workflow execution:

Important:

The Web Services context, which was available in previous releases, is no longer available. It has been renamed to SOAP Web Services.

For a full list of execution contexts, see Execution Context Types.

The execution context value for a script or workflow is dynamic and can change each time the script or workflow runs. The execution context value depends on how the script or workflow was triggered. For example, consider a context script that is deployed on customer records and uses the beforeLoad(context) entry point. When you edit a customer record using the NetSuite UI, the beforeLoad(context) entry point is triggered and the context script runs. For this execution of the script, the execution context value is User Interface. In this case, a UI event (editing a customer record) triggered the execution of the context script through the beforeLoad(context) entry point.

In the same manner, when you edit a customer record using SOAP web services, the beforeLoad(context) entry point is triggered and the context script runs. For this execution of the script, the execution context value is SOAP web services. In this case, a SOAP web services event (editing a customer record) triggered the execution of the context script through the beforeLoad(context) entry point.

However, the beforeLoad(context) entry point can be triggered in other ways. This entry point is triggered immediately before a record of the associated type (in this example, a customer record) loads, but the loading action does not need to occur in the NetSuite UI. If you create a scheduled script that runs every day and loads a record, the beforeLoad(context) entry point is triggered each time the scheduled script runs. The context script runs in response to this trigger. For these executions of the context script, the execution context value is Scheduled. In this case, an event from a scheduled script (loading a customer record) triggered the execution of the context script through the beforeLoad(context) entry point.

Execution context filtering behavior is consistent for scripts and workflows. The same values are available for both scripts and workflows, and the default selections are the same. By default, all contexts are selected except for Web Application and Web Store. Often, it is not required to trigger scripts in these contexts, so they are disabled by default to improve performance. If you want your script or workflow to be triggered in these contexts, ensure that you explicitly select them when you create your script deployment record or workflow.

When the information about a record changes, you can use system notes to determine the execution context that was used for the change. For more information, see Execution Contexts in System Notes.

For more information about execution contexts in SuiteScript and SuiteFlow, see the following:

Execution Contexts in SuiteScript

For SuiteScript scripts, you set up execution context filtering on the Context Filtering tab when you create or edit a script deployment record:

Execution Context field on the script deployment record.

Execution context values are available programmatically in scripts using the runtime.executionContext property and the runtime.ContextType enum. The REST Web Services context is used with SuiteTalk Web Services. For more information, see Using the REST Web Services SuiteScript Execution Context.

Note:

Execution contexts are available only for client scripts and user event scripts.

You can use execution contexts in SuiteScript in two ways:

  • Set up execution context filtering on the Context Filtering tab — You can select the contexts in which you want your script to execute. Your script will not execute if it is triggered in a context that is not selected. For more information, see Using the Context Filtering Tab.

  • Determine the current execution context programmatically — You can determine the context in which your script is currently executing, compare the context to the available context values in the runtime.ContextType enum, and take appropriate action. For more information, see runtime.executionContext.

Execution Contexts in SuiteFlow

For SuiteFlow workflows, you set execution context filtering in the Event Definition section when you create or edit a workflow:

Contexts field in a Suiteflow workflow.

For more information, see Execution Contexts and Workflows.

Related Topics

General Notices