In part 1 and part 2, we covered the basics of the Map/Reduce script type. But we still haven’t covered everything that this powerful NetSuite tool has to offer. It’s time to explore the capabilities of advanced Map/Reduce script.
The Advanced Stages of Map/Reduce
One of the benefits of the Map/Reduce script is that it can handle data at either a simple level or an advanced level. The most basic version only needs the Get-Input-Data and the Map stages. A simple script suffices when all you need is a straightforward automation. But if you need something more complicated, the Map/Reduce script also makes provision for that.
Here’s a diagram that attempts to show the big picture of what we can do with the Map/Reduce script:
The Map Stage
In our previous post, we certainly did not use the Map/Reduce script type to its fullest potential. If we are looking to utilize advanced Map/Reduce script, we must begin with the unique position that the Map stage plays in the process.
If we need to perform an automation on organized data, we can use the Map stage to return a key/value pair for each result from a search. The Shuffle stage will then organize that data. Afterwards, we can perform our automation in the Reduce stage on those organized key/values pairs.
NetSuite describes the process within a code example in their help documentation:
After the map function has been invoked for the last time, the shuffle stage begins. In this stage, the system sorts the 35 key/value pairs that were saved by the map function during its two invocations. From those pairs, the shuffle stage creates a new set of key/value pairs, where the each key is unique. In this way, the number of key/value pairs is reduced to 25. For example, the map stage saved three instances of {‘e’,’1′}. In place of those pairs, the shuffle stage creates one pair: {‘e’, [‘1′,’1′,’1’]}. These pairs are made available to the reduce stage through the context.key and context.values properties.
So if we need to perform an automation on each result from the search in some way, we can just perform that automation in the Map stage. But if we want to handle the data once it’s all categorized by key, we can wait until the Reduce stage.
For example, if we needed to retrieve a certain list of records from a search and edit/save each one, then performing the primary actions within the Map stage would be fitting. But if we needed to retrieve a certain category of item on a record and wanted to organize them by that category in some way, we may want to wait until the Reduce function where our data, categorized by key, has multiple values (e.g., “{‘e’, [‘1′,’1′,’1’]}”).
The Reduce Stage
Once the Shuffler stage is complete (which NetSuite automatically handles between the Map and Reduce stages), we will have access to our key/values data.
But just like the Map stage, each key/values pair will individually and asynchronously feed into the Reduce stage. For each key/values pair, we can handle that data.
The Summarize Stage
The final stage is the Summary stage. This stage provides logging statistics about the success of the total operation. The stage commences once after all the other stages are complete. NetSuite will feed in summary context information about the automation. This parameter then results in access to detailed information about usage, errors, restart status, and more.
Conclusion
Although the Map/Reduce script type in its essence is quite simple, advanced Map/Reduce script provides a variety of options for handling data. Are you finding this series helpful? Let us know in the comment section! And don’t forget to subscribe to our email list to receive our latest tutorials.