Anda di halaman 1dari 4

2/28/2014 Execution Context APIs

SuiteCloud (Customization, Scripting, and Web Services) : SuiteScript : SuiteScript API : SuiteScript Functions : Execution Context APIs

Execution Context APIs

Context APIs are used to get system information or metadata about a script that is running, a
user in a NetSuite account, or certain settings that have been applied to account.
All APIs listed below are in alphabetical order.
nlapiGetContext()
nlapiGetDepartment()
nlapiGetLocation()
nlapiGetRole()
nlapiGetSubsidiary()
nlapiGetUser()
nlapiLogExecution(type, title, details)
nlobjContext

nlapiGetContext()
Used to branch scripts depending on the metadata or context of the execution. For example,
you may want the script to perform in one way when a form is accessed via the UI and another
when the form is accessed via Web services.
This API is supported in client, user event, scheduled, portlet, and Suitelet scripts.

Returns
nlobjContext object containing information (metadata) about the current user or script
context.
You must use the nlobContext. getSetting method on nlapiGetContext to reference script
parameters. For example, to obtain the value of a script parameter called
custscript_case_field, you must use the following code:

nlapiGetContext().getSetting('SCRIPT', 'custscript_case_field')

Specifying Web Services Context


To cause a form to behave differently in Web Services versus the UI, you can do one of the
following:
Write context-specific SuiteScript code and use the nlapiGetContext function to branch the
code
Disable SuiteScript in Web services
However, both Client and Server SuiteScripts are written to enforce customized business
rules that may need to be enforced regardless of the mechanism by which a record is
created or updated within NetSuite. This is particularly true for customers who deploy a
SuiteCloud partner application and want to be sure their business rules are still respected.
Since Client SuiteScript often has browser-specific behavior that requires user action and
cannot automatically run during a Web Services call, NetSuite recommends that you
disable Client SuiteScript and deploy Server SuiteScript for those business conditions that
need to be enforced in all cases.
To specify that Server SuiteScript should never execute during a Web services call, enable
the Disable Server-side Scripting preference on the Web Services Preference page at
Setup > Integration > Web Services.

Important: Only enable this preference when data submitted via Web services does NOT
need to adhere to custom business logic and workflows that may be executed via
Server SuiteScript.

Back to Execution Context APIs | Back to SuiteScript Functions

https://system.na1.netsuite.com/help/helpcenter/en_US/Output/Help/SuiteCloudCustomizationScriptingWebServices/SuiteScript/ExecutionContextAPIs.html… 1/4
2/28/2014 Execution Context APIs

nlapiGetDepartment()
This API is supported in client, user event, scheduled, portlet, and Suitelet scripts.

Returns
The integer value of the current user's department (for example, 3, 9, or 1)
Back to Execution Context APIs | Back to SuiteScript Functions

nlapiGetLocation()
Returns the integer value of the current user's location. This API is supported in client, user
event, scheduled, portlet, and Suitelet scripts.

Returns
The integer value of the current user's location (for example, 5, 7, -2). Note that if a
location has not been set, the value of -1 is returned.
Back to Execution Context APIs | Back to SuiteScript Functions

nlapiGetRole()
Returns the internalId for the current user's role. This API is supported in client, user event,
scheduled, portlet, and Suitelet scripts.

Returns
The integer value of the current user's role (for example: 1, 3, or 5). Note that the value of
-31 is returned if a user cannot be properly identified by NetSuite. This occurs when the
user has not authenticated to NetSuite, for example when using externally available (
Available without Login ) Suitelets or online forms.
Back to Execution Context APIs | Back to SuiteScript Functions

nlapiGetSubsidiary()
Returns the internalId for the current user's subsidiary. This API is supported in client, user
event, scheduled, portlet, and Suitelet scripts.

Returns
The integer value for the current user's subsidiary (for example 1, 3, or 5). Note that if a
subsidiary has not been set (for example, the subsidiaries feature is not turned on in the
user's account), the value of 1 is returned if this function is called.
Back to Execution Context APIs | Back to SuiteScript Functions

nlapiGetUser()
Returns the internalId of the current NetSuite user. This API is supported in client, user event,
scheduled, portlet, and Suitelet scripts.

Returns
The integer value of the current user (for example, 195, 25, 21). Note that the value of
-4 is returned if a user cannot be properly identified by NetSuite. This occurs when the
user has not authenticated to NetSuite, for example when using externally available (
Available without Login ) Suitelets or online forms.

Example
The following sample shows how to use nlapiGetUser in conjunction with nlapiSendEmail.
In this sample, the internal ID of the currently logged in user is passed to the author
argument in nlapiSendEmail, which is a required argument in this API.

function afterSubmitEmail(type)
https://system.na1.netsuite.com/help/helpcenter/en_US/Output/Help/SuiteCloudCustomizationScriptingWebServices/SuiteScript/ExecutionContextAPIs.html… 2/4
2/28/2014 Execution Context APIs
{
//User event script deployed to purchase orders.
//Set the afterSubmit type to approve. As soon as the PO is
//approved, an email is sent.
if (type == 'approve')

//Get the user ID of the person approving the PO. This will be the email author.
var userId = nlapiGetUser();
//Send an email to the supervisor, K. Wolfe in this case.
var sendEmail = nlapiSendEmail(userId, 'kwolfe@netsuite.com', 'Purhase Order
Notification', 'Purchase
order approved', null, null, 'transaction', null);
}

See also
nlapiSendEmail(author, recipient, subject, body, cc, bcc, records, attachments)
Back to Execution Context APIs | Back to SuiteScript Functions

nlapiLogExecution(type, title, details)


This API is supported in Suitelet, scheduled, portlet, user event, and record-level (global) client
scripts.
Use this API to log an entry on the Execution Log subtab. The Execution Log subtab appears on
the Script Deployment page for a script. See Using the Script Execution Log to learn more about
writing logs to the Execution Log subtab.

Note: When you are debugging a script in the SuiteScript Debugger, log details appear on the
Execution Log tab of the SuiteScript Debugger, NOT the script's Script Deployment page.

The log type argument is used in conjunction with the Log Level field on the Script Deployment
to determine whether to log an entry on the Execution Log subtab. If a log level is defined on a
Script Deployment, then only nlapiLogExecution calls with a log type equal to or greater than
this log level will be logged. This is useful during the debugging of a script or for providing useful
execution notes for auditing or tracking purposes. See Setting Script Execution Log Levels for
more information using the Log Level field.

Important: Be aware that NetSuite governs the amount of logging that can be done by a company
in any given 60 minute time period. For complete details, see Governance on Script
Logging.

Also note that if the script's deployment status is set to Released, then the default Log Level is
ERROR. If the status is set to Testing, the default Log Level is DEBUG.

Note: The Execution Log tab also lists notes returned by NetSuite such as error messages. For
additional information on using the Execution Log, see Using the Script Execution Log in the
NetSuite Help C enter.

Parameters
type {string} [required] - One of the following log types:
DEBUG
AUDIT
ERROR
EMERGENCY
title {string} [optional] - A title used to organize log entries (max length: 99 characters). If
you set title to null or empty string (''), you will see the word “Untitled” appear in your log
entry.
details {string} [optional] - The details of the log entry (max length: 3999 characters)

Throws
SSS_MISSING_REQD_ARGUMENT - if no value is specified for title.

Returns

https://system.na1.netsuite.com/help/helpcenter/en_US/Output/Help/SuiteCloudCustomizationScriptingWebServices/SuiteScript/ExecutionContextAPIs.html… 3/4
2/28/2014 Execution Context APIs
void

Example 1
This sample creates a new Customer record. When this script runs, execution details are
logged on the Execution Log subtab on the Script Deployment page.

//Create a new Customer record


var newCust = nlapiCreateRecord('customer');
//Set the title field on the Customer record
newCust.setFieldValue('title', 'My New Customer');

var custId = nlapiSubmitRecord(newCust, true);


nlapiLogExecution('DEBUG', 'customer record created successfully', 'ID = ' +
custId);

Example 2
This snippet shows a search against sales orders, based on specified search filters and
search columns. After the search is complete, the remaining units for the script will be
logged on the Execution Log tab. If you are worried that your script will exceed unit
governance limits, it is useful to track unit usage in the Execution Log.

//Search for the sales orders with trandate of today


var todaySO = nlapiSearchRecord('salesorder', null, todaySOFilters,
todaySOColumns);

nlapiLogExecution('DEBUG', 'Remaining usage after searching sales orders from


today', context.getRemainingUsage());

Back to Execution Context APIs | Back to SuiteScript Functions

nlobjContext
See nlobjContext - defined in the section on Standard Objects.
Back to Execution Context APIs | Back to SuiteScript Functions

https://system.na1.netsuite.com/help/helpcenter/en_US/Output/Help/SuiteCloudCustomizationScriptingWebServices/SuiteScript/ExecutionContextAPIs.html… 4/4

Anda mungkin juga menyukai