Solution Design Tips: Minimizing Governance in SuiteScript

in , , December 2nd, 2024
a man is typing on a computer keyboard

When writing SuiteScripts, you have to take into consideration how much governance your script is going to use.

Scripts that do not use a lot of governance can be set to run on a user event script, which has a limit of 1,000 usage units. On the flip side, if your script is going to use a lot of governance, the function of the script may need to be offloaded to a map/reduce script, where 5,000 usage units per reduce instance can be used. A map/reduce script is inevitable for certain situations, but some solution designs can have their governance usage greatly reduced by using roundabout methods.

Want to learn more about the map/reduce script approach? Check out this article on how to quickly create a Map/Reduce Script to Do the Work of a User Event Script!

record.load Example (Usage 5 units)

Let us say that you have to read multiple item records' fields to get data to process.

You can run a for loop with a record.load, such as the code block below:

for (let i = 0; i < arrayOfItems.length; i++) {
  const itemRecord = record.load({
    type: record.Type.INVENTORY_ITEM,
    id: arrayOfItems[i]
  });
  itemRecord.getValue("sku");
}

Each record.load usage is 5 units for item records. If there are up to 200 item IDs that need to have data read from, this works fine in a user event script. If there are more than 200 item IDs, a scenario which surprisingly comes up often, this script may need to be offloaded to a map/reduce script.

Item Search .each Example (Usage 10 units)

However, if no values need to be set in each item record, if the script is not reading from any sublists or sub-records, and if the script is only getting body fields, the amount of governance can be fixed to 10 units, regardless of the number of item IDs needed. The governance can be fixed by running an item search by internal ID and using the field(s) you have to pull as result columns. Here is an example:

let filterArray = ["internalid", "anyof"];


for (let i = 0; i < arrayOfItems.length; i++) {filterArray.push(arrayOfItems[i])};


const itemSearch = search.create({
  type: search.Type.ITEM,
  filters: [
    filterArray
  ],
  columns: [
  search.createColumn({
    name: "sku"
  })]
});


itemSearch.run().each(function(result) {
  result.getValue("sku");
  return true;
});

Despite the code being about 12 lines longer, this code block uses less governance. The .each function of the search only uses 10 units, regardless of the number of results, while every other line of code does not use any governance.

Designing Similar Shortcuts in SuiteScript

Craftiness in coming up with such shortcuts in governance usage requires deep familiarity with the functions of SuiteScript. This familiarity will come with experience from writing a lot of code as well as reading and understanding other people’s code. One opportunity to gain more familiarity with SuiteScript is to browse our other SuiteScript blog articles, which are concise yet rich with best practices, code samples, and solutions created by our team!

Author: John Baylon


Got stuck on a step in this article?

We like to update our blogs and articles to make sure they help resolve any troubleshooting difficulties you are having. Sometimes, there is a related feature to enable or a field to fill out that we miss during the instructions. If this article didn't resolve the issue, please use the chat and let us know so that we can update this article!

Oracle NetSuite Alliance Partner & Commerce Partner

If you have general questions about NetSuite, SuiteCommerce or more specific questions about how our team can support your business as you implement these solutions, feel free to contact us anytime. Anchor Group is a certified Oracle NetSuite Alliance Partner and Commerce Partner equipped to handle all kinds of NetSuite and SuiteCommerce projects, large or small!

 
 

Want to keep learning?

Our team of NetSuite professionals has written articles on a wide variety of NetSuite topics, from SuiteCommerce tips, to recommended NetSuite solutions, to available support services, and more! 

Your cart