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. Here's what the script does:
-
When you edit the form, the pageInit event sets the customer field to a specific customer.
-
When you save changes, the saveRecord event checks that the customer field is set and you've got at least one sales order item.
-
When you edit a sales order item's quantity, the validateField event checks it's less than three.
-
When you select a sales order item, the fieldChanged event updates a memo field to show it was selected.
-
When you select a specific sales order item, the postSourcing event updates its price level.
-
When you select an existing partner, the lineInit event switches the selected partner to a specific partner.
-
When you delete a partner, the validateDelete event updates a memo field to show it was deleted.
-
When you add or edit a partner, the validateInsert and validateLine events check 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
};
});