• : 1-877-596-8218
  • : sales@aecc.ca
# # #
#

Map/Reduce SuiteScript 2.0

In SuiteScript 1.0, the Scheduled Script is often used to do some scheduled work or bulk operations in the system such as bulk/periodic updating/creation records. SuiteScript 2.0 provides a more powerful way than the Scheduled script in API 1.0 with the Map/Reduce SuiteScript.
Map/Reduce Script is a server side script that is capable to handle a large number of records of data. Compare with the Schedule Script in SuiteScript 1.0, governance usage and yielding of a Map/Reduce Script is tracked automatically by the system, the Script can be re-scheduled or re-queued without manual intervention when the governance limit is exceeded.

The Basic structure of a Map/Reduce Script contains five parts, each part is called a stage.

Not like the Scheduled Script, the governance of a Map/Reduce script is calculated separately. The governances for each of the function is

Every time the Governance usage hit the limit, the Map/Reduce Script will be automatically yielded by the system. One can also set the yield time by setting the “Yield After Minutes” field on the Script Deployment page, the script will yield either hit the usage limit, or run out of the time limit.

Example
The following example shows a Map/Reduce Script to release Work Orders in the system, Work Orders are created with a “Planned” Status, this Map/Reduce Script will find all Work Orders in the system by calling a Saved Search, and change the status of each work order to “Released”.


/**
 * @NApiVersion 2.0
 * @NScriptType MapReduceScript
 */
var SAVED_SEARCH_ID = 'customsearch_find_next_week_open_wo';
define(['N/search', 'N/record', 'N/runtime', 'N/error'],
    function(search, record, runtime, error)
    {
        function getInputData(){
            log.debug('In Get Input data Stage');
            var mySearch = search.load({
                id: SAVED_SEARCH_ID
            });
            return mySearch;
        }

        function map(context)
        {
            log.debug('In Map Stage');
            //log.debug('context is ', JSON.stringify(context));
            var searchResult = JSON.parse(context.value);
            var WOID = searchResult.id;
            log.debug('WOID is '+WOID);
            //submit field, update the status of the WO to Released
            try {
                var id = record.submitFields({
                    type: record.Type.WORK_ORDER,
                    id: WOID,
                    values: {
                        orderstatus: 'B'
                    },
                    options: {
                        enableSourcing: false,
                        ignoreMandatoryFields: true
                    }
                });
            }catch(e){
               log.debug('WO '+ WOID, e.toString());
            }
            log.debug('WO '+ id +' is updated');
        }
        return {
            getInputData: getInputData,
            map: map
        };
    });

The script shows above only use the Get Input Data and Map functions, please note that when the data coming into the Map/Reduce Script is from a Saved Search, it is better not to do the Saved Search in the Get Input Data function, return a reference to the Saved Search in the Get Input Data function and the system will automatically do the rest for you.