Understanding Map / Reduce Script - NetSuite
- Kevin B.
- Sep 17, 2023
- 3 min read

This article is written to help further explain data flow in a map-reduce script. A map reduce script is a component of the SuiteScript ecosystem. This type of script is designed to handle large amounts of data. When the script is executed the framework automatically initiates enough jobs or events to process all of the actions needed to complete data manipulation in each function of the script. Something to consider when writing a map-reduce script is the ability to access the amount of governance that becomes available when using this type of script. Governance pertains to the amount of room or access the script has to complete actions in the script. Without governance means that each step or action has to wait until there is room available in order to execute. These are concepts that are better explained later in the article.
Governance
As with all scripts, Netsuite imposes usage limits on map/reduce scripts. Governance rules fall into two types of categories: Hard Limits and Soft Limits.
Hard Limits - cause interruption of the current function invocation
Soft Limits - these limits never interrupt a function invocation, rather these limits cause a job to yield and its work to be rescheduled for later
The most common use of a map reduction is script where you want to process multiple records, and where your logic can be separated into lightweight segments. Alert: this type of script is not suited for a scenario where you want to enact a long, complex function for each part of your data set.
Structure of Map/Reduce Script
GetInputData() - Marks the beginning of the script execution. This is where all data is gathered to be processed in the map section of the script.
Map() - This executes when the map entry point is triggered. The logic is your map function is applied to each key/value pair that is provided by the getInputData stage. One key/value pair is processed per function invocation, then the function is invoked again for the next pair.
Reduce() - To apply processing to each key/value pair provided. In this stage, each key is unique, and each value is an array of values. This function can optionally pass data to the summarize stage.
Summarize() - To retrieve data about the script’s execution, and take any needed actions with the output of the reduce stage.
Get Input Data Function

Map Function

Reduce Function

Summarize Function

From my understanding, there are a few important things to consider when writing this script. The get input data function is where information should be acquired. It is important to gather information, run all searches, and set parameters to import to the script. My confusion took place when first writing this script was how the data transfers from the get input data function to the map or reduce function inside of the map/reduce script. The data transfers in the form of an object with key-value pairs. For instance, the list of customers that is searched in Netsuite produces a list of objects that have a key, which takes the form of a numerical value, and the value is the data of each customer. That value can be accessed by mapContenxt.value.
The map function is where all functionality must take place. The map function offers the capability to manipulate the data of each object or customer passed through the function. For this particular script, I was able to update the royalty rates of each customer that was connected to the school record. The entire script gathers each customer, inventory item, and assembly item, that is connected to the school record and updates each of the royalty rates that the user changes.
For this script, there was no use of the reduce function. Please refer to the definition listed above for more understanding of the performance power of the reduce function.
The summarize function is a tool offered by Netsuite for developers to monitor the activity and produce results of the performance of the function. In this script, summaryContext was used to print the statistics about the execution of a map/reduce script. The usage consumed and the Number of Yields was displayed in the log debug component offered by the NetSuite company.
The article was written to help further explain the concepts and methodologies of the map-reduce script. For better understanding please refer to the documentation linked below.
Comentarios