Quick Map Reduce Script to do the Work of a User Event Script 

in , , November 18th, 2024
person using grey laptop computer

Very often, when working with SuiteScript, a developer will be tasked to write a map-reduce script that updates all of the NetSuite records that their user event script is already updating. If this is the case, a minimal map/reduce script can be written to put all the work on the user event script. This method also has the benefit of decreasing the amount of code that may have to be reworked to just the user event script.

Setting Up the Map/Reduce Script

Step 1: Check that your user event script is set to trigger on the map/reduce context.

Step 2: In the getInputData stage of your map reduce, return a search for the NetSuite records you wish to update in the map reduce. Below is an example:

        return search.create({
            type: "commercecategory",
            filters: [],
            columns: [
                search.createColumn({
                    name: "internalid",
                    sort: search.Sort.ASC,
                    label: "Internal ID"
                })
            ]
        });

Step 3: In the reduce script, load and save your NetSuite record, triggering the user event script on record save. Use the reduce context’s key to get the NetSuite record’s internal ID, and do not forget to put everything around a try/catch block to catch errors that may occur. Here is an example of how the reduce code would look:

    function reduce(reduceContext) {
        try {
            record.load({
                type: record.Type.COMMERCE_CATEGORY,
                id: reduceContext.key
            }).save({ignoreMandatoryFields: true});
        }
        catch (e) {
            log.debug("reduce error", e);
        }
    }

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 questions about how our team can support your business as you implement NetSuite or SuiteCommerce, 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