Understanding Map/Reduce Script in NetSuite with Example

Map/Reduce scripts in NetSuite are powerful for handling large data sets by processing records in stages: getInputData, map, reduce, and optionally summarize. This script type provides better governance management and parallel processing compared to traditional scheduled scripts.

How It Works:

1. getInputData – Retrieves the input data (e.g., from a saved search).

2. map – Processes each input record individually.

3. reduce – Aggregates and processes groups of records with the same key.

4. summarize – Logs output or sends notifications after processing.

Example: Suppose you want to update all Sales Orders with a specific status to mark them as “Processed” using a custom field.

This script loads sales orders from a saved search, processes each in the map stage, and updates them in the reduce stage.

Map/Reduce scripts are ideal for bulk updates, data cleanups, and integrations, offering scalability and efficient processing in NetSuite.

define(['N/search', 'N/record'], function(search, record) {
  function getInputData() {
    return search.load({ id: 'customsearch_sales_orders_pending' });
  }


  function map(context) {
    var result = JSON.parse(context.value);
    context.write({ key: result.id, value: result.id });
  }


  function reduce(context) {
    var salesOrderId = context.key;
    var so = record.load({ type: 'salesorder', id: salesOrderId });
    so.setValue('custbody_processed', true);
    so.save();
  }


  return { getInputData, map, reduce };
});

Leave a comment

Your email address will not be published. Required fields are marked *