Overview and types of SuiteScript with sample code

SuiteScript is a powerful tool for flexibly customizing NetSuite. It can be used for a wide range of applications, from streamlining business processes to automating data processing and integrating with external systems.

In this article, we will give an overview of SuiteScript and its main types, as well as introduce simple sample code for each script.

What is SuiteScript?

SuiteScript is a customization framework for NetSuite and is based on JavaScript. By using this script, you can:

– Automate workflow: Automate repetitive tasks and inputs

– Customize data processing: Manipulate data under specific conditions

– System integration: Exchange data with external systems

SuiteScript is available in several types for different purposes SuiteScript is available in several types for different purposes. Each has its own specialized role, so it is important to select the appropriate script.

Types of SuiteScript and sample code

1. Client Script

Summary

It operates when a user interacts with a NetSuite screen. It is used to validate input and automatically change field values.

Sample code

Below is an example of a client script that checks for a blank “Amount” before saving.

/**

* @NApiVersion 2.x

* @NScriptType ClientScript

*/

define([‘N/ui/message’], function(message) {

function saveRecord(context) {

var currentRecord = context.currentRecord;

var amount = currentRecord.getValue({ fieldId: ‘amount’ });

if (!amount) {

alert(‘Amount is a required field. Please enter it.’) ;

return false; // cancel save

}

return true; // allow save

}

return {

saveRecord: saveRecord

};

});

2. User Event Script

Summary

It is executed when a record is saved, edited, or deleted. It is suitable for automatically setting values and updating related records.

Sample code

Below is an example of setting a “memo” before a new customer record is saved.

/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
*/
define(['N/record'], function(record) {
function beforeSubmit(context) {
if (context.type === context.UserEventType.CREATE) {
var newRecord = context.newRecord;
newRecord.setValue({ fieldId: 'memo', value: 'New Clients.' });
}
}
return {
beforeSubmit: beforeSubmit
};
});

3. Scheduled Script

Summary

Used for periodic batch processing and large-scale data processing.

Sample code

Below is an example of a script to bulk update the “Memo” of a customer record.

/**
* @NApiVersion 2.x
* @NScriptType ScheduledScript
*/
define(['N/record', 'N/search'], function(record, search) {
function execute(context) {
var customerSearch = search.create({
type: search.Type.CUSTOMER,
filters: [],
columns: ['internalid']
});

customerSearch.run().each(function(result) {
var customerId = result.getValue({ name: 'internalid' });
var customerRecord = record.load({
type: record.Type.CUSTOMER,
id: customerId
});

customerRecord.setValue({
fieldId: 'memo',
value: 'Scheduled script updates'
});
customerRecord.save();
return true; // Next record
});
}
return {
execute: execute
};
});

4. Suitelet

Summary

Used to create custom UIs and unique data entry forms.

Sample code

Below is an example of a suitelet that displays a simple HTML form.

/**
* @NApiVersion 2.x
* @NScriptType Suitelet
*/
define(['N/ui/serverWidget'], function(serverWidget) {
function onRequest(context) {
if (context.request.method === 'GET') {
var form = serverWidget.createForm({ title: 'custom form' });
form.addField({
id: 'custpage_field',
type: serverWidget.FieldType.TEXT,
label: 'text field'
});
form.addSubmitButton({ label: 'send' });
context.response.writePage(form);
}
}
return {
onRequest: onRequest
};
});

5. Map/Reduce Script

Summary

Parallel processing scripts for efficient large-scale data processing.

Sample code

Below is an example of a map-reduce script that updates a customer record.

/**
* @NApiVersion 2.x
* @NScriptType MapReduceScript
*/
define(['N/record', 'N/search'], function(record, search) {
function getInputData() {
return search.create({
type: search.Type.CUSTOMER,
filters: [],
columns: ['internalid']
});
}

function map(context) {
var customerId = JSON.parse(context.value).id;
var customerRecord = record.load({
type: record.Type.CUSTOMER,
id: customerId
});

customerRecord.setValue({
fieldId: 'memo',
value: 'update with Map Reduce Script'
});
customerRecord.save();
}

return {
getInputData: getInputData,
map: map
};
});

Summary

By utilizing SuiteScript, you can customize NetSuite more flexibly and improve your business efficiency. Why not try it out by referring to the sample code of each script introduced in this issue?