The client wants to create a scheduled script that will update the value in a custom field. The criteria are that all items in the sales order should have Quantity=Commited Quantity. If not we don’t want to consider the order.
/**
* @NApiVersion 2.1
* @NScriptType MapReduceScript
*/
/************************************************************************************************
* * Map/Reduce **
* * To Set Integration Status In Sales Order **
*
*
* **********************************************************************************************
*
* Author: Jobin and Jismi
*
* Date Created : 03 March 2023
*
* Created By: Athul Krishna, Jobin and Jismi
*
* Description : To Set Integration Status In Sales Order
*
* REVISION HISTORY
*
*
*
*
***********************************************************************************************/
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{
var salesorderSearchObj = search.create({
type: "salesorder",
filters:
[
["type","anyof","SalesOrd"],
"AND",
["custbodyjj_crtd_by_prtnr","anyof","2"],
"AND",
["custbodyintegrationstatus","anyof","@NONE@"],
"AND",
["mainline","is","T"],
"AND",
["internalid","anyof","417349"]
],
columns:
[
search.createColumn({name: "tranid", label: "Document Number"}),
search.createColumn({name: "internalid", label: "Internal ID"}),
search.createColumn({name: "custbodyintegrationstatus", label: "Integration Status"}),
search.createColumn({name: "custbodyjj_crtd_by_prtnr", label: "Created by (Partner)"})
]
});
var searchResultCount = salesorderSearchObj.runPaged().count;
log.debug("salesorderSearchObj result count",searchResultCount);
if(searchResultCount>0){
return salesorderSearchObj
}else{
return []
}
}
catch (e) {
log.debug("error@getInputData",e)
return []
}
}
/**
* 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 searchResult=JSON.parse(reduceContext.values)
//let searchResult=reduceContext.values.map(JSON.parse)
log.debug('searchResult',searchResult);
let currentRec=record.load({
type: searchResult.recordType,
id: searchResult.id
})
log.debug("CurrentRec",currentRec);
let itemLineCount=currentRec.getLineCount({
sublistId: 'item'
});
log.debug("itemLineCount",itemLineCount);
let flag=0
for(let i=0;i<itemLineCount;i++){
let item=currentRec.getSublistText({
sublistId: 'item',
fieldId: 'item',
line: i
})
let quantity=currentRec.getSublistValue({
sublistId: 'item',
fieldId: 'quantity',
line: i
});
let commitedQuantity=currentRec.getSublistValue({
sublistId: 'item',
fieldId: 'quantitycommitted',
line: i
});
//log.debug(item,commitedQuantity)
if(parseInt(commitedQuantity)>=0){
if(quantity!=commitedQuantity){
log.debug("Item",item)
log.debug("Quantity",quantity)
log.debug("Commited Quantity",commitedQuantity)
flag=flag+1
}
}
}
if(flag==0){
currentRec.setValue({
fieldId: 'custbodyintegrationstatus',
value: 1
})
currentRec.save({
ignoreMandatoryFields: true,
enableSourcing: true
})
}
log.debug("Flag",flag)
}
catch (e) {
log.debug("error@Reduce",e)
}
}
/**
* Defines the function that is executed when the summarize entry point is triggered. This entry point is triggered
* automatically when the associated reduce stage is complete. This function is applied to the entire result set.
* @param {Object} summaryContext - Statistics about the execution of a map/reduce script
* @param {number} summaryContext.concurrency - Maximum concurrency number when executing parallel tasks for the map/reduce
* script
* @param {Date} summaryContext.dateCreated - The date and time when the map/reduce script began running
* @param {boolean} summaryContext.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 {Iterator} summaryContext.output - Serialized keys and values that were saved as output during the reduce stage
* @param {number} summaryContext.seconds - Total seconds elapsed when running the map/reduce script
* @param {number} summaryContext.usage - Total number of governance usage units consumed when running the map/reduce
* script
* @param {number} summaryContext.yields - Total number of yields when running the map/reduce script
* @param {Object} summaryContext.inputSummary - Statistics about the input stage
* @param {Object} summaryContext.mapSummary - Statistics about the map stage
* @param {Object} summaryContext.reduceSummary - Statistics about the reduce stage
* @since 2015.2
*/
const summarize = (summaryContext) => {
}
return {getInputData, reduce, summarize}
});