To add the PO link in every Inbound shipment item line in a custom field and will fetch the PO link with the corresponding PO document number that is already existing in the item line. We will use a checkbox as a flag here. Only if the checkbox is false we will consider the Inbound shipment. After processing each Inbound shipment we will change it to true
Solution:
- Created a custom sub list field in Inbound shipment record item line and named it as “PO link”
- The custom field “PO link” to store the link of the corresponding Purchase Order by using MapReduce Script
- Created the checkbox field named it as “PO INBOUND SHIPMENT”
- If the checkbox “PO INBOUND SHIPMENT” is false we will consider the Inbound shipment. After processing each Inbound shipment we will change it to true
/**
* @NApiVersion 2.1
* @NScriptType MapReduceScript
*/
define(['N/http', 'N/https', 'N/record', 'N/redirect', 'N/runtime', 'N/search', 'N/url'],
/**
* @param{http} http
* @param{https} https
* @param{record} record
* @param{redirect} redirect
* @param{runtime} runtime
* @param{search} search
* @param{url} url
*/
(http, https, record, redirect, runtime, search, url) => {
/**
* 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 Inbound_Result_Obj = [];
var inboundshipmentSearchObj = search.create({
type: "inboundshipment",
filters:
[
["custrecord1568","is","F"]
],
columns:
[
search.createColumn({name: "internalid", label: "Internal Id"}),
]
});
var searchPageRanges;
try {
searchPageRanges = inboundshipmentSearchObj.runPaged({
pageSize: 1000
});
} catch (err) {
return [];
}
if (searchPageRanges.pageRanges.length < 1)
return [];
var pageRangeLength = searchPageRanges.pageRanges.length;
for (var pageIndex = 0; pageIndex < pageRangeLength; pageIndex++)
searchPageRanges.fetch({
index: pageIndex
}).data.forEach(function (result) {
var id = result.getValue({
name: "internalid",
sort: search.Sort.ASC,
label: "Internal Id"
});
Inbound_Result_Obj.push({
id: id
})
});
return Inbound_Result_Obj;
} catch (err) {
log.debug('Error@GetInputData', err)
}
}
/**
* Defines the function that is executed when the map entry point is triggered. This entry point is triggered automatically
* when the associated getInputData stage is complete. This function is applied to each key-value pair in the provided
* context.
* @param {Object} mapContext - Data collection containing the key-value pairs to process in the map stage. This parameter
* is provided automatically based on the results of the getInputData stage.
* @param {Iterator} mapContext.errors - Serialized errors that were thrown during previous attempts to execute the map
* function on the current key-value pair
* @param {number} mapContext.executionNo - Number of times the map function has been executed on the current key-value
* pair
* @param {boolean} mapContext.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} mapContext.key - Key to be processed during the map stage
* @param {string} mapContext.value - Value to be processed during the map stage
* @since 2015.2
*/
const map = (mapContext) => {
}
/**
* 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 {
log.debug('In Reduce')
var data_obj = JSON.parse(reduceContext.values[0]);
var poLink = data_obj.purchaseOrder;
// load the inbound Shipment purchase order
var loadInbShipment = record.load({
type: record.Type.INBOUND_SHIPMENT,
id: data_obj.id,
//isDynamic: true
});
log.debug("loadInShipment", loadInbShipment)
// To get value of PO Inbound Shipment checkbox
var flagvalue = loadInbShipment.getValue({
fieldId: 'custrecord1568'
});
// Checkbox flag value is true or false
if (flagvalue == true) {
log.debug("loop condition true")
} else {
var itemLineCount = loadInbShipment.getLineCount({
sublistId: 'items'
});
for (var i = 0; i < itemLineCount; i++) {
var poDocumentNumber = loadInbShipment.getSublistValue({
sublistId: 'items',
fieldId: 'purchaseorder',
line: i
});
var s = loadInbShipment.setSublistValue({
sublistId: 'items',
fieldId: 'custrecordjj_po_link',
value: poDocumentNumber,
line: i
});
}
log.debug("PO Doc Num: ", poDocumentNumber);
log.debug("ccck ")
loadInbShipment.setValue({
fieldId: 'custrecord1568',
value: true
});
loadInbShipment.save();
}
}
catch (err) {
log.debug('Error@GetInputData', err)
}
}
/**
* 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 = (context) => {
log.debug("contextinsummarize", context);
}
return {
getInputData: getInputData,
reduce: reduce,
summarize: summarize
};
});