Create Multiple Sales Records Using a Map Reduce Script
The following sample shows how to use a map reduce script to create multiple sales records and log the record creation progress.
Note:
This script sample uses the define
function, which is required for an entry point script (a script you attach to a script record and deploy). You must use the require
function if you want to copy the script into the SuiteScript Debugger and test it. For more information, see SuiteScript 2.x Global Objects.
/**
* @NScriptType MapReduceScript
* @NApiVersion 2.0
*/
define(['N/runtime', 'N/record', 'N/log'], function(runtime, record, log) {
function getInputData(context) {
var orderCount = [1,2,3];
return orderCount;
}
function map (orderCount) {
log.debug('Order Count:', orderCount);
var rec;
rec = record.create({
type: 'salesorder',
isDynamic: true,
});
rec.setValue({
fieldId: 'entity',
value: 2135
})
rec.selectNewLine({
sublistId: 'item'
});
rec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'item',
value: 102
});
rec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity',
value: 3
});
rec.commitLine({
sublistId: 'item'
});
rec.save();
log.debug({
title: "record details",
details: rec,
});
var percentComplete = (orderCount.value * 100)/3;
log.debug({
title: 'New Sales Orders',
details: 'Record creation progress: ' + percentComplete + '%'
});
}
function summarize(summary) {
var type = summary.toString();
log.audit({title: type + ' Usage Consumed ', details: summary.usage});
log.audit({title: type + ' Concurrency Number ', details: summary.concurrency});
log.audit({title: type + ' Number of Yields ', details: summary.yields});
}
return {
getInputData: getInputData,
map: map,
// reduce: reduce,
summarize: summarize
};
})