Client needs to change the lot numbers in Cash Sale records.
/**
* @NApiVersion 2.1
* @NScriptType MapReduceScript
*/
define(['N/record', 'N/search','N/file','/SuiteScripts/Jobin and Jismi/RLPL-129 Update Lot Number/papaparse.min.js'],
/**
* @param{record} record
* @param{search} search
*/
(record, search,file,papa) => {
/**
* 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 uploadFile = file.load({id:16344243});
var file_contents = uploadFile.getContents();
log.debug("file contents", file_contents);
var newValue = papa.parse(file_contents, {
header: true,
skipEmptyLines: true
});
log.debug("newValue", newValue)
return newValue.data.map(el => {
el.customRecInternalId = 1;
return el
});
}
catch(e){
log.debug("Error@getInputDate",e)
}
}
/**
* 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) => {
// }
function getIfRecord(document){
try{
var cashsaleSearchObj = search.create({
type: "cashsale",
filters:
[
["type","anyof","CashSale"],
"AND",
["numbertext","is",document],
"AND",
["mainline","is","T"]
],
columns:
[
search.createColumn({name: "tranid", label: "Document Number"}),
search.createColumn({name: "internalid", label: "Internal ID"})
]
});
var searchResultCount = cashsaleSearchObj.runPaged().count;
log.debug("cashsaleSearchObj result count",searchResultCount);
// cashsaleSearchObj.run().each(function(result){
// // .run().each has a limit of 4,000 results
// return true;
// });
let internalId="";
if(searchResultCount>0){
cashsaleSearchObj.run().each(function(result){
internalId=result.getValue({name: "internalid", label: "Internal ID"})
return true;
});
}
return internalId
}
catch(e){
log.debug("error@getIfRecord",e)
return false;
}
}
function getInventory(item,inventory){
try{
var inventorynumberSearchObj = search.create({
type: "inventorynumber",
filters:
[
["inventorynumber","is",inventory],
"AND",
["item","anyof",item]
],
columns:
[
search.createColumn({name: "internalid", label: "Internal ID"})
]
});
var searchResultCount = inventorynumberSearchObj.runPaged().count;
let internalId=''
log.debug("inventorynumberSearchObj result count",searchResultCount);
if(searchResultCount>0){
inventorynumberSearchObj.run().each(function(result){
internalId=result.getValue({name: "internalid", label: "Internal ID"})
return true;
});
}
return internalId;
}
catch(e){
log.debug("error@getInventory",e)
return false;
}
}
/**
* 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 result = JSON.parse(reduceContext.values);
//log.debug("result",result)
let ifRecord=result['Document Number'];
//log.debug("ifRecord",ifRecord);
if(ifRecord!="CSMAHO09049" || ifRecord!='CSMAHO08566'){
log.debug("ifRecord",ifRecord);
let internalId= getIfRecord(ifRecord);
let curRec= record.load({
type: 'cashsale',
id: internalId
});
log.debug("curRec",curRec);
let lineCount=curRec.getLineCount({
sublistId: 'item'
});
log.debug("lineCount",lineCount)
for(let i=0;i<lineCount;i++){
let inventoryDetails= curRec.getSublistValue({
sublistId: 'item',
fieldId: 'inventorydetailavail',
line: i
});
log.debug("inventoryDetails",inventoryDetails)
if(inventoryDetails){
log.debug("inventoryDetails",inventoryDetails);
let subRec= curRec.getSublistSubrecord({
sublistId: 'item',
fieldId: 'inventorydetail',
line: i
});
log.debug("subREc",subRec);
let subLine=subRec.getLineCount({
sublistId: 'inventoryassignment',
})
log.debug("sublistLne",subLine);
for(let j=0;j<subLine;j++){
let invent=subRec.getSublistValue({
// sublistId: 'inventoryassignment',
// fieldId: 'issueinventorynumber ',
// line: j
sublistId: 'inventoryassignment',
fieldId: 'issueinventorynumber',
line: j
});
if(!invent){
invent=subRec.getSublistValue({
// sublistId: 'inventoryassignment',
// fieldId: 'issueinventorynumber ',
// line: j
sublistId: 'inventoryassignment',
fieldId: 'receiptinventorynumber',
line: j
});
log.debug("invent",invent)
let oldNumber= result['Current Lot Number'];
let item=result['Item Internal ID'];
log.debug("oldNumber",oldNumber)
// oldNumber=getInventory(item,oldNumber);
// log.debug("oldNumber After",oldNumber)
if(invent==oldNumber){
log.debug("working");
let newNumber=result['Correct Lot Number'];
log.debug("newNumber",newNumber)
//newNumber=getInventory(item,newNumber);
// log.debug("New Number After",newNumber)
subRec.setSublistValue({
// sublistId: 'inventoryassignment',
// fieldId: 'issueinventorynumber ',
// line: j
sublistId: 'inventoryassignment',
fieldId: 'receiptinventorynumber',
value: newNumber,
line: j
});
}
}else{
log.debug("invent",invent)
let oldNumber= result['Old Lot Number'];
let item=3241;
log.debug("oldNumber",oldNumber)
oldNumber=getInventory(item,oldNumber);
log.debug("oldNumber After",oldNumber)
if(invent==oldNumber){
log.debug("working");
let newNumber=result['New Lot Number'];
log.debug("newNumber",newNumber)
newNumber=getInventory(item,newNumber);
log.debug("New Number After",newNumber)
subRec.setSublistValue({
// sublistId: 'inventoryassignment',
// fieldId: 'issueinventorynumber ',
// line: j
sublistId: 'inventoryassignment',
fieldId: 'issueinventorynumber',
value: newNumber,
line: j
});
}
}
}
//subRec.save();
}
}
curRec.save();
}
}
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}
});