Task Link:
Scenario:
Upadate all the line level and body level locations in the Credit Memo record. The credit memo internal id is given as static values.
Solution:
/**
* @NApiVersion 2.1
* @NScriptType MapReduceScript
*/
/************************************************************************************
HGLPA - 227 Update Credit Memo body level location and line level location
************************************************************************************
*
* Author: Jobin & Jismi IT Services LLP
*
* Date Created : 29th-September-2022
*
* Description : Update Credit Memo body level location and line level location
* REVISION HISTORY
*
***********************************************************************************/
define(['N/record'],
/**
* @param{record} record
*/
(record) => {
/**
* 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
*/
/**
*
* The internal id of the credit memo record is given as array .
*/
const getInputData = (inputContext) => {
try {
let dataArray = ["453301", "453773", "453772", "453771", "453770", "453768", "453769", "453766", "453765", "453763", "453764", "453762",
"453326", "470414", "478523", "484597", "508857", "533350", "549447", "553556", "556788", "556789", "556790",
"582303", "582304", "585976", "585977", "592880", "606767", "606768", "632056", "659496", "667892", "673774",
"673775", "681099", "681100", "694828", "708603", "712771", "712780", "736309", "739861", "744830", "749914", "749915",
"815918", "764308", "767539", "767541", "777523", "777526", "777527", "781650", "800085", "804511", "804512", "809510", "813766",
"819988", "825374", "842526", "864567", "864568", "871326", "874483", "874487", "884301", "889689", "896989"];
return dataArray
} catch (err) {
log.debug("error@GetInputData", err)
}
}
/**
* 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 dataObj = reduceContext.values.map(JSON.parse)
//log.debug("dataObj",dataObj);
/**
* Get the internal of the credit memo record in the reduce context
*/
let creditMemoId = dataObj[0];
/**
* Loaded the the Credit Memo record
*/
let CreditMemoData = record.load({
type: record.Type.CREDIT_MEMO,
id: creditMemoId,
isDynamic: false
});
log.debug("CreditMemoData", CreditMemoData);
/**
* The body level location value is set to location field. The location GV has the internal id 4 in NetSuite
*/
let bodyLocation = CreditMemoData.setValue({
fieldId: 'location',
value: 4
})
/**
* Get the line count from the credit memo record. Since we have to set location in each line level.
*/
let lineCount = CreditMemoData.getLineCount({
sublistId: 'item'
});
/**
* The line level location value is set to line level location field. The location GV has the internal id 4 in NetSuite
*/
for (let i = 0; i < lineCount; i++) {
let lineLocation = CreditMemoData.setSublistValue({
sublistId: 'item',
fieldId: 'location',
value: 4,
line: i,
ignoreFieldChange: true
});
}
/**
* The Credit Memo record is saved after setting the values in body level and line level.
*/
let recordId = CreditMemoData.save({
enableSourcing: true,
ignoreMandatoryFields: true
});
log.debug("recordId", recordId)
} catch (err) {
log.debug("error@Reduce", err)
}
}
return {getInputData, reduce}
});