the following code sample is to change the item status field in the sales order with a map Reduce script that can be scheduled to update the status of the item based on the inventory level
define([‘N/record’, ‘N/search’],
/**
* @param{record} record
* @param{search} search
*/
(record, search) => {
/**
* Defines the function that is executed at the beginning of the map/reduce process and generates the input data.
* @param {Object} inputContext
* @param {boolean} inputContext.isRestarted – Indicates whether the current invocation of this function is the first
* invocation (if true, the current invocation is not the first invocation and this function has been restarted)
* @param {Object} inputContext.ObjectRef – Object that references the input data
* @typedef {Object} ObjectRef
* @property {string|number} ObjectRef.id – Internal ID of the record instance that contains the input data
* @property {string} ObjectRef.type – Type of the record instance that contains the input data
* @returns {Array|Object|Search|ObjectRef|File|Query} The input data to use in the map/reduce process
* @since 2015.2
*/
const getInputData = (inputContext) => {
try {
let itemSearchObj = search.create({
type: “item”,
filters:
[
[[[“formulanumeric: CASE WHEN TO_CHAR({inventorylocation}) = ‘a. Product Distribution Center’ THEN 1 ELSE 0 END”, “equalto”, “1”]], “AND”, [[[“custitem_aha_item_status”, “anyof”, “1”], “AND”, [“locationquantityavailable”, “lessthanorequalto”, “2”]], “OR”, [[“custitem_aha_item_status”, “anyof”, “2”], “AND”, [“locationquantityavailable”, “greaterthan”, “2”]]]],
],
columns:
[
search.createColumn({
name: “internalid”,
summary: “GROUP”,
label: “Internal ID”
}),
search.createColumn({
name: “itemid”,
summary: “GROUP”,
label: “Item Name”
}),
search.createColumn({
name: “custitem_aha_item_status”,
summary: “GROUP”,
label: “Item Status”
}),
search.createColumn({
name: “inventorylocation”,
summary: “GROUP”,
label: “Inventory Location”
}),
search.createColumn({
name: “locationquantityavailable”,
summary: “MAX”,
label: “Location Available”
})
]
});
let searchResultCount = itemSearchObj.runPaged().count;
log.debug(“itemSearchObj result count”, searchResultCount);
itemSearchObj.run().each(function (result) {
return true;
});
log.debug(“searchresults”, itemSearchObj);
return itemSearchObj;
}
catch (error) {
log.error({ title: ‘Error in reduce function’, details: error });
}
}
/**
* Defines the function that is executed when the reduce entry point is triggered. This entry point is triggered
* automatically when the associated map stage is complete. This function is applied to each group in the provided context.
* @param {Object} reduceContext – Data collection containing the groups to process in the reduce stage. This parameter is
* provided automatically based on the results of the map stage.
* @param {Iterator} reduceContext.errors – Serialized errors that were thrown during previous attempts to execute the
* reduce function on the current group
* @param {number} reduceContext.executionNo – Number of times the reduce function has been executed on the current group
* @param {boolean} reduceContext.isRestarted – Indicates whether the current invocation of this function is the first
* invocation (if true, the current invocation is not the first invocation and this function has been restarted)
* @param {string} reduceContext.key – Key to be processed during the reduce stage
* @param {List<String>} reduceContext.values – All values associated with a unique key that was passed to the reduce stage
* for processing
* @since 2015.2
*/
const reduce = (reduceContext) => {
try {
let searchResults = JSON.parse(reduceContext.values);
log.debug(“Reduce Result”, searchResults);
let quantity = searchResults.values[‘MAX(locationquantityavailable)’];
let status = 1;
if (quantity <= 2) {
status = 2;
}
record.submitFields({
type: ‘inventoryitem’,
id: searchResults.values[“GROUP(internalid)”][‘value’],
values: {
custitem_aha_item_status: status // B status
}
});
} catch (error) {
log.error({ title: ‘Error in reduce function’, details: error });
}
}
return { getInputData, reduce }
});