Map Reduce Script

Hi All,

Map/Reduce script is an alternative to scheduled script which can handle/process large amount of data.

It is best suited for scenarios where data can be divided into small and independent parts.
When script executed, a structured framework automatically created enough jobs to process
all of these parts.No need to manage process, all jobs can be work in parallel.
Same way as scheduled script , map/reduce script can be triggered/invoked manually or
on a predefined schedule.

Advantage Over Schedule Script:
   Automatically create yield if script terminates or violates Netsuite governance, script automatically cause
the job to yield and it work to be rescheduled for later.

Disadvantage:
Map/reduce script is not well suited to situations where you want to write a long, complex function for each part of your data set. A complex series of steps might be one that includes the loading and saving of multiple records.

Map/Reduce Use Case:
 Map/reduce is ideal for scenarios where you want to apply the same logic repeatedly.
Example:
1. From list/array , transform each requisition into a purchase order.
2. Search transaction record and apply discount on each one
3. Search for customer record that appears to be duplicates ad then process each as per business rules.
4. search for cases and assigned to sales rep.
5. for each new order, create supporting document and upload it to external server.

Map/Reduce Script Execution Stages:
This script executed in five stages.Every stage occurs in a specific sequence.
User can control script's behaviors in stages.

1. getInputData stage:
Write a function that return a search of Netsuite Records.
Key/value pairs world be the result of search: each key would be internal id of record.
Ex:
function getInputData()
        {
            return search.create({
                type: record.Type.INVOICE,
                filters: [['memo', search.Operator.IS, ''], 'AND', ['mainline', search.Operator.IS, 'T']],
                columns: ['entity'],
                title: 'Empty Invoice memo Search'
            });
        }

2. map Stage: In this stage, get the values from getInputData and process as per need ,
Example: Get Internal id and values from search result of getInputData.
function map(context)
        {
            var searchResult = JSON.parse(context.value);
            var invoiceId = searchResult.id;
            var entityId = searchResult.values.entity.value;

            updateInvoice(invoiceId);

            context.write({
                key: entityId, 
                value: invoiceId
            });

        }
      
      
3. Reduce Stage: If you want to process data further, use Reduce Stage. Data from Map stage comes to Reduce in Key/Value pair.
Get data using key and value and add your business logic.
Example: If you want to approve SO and then Create FulFillment,
Or
If you want to create parent record and then create a child record for that.
function reduce(context)
        {
            var customerId = context.key;
            var cInvoiceId = context.value;
   
            var rec = record.create({
                    type: 'customrecord_mappingrecord',
                });

                rec.setValue({
                    fieldId : 'name',
                    value: customerId+' ' +cInvoiceId
                });

                rec.setValue({
                    fieldId: 'custrecord_invoice',
                    value: cInvoiceId
                });
                rec.setValue({
                    fieldId: 'custrecord_customerid',
                    value: customerId
                });
            var mappingRec = rec.save();

            context.write({
                key: mappingRec
            });
        }
**** Please note: Map and Reduce can be used both or any one of them.

4. Summarize Stage: This is optional, summarize function is invoked one time in the execution of the script.
This can be used to send email, notifications and even processing of data you get from above stages.
Please note summarize function invoked just once.

function summarize(summary)
        {
            var noOfProcessedRecord = 0;
             context.output.iterator().each(function () { noOfProcessedRecord++; });
             log.debug({ title: 'No of record processed', details: noOfProcessedRecord });
             sendFinalEmail(noOfProcessedRecord);
           
        }

**** Please Note:
All server side APIs are available for Map/Reduced script and supported in SuiteScript2.0

Example Code:
Please see Map/Reduced Script example here


Download Netsuite Guru Android App
Follow Me on Linkedin
Our FB Page

Comments