Hi
I have a suitelet where the user selects the lines they wish to credit from an invoice
Once the user selection is submitted, a map reduce script is triggered.
The map reduce script is supposed to credit only the lines that were selected on the suitelet
However. a credit is being created for the entire invoice.
For example, in the above screenshot, since #IN0001666, line no 1 is selected, a credit memo of line 1 should be created.
However the entire invoice is being credited.
I can't see in my script where the issue is:
/**
* @NApiVersion 2.x
* @NScriptType MapReduceScript
*/
define(["N/runtime", "N/search", "N/util", "N/record"], function (
runtime,
search,
util,
record
) {
function getInputData() {
var scriptObj = runtime.getCurrentScript();
var filtersParam = runtime
.getCurrentScript()
.getParameter({ name: "custscript_selected_lines_json" });
var lineIdToTransactionIdMap = JSON.parse(filtersParam);
log.debug("lineIdToTransactionIdMap", lineIdToTransactionIdMap);
var searchResults = [];
function convertToSuiteScriptFilter(lineIdToTransactionIdMap) {
var filter = []; // Initialize an empty array to hold the filter structure.
filter.push(["type", "anyof", "CustInvc"]); // Add condition for "CustInvc" type.
filter.push("AND");
var internalIds = Object.keys(lineIdToTransactionIdMap);
for (var i = 0; i < internalIds.length; i++) {
var internalId = internalIds[i];
var lineIds = lineIdToTransactionIdMap[internalId]; // Get the array of line IDs for the current internal ID.
var lineFilters = []; // Initialize an empty array to hold the line filters for the current internal ID.
// Construct line filters for each line ID.
for (var j = 0; j < lineIds.length; j++) {
if (j > 0) {
lineFilters.push("OR"); // Add "OR" operator between line filters.
}
lineFilters.push(["line", "equalto", lineIds[j]]); // Add a line filter to the lineFilters array.
}
// Add conditions for internal ID to the main filter array.
if (i > 0) {
filter.push("OR"); // Add "OR" operator between sets of conditions.
}
filter.push(["internalid", "anyof", internalId]); // Add condition for the current internal ID.
filter.push("AND"); // Connect the previous condition with "AND".
filter.push(lineFilters); // Add the line filters for the current internal ID.
}
return filter; // Return the constructed filter structure.
}
var dynamicFilter = convertToSuiteScriptFilter(lineIdToTransactionIdMap);
// log.debug("filter", filter);
// var keyValuePairs = lineIdToTransactionIdMap;
// Creating the filter
// var filters = createDynamicFilter(keyValuePairs);
log.debug("createdynamicfilters", dynamicFilter);
var isarray = util.isArray(dynamicFilter);
log.debug("isarray?", isarray);
try {
// Create the search using the filters
var invoiceSearchObj = search.create({
type: "invoice",
filters: dynamicFilter,
columns: [
search.createColumn({ name: "internalid", label: "Internal ID" }),
search.createColumn({ name: "trandate", label: "Date" }),
search.createColumn({ name: "entity", label: "Name" }),
search.createColumn({ name: "tranid", label: "Document Number" }),
search.createColumn({ name: "line", label: "Line" }),
search.createColumn({ name: "amount", label: "Amount" }),
],
});
var searchResultCount = invoiceSearchObj.runPaged().count;
log.debug("invoiceSearchObj result count", searchResultCount);
invoiceSearchObj.run().each(function (result) {
log.debug("resultSEt", result);
return true;
});
return invoiceSearchObj;
} catch (e) {
var errorMessage = e.message + " (Line: " + e.lineNumber + ")";
log.error({
title: "Error",
details: errorMessage,
});
}
}
// /**
// * @param {MapReduceContext.map} context
// */
function map(context) {
try {
var result = JSON.parse(context.value); // Get the result for the current context
log.debug("result", result);
var internalId = result.id; // Get the internal ID from the result
var lineId = result.values.line; // Get the line ID from the result. See debug log for 'result' to see how this structured
log.debug({
title: "internalId",
details: internalId,
});
log.debug({
title: "lineId",
details: lineId,
});
// Load the invoice record
var invoiceRecord = record.load({
type: record.Type.INVOICE,
id: internalId,
isDynamic: true,
});
log.debug("invoiceRecord", invoiceRecord);
// Create credit memo based on the invoice record
var creditMemo = record.transform({
fromType: "invoice",
fromId: internalId,
toType: "creditmemo",
isDynamic: true,
});
log.debug("creditMemo", creditMemo);
// Find the line index for the specified line ID
var lineIndex = -1;
for (
var i = 0;
i < invoiceRecord.getLineCount({ sublistId: "item" });
i++
) {
var lineItemId = invoiceRecord.getSublistValue({
sublistId: "item",
fieldId: "line",
line: i,
});
if (lineItemId === lineId) {
lineIndex = i;
break;
}
}
if (lineIndex !== -1) {
// Include the specified line in the credit memo
creditMemo.selectNewLine({
sublistId: "item",
});
// Copy fields from the invoice line to the credit memo line
// Example: Set item, description, amount, etc.
creditMemo.setCurrentSublistValue({
sublistId: "item",
fieldId: "item",
value: invoiceRecord.getSublistValue({
sublistId: "item",
fieldId: "item",
line: lineIndex,
}),
});
creditMemo.setCurrentSublistValue({
sublistId: "item",
fieldId: "quantity",
value: invoiceRecord.getSublistValue({
sublistId: "item",
fieldId: "quantity",
line: lineIndex,
}),
});
// ... (Set other fields on the credit memo line based on the invoice line)
creditMemo.commitLine({
sublistId: "item",
});
// Save the credit memo
var creditMemoId = creditMemo.save();
log.debug("creditMemoId", creditMemoId);
// Emit the result for the summarize function
context.write({
key: "creditMemoId",
value: creditMemoId,
});
}
} catch (error) {
var errorMessage = error.message + " (Line: " + error.lineNumber + ")";
log.error({
title: "Error",
details: errorMessage,
});
}
}
function reduce(context) {
var invoiceInternalId = context.key; // Get the invoice internal ID
var lineIds = [];
// Collect line IDs associated with this invoice
context.values.forEach(function (value) {
var lineIdArray = JSON.parse(value);
lineIds = lineIds.concat(lineIdArray);
});
// Load the invoice record
var invoiceRecord = record.load({
type: record.Type.INVOICE,
id: invoiceInternalId,
isDynamic: true,
});
log.debug("invoiceREcord", invoiceRecord);
// Get the total number of lines on the invoice
var totalLines = invoiceRecord.getLineCount({ sublistId: "item" });
// Create credit memo based on the invoice record
var creditMemo = record.transform({
fromType: record.Type.INVOICE,
fromId: invoiceInternalId,
toType: record.Type.CREDIT_MEMO,
isDynamic: true,
});
// Loop through each line of the invoice and exclude lines not returned
for (var line = 0; line < totalLines; line++) {
var lineId = invoiceRecord.getSublistValue({
sublistId: "item",
fieldId: "line",
line: line,
});
if (lineIds.includes(lineId)) {
// Include the line in the credit memo
creditMemo.selectNewLine({
sublistId: "item",
});
// Copy fields from the invoice line to the credit memo line
// Example: Set item, description, amount, etc.
creditMemo.setCurrentSublistValue({
sublistId: "item",
fieldId: "item",
value: invoiceRecord.getSublistValue({
sublistId: "item",
fieldId: "item",
line: line,
}),
});
creditMemo.setCurrentSublistValue({
sublistId: "item",
fieldId: "quantity",
value: invoiceRecord.getSublistValue({
sublistId: "item",
fieldId: "quantity",
line: line,
}),
});
creditMemo.setCurrentSublistValue({
sublistId: "item",
fieldId: "description",
value: invoiceRecord.getSublistValue({
sublistId: "item",
fieldId: "description",
line: line,
}),
});
creditMemo.setCurrentSublistValue({
sublistId: "item",
fieldId: "price",
value: invoiceRecord.getSublistValue({
sublistId: "item",
fieldId: "price",
line: line,
}),
});
creditMemo.setCurrentSublistValue({
sublistId: "item",
fieldId: "rate",
value: invoiceRecord.getSublistValue({
sublistId: "item",
fieldId: "rate",
line: line,
}),
});
creditMemo.setCurrentSublistValue({
sublistId: "item",
fieldId: "taxcode",
value: invoiceRecord.getSublistValue({
sublistId: "item",
fieldId: "taxcode",
line: line,
}),
});
// Set other fields on the credit memo line based on the invoice line
// ...
creditMemo.commitLine({
sublistId: "item",
});
}
}
// Save the credit memo
var creditMemoId = creditMemo.save();
// Log the created credit memo ID
log.debug("Created Credit Memo", "ID: " + creditMemoId);
}
function summarize(summary) {
// ... (Your existing summarize code)
}
return {
getInputData: getInputData,