Its been a while since suitescript 2.0
made an appearance into Netsuite with Version 2015 Release 2. Netsuite introduced
a SuiteScript 2.0- a complete re-factor of the SuiteScript model driving
towards a more modular based development approach. Recently, I was trying to
have some hands on it and thought of sharing few keynotes based on my
exploration.
Advantages to SuiteScript 2.0
- Modular
Architecture
- Modern
JavaScript Syntax and Behavior
- New
and Improved API Functionality
- Asynchronous
Client Side Processing (Promises)
- New
Batch Processing Framework (Map/Reduce Script Type)
Lets see some examples:
Load a record
require(['N/record'], function(RECORDMODULE){
var recordType = RECORDMODULE.Type.ACCOUNT; //The type of record to load.
var recordID = 10011; //The internal ID of the existing record in NetSuite.
var isDynamic = true; //Determines whether to load the record in dynamic mode.
var loadedRecord = RECORDMODULE.load({
type: recordType,
id: recordID,
isDynamic: isDynamic,
});
});
Create Search
We can use search.create to build out our search object or
search.load to load a saved search. Then we can invoke run on the resulting search object.
Finally, we will process the results in two ways:
- Use the
each method and a callback
- Use the
getRange method to get a specific number of results
In the example below, I've imported N/search into my module
as s and shown the usage of the each method.
- Summary Search (Suitescript 2.0)
// Assuming N/search is imported as `s`
var demoSalesOrderSearch = s.create({
type: 'salesorder'
// Use the summary property of a Column to perform grouping/summarizing
columns: [{
name: 'salesrep',
summary: s.Summary.GROUP
},{
name: 'internalid',
summary: s.Summary.COUNT
}],
filters: [{
name: 'mainline',
operator: 'is',
values: ['T']
}]
});
demoSalesOrderSearch.run().each(function (result) {
var repId = result.getValue({
"name": "salesrep",
"summary": s.Summary.GROUP
});
var repName = result.getText({
"name": "salesrep",
"summary": s.Summary.GROUP
});
var orderCount = parseInt(result.getValue({
"name": "internalid",
"summary": s.Summary.COUNT
}), 10);
log.debug({
"title": "Order Count by Sales Rep",
"details": repName + " has sold " + orderCount + " orders."
});
});
- Search with criteria search
require(['N/search'], function(SEARCHMODULE){
var type = 'transaction';
var columns = [];
columns.push(SEARCHMODULE.createColumn({
name: 'internalid'
}));
columns.push(SEARCHMODULE.createColumn({
name: 'formulanumeric',
formula: '{quantity}-{quantityshiprecv}'
}));
var salesOrdersArray = [1001,2001,3001,4001];
var filters = [];
filters.push(['type', 'anyof', 'SalesOrd']);
filters.push('and');
filters.push(['mainline', 'is', 'F']);
filters.push('and');
filters.push(['internalid', 'anyof', salesOrdersArray]);
var mySearchObj = {};
mySearchObj.type = type;
mySearchObj.columns = columns;
mySearchObj.filters = filters;
var mySearch = SEARCHMODULE.create(mySearchObj);
var resultset = mySearch.run();
var results = resultset.getRange(0, 1000);
for(var i in results){
var result = results[i];
var row = {};
for(var k in result.columns){
log.debug('Result is ' + result.getValue(result.columns[k])); //result
}
}
});
require(['N/search'], function(SEARCHMODULE){
var savedSearchId = 'customsearch_customsearch';
var mySearch = SEARCHMODULE.load(savedSearchId);
var resultset = mySearch.run();
var results = resultset.getRange(0, 1000);
for(var i in results){
var result = results[i];
for(var k in result.columns){
log.debug('Result is ' + result.getValue(result.columns[k])); //result
}
}
});
If you want to explore more related to Suitescript 2.0 check out the below resources: