Search

Passing input data through Map/Reduce stages

In order to make multi-threading work for a “classless” programming language such as JavaScript, SuiteScript 2.0 Map/Reduce scripts employ a stringent way how data needs to be fed into map and reduce stages.


Further to NetSuite Help Centre Section Map/Reduce Entry Points (https://system.eu1.netsuite.com/app/help/helpcenter.nl?fid=section_4387799161.html#subsect_1518300127), I would like to share some key findings on our end:

  • Why not making it an object every time? Indeed, with Map/Reduce script, I have come to wrap up input parameters as objects whenever in doubt. Just wrap up all your data in an object objInput and pass it into the map stage: var objInput = {}; objInput[key] = {}; // Map will process these child objects on nested level 1 separately. Think carefully on how you want to split your data at this point. objInput[key][property1] = Property 1; objInput[key][property2] = Property 2; objInput[key][‘transdata’] = {}; objInput[key][‘transdata’][tranproperty1] = Transaction Property 1; objInput[key][‘transdata’][tranproperty2] = Transaction Property 2; return objInput;In the map stage, you can subsequently reconcile your data easily as follows: var keyMap = context.key; var objMapInput = JSON.parse(context.value); var objInput = {}; objInput[keyMap] = objMapInput; // If you feel likeLikewise, in order to push your data into the Reduce stage, you format your data in an object and push it into the Map/Reduce context object as follows: for ( var keyId in objRedInput ) { if ( !isEmptyObj(objRedInput) ) { context.write({ key: keyId, value: objRedInput[keyId] }); } }In the Reduce stage, you subsequently need to be careful for two reasons:

  1. The content of your input object objRedInput is stored in an array called context.values (not context.value).

  2. Presently, there appears to be a limitation wherein you cannot parse the entire array byvar objRedInput = JSON.parse(context.values);as this will return a value “L”, a reference to the underlying Java object.

Instead you can process your data as follows var keyRed = context.key; if ( context.values.length > 0 ) { var redInput = new Array; for ( var i = 0 ; i < context.values.length ; i++ ) { redInput.push(JSON.parse(context.values[i])); } } else { var redInput = JSON.parse(context.values[0]); } var objInput = {}; objInput[keyRed] = redInput; // If you feel like The key here is to loop through the array elements and parse them individually.

  • N/cache is said not to work well with Map/Reduce. I hope this will change so I don’t need to call on script deployment parameters several times in the map and reduce stage.

  • Generate output in Suitelets. This will allow to leverage on more functionality, including jQuery for formatting, which is not available in Map/Reduce. Currently, I am trying to introduce N/cache to park larger summarize input objects in the cache instead of passing it through the Suitelet deployment URL

1,460 views0 comments

Recent Posts

See All
Talk to our experts to start your Automation ERP journey. Click here