Hi There
I have the following map reduce script:
/**
*@NApiVersion 2.x
*@NScriptType MapReduceScript
*/
define(["N/record", "N/search", "N/runtime"], function (
record,
search,
runtime
) {
function getInputData() {
try {
var scriptObj = runtime.getCurrentScript();
var recordId = scriptObj.getParameter("custscript_recordid");
var picked = scriptObj.getParameter("custscript_picked_status");
var packed = scriptObj.getParameter("custscript_packed_status");
var shipped = scriptObj.getParameter("custscript_shipped_status");
log.debug(
"statuses",
" picked: " + picked + " packed: " + packed + " shipped: " + shipped
);
var salesOrderid = recordId;
log.debug("recordid", salesOrderid);
var salesorderSearchObj = search.create({
type: "salesorder",
filters: [
["type", "anyof", "SalesOrd"],
"AND",
["internalid", "anyof", [salesOrderid]],
"AND",
["shipping", "is", "F"],
"AND",
["taxline", "is", "F"],
"AND",
["mainline", "is", "F"],
"AND",
[
[
"formulanumeric: {quantitypacked}-{quantitybilled}",
"greaterthan",
"0",
],
"OR",
[
"formulanumeric: {quantitypicked}-{quantitybilled}",
"greaterthan",
"0",
],
"OR",
[
"formulanumeric: {quantityshiprecv}-{quantitybilled}",
"greaterthan",
"0",
],
],
],
columns: [
search.createColumn({ name: "internalid", label: "Internal ID" }),
search.createColumn({ name: "source", label: "Source" }),
search.createColumn({ name: "salesrep", label: "Sales Rep" }),
search.createColumn({ name: "trandate", label: "Date" }),
search.createColumn({ name: "postingperiod", label: "Period" }),
search.createColumn({ name: "type", label: "Type" }),
search.createColumn({
name: "tranid",
sort: search.Sort.DESC,
label: "Document Number",
}),
search.createColumn({ name: "entity", label: "Name" }),
search.createColumn({ name: "account", label: "Account" }),
search.createColumn({ name: "memo", label: "Memo" }),
search.createColumn({ name: "amount", label: "Amount" }),
search.createColumn({ name: "item", label: "Item" }),
search.createColumn({ name: "rate", label: "Item Rate" }),
search.createColumn({ name: "fxrate", label: "Item Rate" }),
search.createColumn({ name: "line", label: "Line ID" }),
search.createColumn({ name: "quantity", label: "Quantity" }),
search.createColumn({
name: "quantitybilled",
label: "Quantity Billed",
}),
search.createColumn({
name: "quantitycommitted",
label: "Quantity Committed",
}),
search.createColumn({
name: "quantityshiprecv",
label: "Quantity Fulfilled/Received",
}),
search.createColumn({
name: "quantitypacked",
label: "Quantity Packed",
}),
search.createColumn({
name: "quantitypicked",
label: "Quantity Picked",
}),
search.createColumn({ name: "taxcode", label: "Tax Item" }),
search.createColumn({
name: "formulanumeric",
formula: "{quantitypacked}-{quantitybilled}",
label: "Qty_to_invoice",
}),
],
});
return salesorderSearchObj
} catch (error) {
log.error({
title: "get input data error",
details: error.message + " line id: " + error.lineNumber,
});
}
}
/**
* @param {MapReduceContext.map} context
*/
function map(context) {
try {
var scriptObj = runtime.getCurrentScript();
// Access the custom script parameters
var picked = scriptObj.getParameter("custscript_picked");
var packed = scriptObj.getParameter("custscript_packed");
var shipped = scriptObj.getParameter("custscript_shipped");
log.debug(
"statuses",
" picked: " + picked + " packed: " + packed + " shipped: " + shipped
);
var result = JSON.parse(context.value);
var resultObj = JSON.stringify(context.values);
log.debug("map-result:", JSON.stringify(result));
var internalId = result.id;
log.debug("internalId", internalId);
var lineId = result.values.line;
var itemRate = result.values.rate;
var soItem = result.values.item;
var description = result.values.memo;
var quantityPicked = result.values["quantitypicked"];
var quantityPacked = result.values["quantitypacked"];
var quantityBilled = result.values["quantitybilled"];
var quantityShipRecv = result.values["quantityshiprecv"];
try { // Calculate quantity based on checkboxes
var quantity = 0;
if (picked) {
quantity += quantityPicked;
log.debug("picked qty", quantity);
}
if (packed) {
quantity += quantityPacked;
log.debug("packed qty", quantity);
}
if (shipped) {
quantity += quantityShipRecv;
log.debug("shipped qty", quantity);
}
quantity -= quantityBilled;
} catch (error) {
log.error("error for picked, packed,shipped", error.message)
}
// var quantity = quantityPacked - quantityBilled;
var lineData = {
line: lineId,
item: soItem,
rate: itemRate,
quantity: quantity,
memo: description,
};
log.debug("lineData", lineData);
var lineDataString = JSON.stringify(lineData);
// Pass the JSON string as the value in context.write()
context.write({
key: internalId,
value: lineDataString,
});
log.debug("context-end of map", context);
} catch (error) {
log.error("map error", error.message + " lineID: " + error.lineNumber);
}
}
/**
* @param {MapReduceContext.reduce} context
*/
function reduce(context) {
try {
var mapData = context.values;
log.debug("mapData", mapData);
log.debug("internalID", context.key);
var invoiceRecord = record.transform({
fromType: record.Type.SALES_ORDER,
fromId: context.key,
toType: record.Type.INVOICE,
isDynamic: true,
});
var lineCount = invoiceRecord.getLineCount({ sublistId: "item" });
log.debug("lineCount", lineCount);
// Create an object to store line data using line IDs as keys
var lineDataMap = {};
// Iterate through the mapData to organize line data by line ID
mapData.forEach(function (value) {
var lineData = JSON.parse(value);
var lineId = lineData.line;
lineDataMap[lineId] = lineData;
});
// Iterate through the lines in the invoice record
for (var lineIndex = 0; lineIndex < lineCount; lineIndex++) {
// Get the line ID of the current line in the invoice record
var lineId = invoiceRecord.getSublistValue({
sublistId: "item",
fieldId: "line",
line: lineIndex,
});
// Check if the line ID is found in the lineDataMap
if (lineDataMap[lineId]) {
var lineData = lineDataMap[lineId];
// Set the updated values for the retained line
invoiceRecord.selectLine({ sublistId: "item", line: lineIndex });
invoiceRecord.setCurrentSublistValue({
sublistId: "item",
fieldId: "item",
value: lineData.item.value,
});
invoiceRecord.setCurrentSublistValue({
sublistId: "item",
fieldId: "quantity",
value: lineData.quantity,
});
invoiceRecord.setCurrentSublistValue({
sublistId: "item",
fieldId: "rate",
value: lineData.rate,
});
invoiceRecord.commitLine({ sublistId: "item" });
} else {
// Remove lines that are not found in the lineDataMap
invoiceRecord.removeLine({ sublistId: "item", line: lineIndex });
// Decrement lineCount as you are removing a line
lineCount--;
// Decrement lineIndex to correctly process the next line
lineIndex--;
}
}
// Save the updated invoice record
var updatedInvoiceId = invoiceRecord.save();
log.debug("invoiceRecord", "invoice record: " + updatedInvoiceId);
// Emit the updated invoiceRecord data as a key-value pair
context.write({
key: "InvoiceRecord_" + updatedInvoiceId,
value: JSON.stringify({
type: "InvoiceRecord",
data: {
/* Your invoiceRecord data */
},
}),
});
log.debug("reduce-context after write", context);
} catch (error) {
log.error("error-reduce", error.message + " lineId: " + error.lineNumber);
}
}
/**
* @param {MapReduceContext.summarize} context
*/
function summarize(summary) {}
return {
getInputData: getInputData,
map: map,
reduce: reduce,
summarize: summarize,
};
});
The script is supposed to take the picked, packed or shipped quantities and set these as line quantities on a transformed invoice.
This works correctly provided I 'hard code' the values in. Ie. If I make the quantity formula in map stage var quanttiy=quantityPacked-quantitybilled, that works as expected and uses this value of quantity to replace the quantity that appears on the transformed invoice natively
I want this to be dynamic however.
For example:
Under company>General Preferences>Custom Preferences
With those selections checked (packed and fulfilled), I want the quantity formula in the map stage to equal:
var quantity=(quantityPacked + quantityShipRecv)-quantityBilled
The issue is, I can't read the script parameters (the picked, packed or shipped variables) in the map stage.
How do I pass across these values from getInput to map without messing up the rest of the script (at the moment, the searchobj returned from the get input stage is correct)
This is the original code:
/**
*@NApiVersion 2.x
*@NScriptType MapReduceScript
*/
define(["N/record", "N/search", "N/runtime"], function (
record,
search,
runtime
) {
function getInputData() {
try {
var scriptObj = runtime.getCurrentScript();
var recordId = scriptObj.getParameter("custscript_recordid");
var salesOrderid = recordId;
log.debug("recordid", salesOrderid);
var salesorderSearchObj = search.create({
type: "salesorder",
filters: [
["type", "anyof", "SalesOrd"],
"AND",
["internalid", "anyof", [salesOrderid]],
"AND",
["shipping", "is", "F"],
"AND",
["taxline", "is", "F"],
"AND",
["mainline", "is", "F"],
"AND",
[
"formulanumeric: {quantitypacked}-{quantitybilled}",
"greaterthan",
"0",
],
],
columns: [
search.createColumn({ name: "internalid", label: "Internal ID" }),
search.createColumn({ name: "source", label: "Source" }),
search.createColumn({ name: "salesrep", label: "Sales Rep" }),
search.createColumn({ name: "trandate", label: "Date" }),
search.createColumn({ name: "postingperiod", label: "Period" }),
search.createColumn({ name: "type", label: "Type" }),
search.createColumn({
name: "tranid",
sort: search.Sort.DESC,
label: "Document Number",
}),
search.createColumn({ name: "entity", label: "Name" }),
search.createColumn({ name: "account", label: "Account" }),
search.createColumn({ name: "memo", label: "Memo" }),
search.createColumn({ name: "amount", label: "Amount" }),
search.createColumn({ name: "item", label: "Item" }),
search.createColumn({ name: "rate", label: "Item Rate" }),
search.createColumn({ name: "fxrate", label: "Item Rate" }),
search.createColumn({ name: "line", label: "Line ID" }),
search.createColumn({ name: "quantity", label: "Quantity" }),
search.createColumn({
name: "quantitybilled",
label: "Quantity Billed",
}),
search.createColumn({
name: "quantitycommitted",
label: "Quantity Committed",
}),
search.createColumn({
name: "quantityshiprecv",
label: "Quantity Fulfilled/Received",
}),
search.createColumn({
name: "quantitypacked",
label: "Quantity Packed",
}),
search.createColumn({
name: "quantitypicked",
label: "Quantity Picked",
}),
search.createColumn({ name: "taxcode", label: "Tax Item" }),
search.createColumn({
name: "formulanumeric",
formula: "{quantitypacked}-{quantitybilled}",
label: "Qty_to_invoice",
}),
],
});
return salesorderSearchObj;
} catch (error) {
log.error({
title: "get input data error",
details: error.message + " line id: " + error.lineNumber,
});
}
}
/**
* @param {MapReduceContext.map} context
*/
function map(context) {
try {
var result = JSON.parse(context.value);
var resultObj = JSON.stringify(context.values);
log.debug("map-result:", JSON.stringify(result));
var internalId = result.id;
log.debug("internalId", internalId);
var lineId = result.values.line;
var itemRate = result.values.rate;
var soItem = result.values.item;
var description = result.values.memo;
var quantityPacked = result.values["quantitypacked"];
var quantityBilled = result.values["quantitybilled"];
var quantity = quantityPacked - quantityBilled;
var lineData = {
line: lineId,
item: soItem,
rate: itemRate,
quantity: quantity,
memo: description,
};
log.debug("lineData", lineData);
var lineDataString = JSON.stringify(lineData);
// Pass the JSON string as the value in context.write()
context.write({
key: internalId,
value: lineDataString,
});
log.debug("context-end of map", context);
} catch (error) {
log.error("map error", error.message + " lineID: " + error.lineNumber);
}
}
/**
* @param {MapReduceContext.reduce} context
*/
function reduce(context) {
try {
var mapData = context.values;
log.debug("mapData", mapData);
log.debug("internalID", context.key);
var invoiceRecord = record.transform({
fromType: record.Type.SALES_ORDER,
fromId: context.key,
toType: record.Type.INVOICE,
isDynamic: true,
});
var lineCount = invoiceRecord.getLineCount({ sublistId: "item" });
log.debug("lineCount", lineCount);
// Create an object to store line data using line IDs as keys
var lineDataMap = {};
// Iterate through the mapData to organize line data by line ID
mapData.forEach(function (value) {
var lineData = JSON.parse(value);
var lineId = lineData.line;
lineDataMap[lineId] = lineData;
});
// Iterate through the lines in the invoice record
for (var lineIndex = 0; lineIndex < lineCount; lineIndex++) {
// Get the line ID of the current line in the invoice record
var lineId = invoiceRecord.getSublistValue({
sublistId: "item",
fieldId: "line",
line: lineIndex,
});
// Check if the line ID is found in the lineDataMap
if (lineDataMap[lineId]) {
var lineData = lineDataMap[lineId];
// Set the updated values for the retained line
invoiceRecord.selectLine({ sublistId: "item", line: lineIndex });
invoiceRecord.setCurrentSublistValue({
sublistId: "item",
fieldId: "item",