11 Secret Suitescript 2.1 Features That Reptilians Don’t Want You To Know
Suitescript 2.1 is out of beta and beginning on 2021.1 release all Suitescript 2.x scripts will be running as Suitescript 2.1 so better test your existing scripts for any bugs.
To change your scripts to use Suitescript 2.1 all you have to do is change this JSTag:
From:
1 |
“@NApiVersion 2.x” |
To:
1 |
“@NApiVersion 2.1” |
It’s also worth mentioning that as of today Suitescript 2.1 is not available for client scripts yet so bear that in mind.
“let” keyword:
This is basically a super local variable, it’s very useful for using them in for loops. Now you can have multiple nested for loops and declare all of your loop counter variables “i” and not worry about conflicts.
Example
Using var:
1 2 3 4 5 |
var i = 5; for (var i = 0; i < 10; i++) { // after last loop i equals 10 } // Here i is 10 |
Using let:
1 2 3 4 5 |
let i = 5; for (let i = 0; i < 10; i++) { // after last loop i equals 10 } // Here i is 5 |
For details check out w3schools.
“for of” statement
This is another way to loop through an array, I find it easier to write although it only works on iterables so this means it doesn’t work on objects as opposed to “for in” which works on both objects and arrays.
Example
Considering:
1 |
var array = [1]; |
Using “for in”:
1 2 3 4 5 |
for (let index in array){ let value = array[index]; //value equals 1 } |
Using “for of”:
1 2 3 |
for (let value of array){ //value equals 1 } |
This looks more elegant to me, but you be the judge.
For details check out w3schools.
String includes() Method
If you are like me and hate the sight of using .search() or regex to check if a string includes some text then you need this in your life.
Example
Considering the following string:
1 |
var myString = "The quarantine is a hoax designed by the government to change the batteries in all the birds"; |
Using search method:
1 |
var includesBirds = myString.search("birds") != -1; //true |
Using includes method:
1 |
var includesBirds = myString.includes("birds"); //true |
So your code would end up looking like this:
1 2 3 |
var sendFBI = myString.includes("birds") && myString.includes("government") && myString.includes("batteries"); //true |
Holy molly, this was long overdue. I love this and will be using it very oftenly.
For details check out w3schools.
Array includes() Method
This is the same thing as the previous example with String includes() method. I find it easier and more elegant than using Array indexOf.
Example
Considering the following string:
1 |
var myArray = ["banana", "apple", "illuminati", "watermelon"]; |
Using indexOf method:
1 |
var includesIlluminati = myArray.indexOf("illuminati") != -1; //true |
Using includes method:
1 |
var includesIlluminati = myArray.includes("illuminati"); //true |
For details check out w3schools.
Array every() Method
This method is useful for validating if all of the elements of the array meet certain condition, if one of them doesn’t meet the condition then it returns false.
Example
1 2 3 |
var guestAges = [17,18,21,22,24]; var allAdults = guestAges.every( function (age){ return age >= 18; } ); //allAdults equals false because one of the guest ages is under 18 |
For details check out w3schools.
Set Object
The set object is a list of unique values of any type. This is very useful for removing duplicates from an Array.
Example
1 2 3 |
let myArray = [1,2,3,4,4,4,4,4,4,4,4,4,4,4,4,4,5]; let mySet = new Set(myArray); // mySet now equals {1, 2, 3, 4, 5} let deDuplicatedArray = Array.from(mySet); //deDuplicatedArray now equals [1,2,3,4,5] |
Here’s another example
1 2 3 4 5 6 7 8 9 |
let animals = new Set(); animals.add('pig'); animals.add('bear'); animals.add('cat'); animals.add('dog'); console.log(animals.size); // 4 animals.add('bear'); // Adding a duplicate console.log(animals.size); // Still 4! |
It has several methods you can use like: add, size, has, forEach, delete and clear so it makes a powerful tool by itself. Sets also outperforms Arrays so that’s another reason to love it.
For details check out Alligator.io and Mozilla Docs.
Spread syntax
This is basically used to expand an array or compact several parameters into an array.
Examples
Useful for concatenating two arrays like:
1 2 3 |
let array1 = ["bears", "beets"]; let array2 = ["battlestar galactica", ...array1 ]; //array2 equals ["bears","beets","battlestar galactica"] |
Or making copies of arrays like:
1 2 3 4 5 6 7 8 |
let array1 = [1, 2, 3]; let array1Copy = array1; //This is a shallow copy let array1CopySpread = [...array1 ]; array1Copy.push(4); //Pushing to the shallow copy //array1 equals [1,2,3,4] //array1Copy equals [1,2,3,4] //array1CopySpread equals [1,2,3] //The copy using spread remains unmodified |
For details and more uses check out Scotch.io and Mozilla Docs.
1 2 3 4 5 6 |
var myNumber = 23; var myString = `text line 1 text line 2 text with "double quotes" and 'single quotes' text with variable value of ${myNumber}`; |
1 2 3 4 |
text line 1 text line 2 text with "double quotes" and 'single quotes' text with variable value of 23 |
Arrow functions
These functions work the same as regular functions but have a more compact syntax.
Example
Regular function:
1 2 3 |
function hello() { return "Hello World!"; } |
Arrow function:
1 2 3 |
hello = () => { return "Hello World!"; } |
And it can get even shorter:
1
hello = () => "Hello World!";
Anonymous arrow function with params:
1
((name)=> `Hello ${name}!`)('stranger'); //"Hello stranger!"
For details and more uses check out w3schools.
Serverside promises
Promises are now supported serverside so you can now write asynchronous code.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
https.get.promise({ url: 'https://jsonplaceholder.typicode.com/todos/1' }) .then(function(response){ log.debug({ title: 'Response', details: response }); }) .catch(function onRejected(reason) { log.debug({ title: 'Invalid Get Request: ', details: reason }); }) |
You can also create your own Promises now like so:
1 2 3 4 5 6 |
let promise = new Promise((resolve) => { let response = https.get({url: 'https://jsonplaceholder.typicode.com/todos/1'}); resolve(response.body); }); promise.then(log.debug); //This calls log.debug and uses whatever data is returned as parameter |
For details and more uses check out Mozilla Docs.
Async functions (Async/Await)
Async functions provide a way to structure and simplify your asynchronous code. I find this very useful to avoid callback hell that comes with Promises as it can get pretty complex, I have seen some code where there’s an unending statements of .then.then.then….. in a row.
Example
1 2 3 4 5 6 7 8 9 10 |
async function asyncRequest(){ //<< Note the async keyword next to the function let promise = new Promise((resolve) => { let response = https.get({url: 'https://jsonplaceholder.typicode.com/todos/1'}); resolve(response.body); }); //promise.then(log.debug); //No longer using this let responseBody = await promise; //Using await instead log.debug('responseBody', responseBody); //Logs correctly } |
For details and more uses check out Mozilla Docs.
So what do you think? Do you agree with our list? Share your comments below.