SuiteScript 2.1 gives developers modern JavaScript tools to use in their NetSuite projects.
What is SuiteScript 2.1?
Initially released in the beginning of 2020, SuiteScript 2.1 provides native support for JavaScript ECMA 2018. There are many benefits to this updated version of JavaScript. The most significant improvements to JavaScript occured in 2015 with ES6, but ECMA 2018 includes these important improvements, and others.
Here are some highlights that might be of interest to SuiteScript developers:
let
and const
Compatibility with Let
and const
are variable declarations that developers can use in place of var
. Both let
and const
are blocked-scoped declarations as opposed to the function-scoping or global-scoping of var
. A block scope is essentially anything organized by {}
in your code. This means that a variable declared with let
in an if statement will only be available in that if statement. With var
, such a variable would be available in the entire function, which is confusing.
The let
declaration is similar to var
in that it can change. However, it is different from var
in two significant ways. First, it is limited to the scope of the block. And secondly, it cannot be re-declared. With var
, it is possible to make the same declaration twice without error (e.g., var foo = 0
; var foo = 5
;). With let
, this is not possible. Perhaps this may encourage some of us developers to write cleaner code. 🙂
The const
(short for “constant”) declaration is immutable. It cannot be changed later on. Once declared within a code block, it must remain the same within that block. This can prevent strange bugs from happening.
Read more about “let” and “const”.
Asynchronous Functions
SuiteScript 2.0 already supported Promises with the SuiteScript API, but SuiteScript 2.1 brings support for vanilla JavaScript Promises, including async
and await
keywords. We haven’t found the need for these yet on our SuiteScript team, but we may explore these new possibilities in the future!
Read more about async functions here.
Arrow Functions
Arrow functions are a unique shorthand that can be used in place of function declarations. There are numerous use-cases where these can be helpful (and even crucial, in some rare cases).
We have found arrow functions to be helpful in running searches:
mySearch.run.each((result) => {
log.debug("result", result);
})
Another example could be for looping over data:
lineCount.forEach((line) => {
log.debug("line", line);
})
There are many more use cases, so feel free to go explore with this new tool!
Read more about arrow functions here.
Spread operator
The spread operator essentially gathers any remaining data from an array or an object. There are many use cases for this tool, but here’s an example where we copy an array and add data to it:
let fruits = ['apple', 'orange', 'banana'];
let food = [...fruits];
food.push('donut');
Read about the spread operator here.
How to write in SuiteScript 2.1?
To begin using SuiteScript 2.1, simply change the NApiVersion at the top of your script.
/**
* @NApiVersion 2.1
* @NScriptType ClientScript
*/
There is a setting in NetSuite (Setup > Company > General Preferences > EXECUTE SUITESCRIPT 2.X SERVER SCRIPTS AS) to turn all 2.x
labeled scripts into 2.1
, but most clients would prefer to wait until all the wrinkles of SuiteScript 2.1 are ironed out before switching entirely. But it may be safe to start using SuiteScript 2.1 for new scripts. Just be aware that when hardcoded as @NApiVersion 2.1
instead of @NApiVersion 2.x
, these scripts will remain as 2.1 indefinitely until changed. Whenever SuiteScript 2.2 comes out, you may need to go back to these scripts and change the API version back to 2.x or 2.2. We do find the benefits of 2.1 to outweigh this cost, however.
Cautions
As hinted at previously, you may find some occasional bugs with SuiteScript 2.1 as of writing this article. For example, when we try uploading a perfectly legitimate script with 2.1 using the SuiteCloud CLI, we occasionally get an error that a certain module is not supported. You may find other rare abnormalities as you begin using this new version of SuiteScript.
Conclusion
SuiteScript 2.1 brings some long-awaited and exciting JavaScript features. We are excited to begin the process of switching over to SuiteScript 2.1 gradually. Have you tried out any of these modern JavaScript features yet? Let us know in the comments below. And don’t forget to subscribe to our mailing list for weekly blogs posts on all things SuiteScript.