SuiteScript 2.x Client Script Type
Client scripts are scripts that are executed by predefined event triggers in the client browser. They can validate user-entered data and auto-populate fields or sublists at various form events. For details, see SuiteScript 2.x Client Script Entry Points and API.
Scripts can be run on most standard records, custom record types, and custom NetSuite pages such as Suitelets. See SuiteScript Supported Records for a list of NetSuite records that support SuiteScript.
Client scripts only execute in edit mode. If you have a deployed client script with a pageInit entry point, that script does not execute when you view the form. It executes when you click Edit.
The following triggers can run a client script:
-
Loading a form for editing
-
Entering or changing a value in a field (before and after it is entered)
-
Entering or changing a value in a field that sources another field
-
Selecting a line item on a sublist
-
Adding a line item (before and after it is entered)
-
Saving a form
Record-level client scripts are executed after any existing form-based clients are run, and before any user event scripts are run.
See Script Type Usage Unit Limits for details about client script governance.
You can set the order in which client scripts execute on the Scripted Records page. See The Scripted Records Page.
You can use SuiteCloud Development Framework (SDF) to manage client scripts as part of file-based customization projects. For information about SDF, see SuiteCloud Development Framework. You can use the Copy to Account feature to copy an individual client script to another of your accounts. Each client script page has a clickable Copy to Account option in the upper right corner. For information about Copy to Account, see Copy to Account.
You can use SuiteScript Analysis to learn about when the script was installed and how it performed in the past. For more information, see Analyzing Scripts.
For additional information about SuiteScript 2.x client scripts, see the following:
SuiteScript Client Script Sample
For help with writing scripts in SuiteScript 2.x, see SuiteScript 2.x Hello World and SuiteScript 2.x Entry Point Script Creation and Deployment.
The following sample shows a client script applied to a sales order form. The script performs the following tasks:
-
When the form loads for editing, the pageInit event (entry point) sets the customer field to a specific customer.
-
When form changes are saved, the saveRecord event ensures that the customer field is set and at least one sales order item is listed.
-
When editing the quantity of a sales order item, the validateField event ensures that the number is less than three.
-
When selecting a sales order item, the fieldChanged event updates a memo field to indicate that the item was selected.
-
When a specific sales order item is selected, the postSourcing event updates the price level for that particular item.
-
When an existing partner is selected, the lineInit event changes the selected partner to a specific partner.
-
When a partner is deleted, the validateDelete event updates a memo field to indicate that the partner was deleted.
-
When adding a new partner or editing an existing partner, the validateInsert and validateLine events ensure that their contribution is set to 100%.
/**
*@NApiVersion 2.x
*@NScriptType ClientScript
*/
define(['N/error'], function(error) {
function pageInit(context) {
if (context.mode !== 'create')
return;
var currentRecord = context.currentRecord;
currentRecord.setValue({
fieldId: 'entity',
value: 107
});
}
function saveRecord(context) {
var currentRecord = context.currentRecord;
if (!currentRecord.getValue({fieldId: 'entity'}) || currentRecord.getLineCount({sublistId: 'item'}) < 1)
throw error.create({
name: 'MISSING_REQ_ARG',
message: 'Please enter all the necessary fields on the salesorder before saving'
});
return true;
}
function validateField(context) {
var currentRecord = context.currentRecord;
var sublistName = context.sublistId;
var sublistFieldName = context.fieldId;
var line = context.line;
if (sublistName === 'item') {
if (sublistFieldName === 'quantity') {
if (currentRecord.getCurrentSublistValue({
sublistId: sublistName,
fieldId: sublistFieldName
}) < 3)
currentRecord.setValue({
fieldId: 'otherrefnum',
value: 'Quantity is less than 3'
});
else
currentRecord.setValue({
fieldId: 'otherrefnum',
value: 'Quantity accepted'
});
}
}
return true;
}
function fieldChanged(context) {
var currentRecord = context.currentRecord;
var sublistName = context.sublistId;
var sublistFieldName = context.fieldId;
var line = context.line;
if (sublistName === 'item' && sublistFieldName === 'item')
currentRecord.setValue({
fieldId: 'memo',
value: 'Item: ' + currentRecord.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'item'
}) + ' is selected'
});
}
function postSourcing(context) {
var currentRecord = context.currentRecord;
var sublistName = context.sublistId;
var sublistFieldName = context.fieldId;
var line = context.line;
if (sublistName === 'item' && sublistFieldName === 'item')
if (currentRecord.getCurrentSublistValue({
sublistId: sublistName,
fieldId: sublistFieldName
}) === '39')
if (currentRecord.getCurrentSublistValue({
sublistId: sublistName,
fieldId: 'pricelevels'
}) !== '1-1')
currentRecord.setCurrentSublistValue({
sublistId: sublistName,
fieldId: 'pricelevels',
value: '1-1'
});
}
function lineInit(context) {
var currentRecord = context.currentRecord;
var sublistName = context.sublistId;
if (sublistName === 'partners')
currentRecord.setCurrentSublistValue({
sublistId: sublistName,
fieldId: 'partner',
value: '55'
});
}
function validateDelete(context) {
var currentRecord = context.currentRecord;
var sublistName = context.sublistId;
if (sublistName === 'partners')
if (currentRecord.getCurrentSublistValue({
sublistId: sublistName,
fieldId: 'partner'
}) === '55')
currentRecord.setValue({
fieldId: 'memo',
value: 'Removing partner sublist'
});
return true;
}
function validateInsert(context) {
var currentRecord = context.currentRecord;
var sublistName = context.sublistId;
if (sublistName === 'partners')
if (currentRecord.getCurrentSublistValue({
sublistId: sublistName,
fieldId: 'contribution'
}) !== '100.0%')
currentRecord.setCurrentSublistValue({
sublistId: sublistName,
fieldId: 'contribution',
value: '100.0%'
});
return true;
}
function validateLine(context) {
var currentRecord = context.currentRecord;
var sublistName = context.sublistId;
if (sublistName === 'partners')
if (currentRecord.getCurrentSublistValue({
sublistId: sublistName,
fieldId: 'contribution'
}) !== '100.0%')
currentRecord.setCurrentSublistValue({
sublistId: sublistName,
fieldId: 'contribution',
value: '100.0%'
});
return true;
}
function sublistChanged(context) {
var currentRecord = context.currentRecord;
var sublistName = context.sublistId;
var op = context.operation;
if (sublistName === 'item')
currentRecord.setValue({
fieldId: 'memo',
value: 'Total has changed to ' + currentRecord.getValue({fieldId: 'total'})
});
}
return {
pageInit: pageInit,
fieldChanged: fieldChanged,
postSourcing: postSourcing,
sublistChanged: sublistChanged,
lineInit: lineInit,
validateField: validateField,
validateLine: validateLine,
validateInsert: validateInsert,
validateDelete: validateDelete,
saveRecord: saveRecord
};
});
Related Topics
- SuiteScript 2.x Script Types
- SuiteScript 2.x Bundle Installation Script Type
- SuiteScript 2.x Map/Reduce Script Type
- SuiteScript 2.x Mass Update Script Type
- SuiteScript 2.x Portlet Script Type
- SuiteScript 2.x RESTlet Script Type
- SuiteScript 2.x Scheduled Script Type
- SuiteScript 2.x Suitelet Script Type
- SuiteScript 2.x User Event Script Type
- SuiteScript 2.x Workflow Action Script Type