Reputation: 47
I am processing multiple invoices and it's line level data using scheduled script.
Below is array of objects.
Var JEIDArray = [ { item: "663", amount: "3000.00", JE: 13026407, invId: "13020561" }, { item: "663", amount: "100.00", JE: 13026408, invId: "13020561" }, { item: "663", amount: "111.00", JE: 13026409, invId: "13026406" }, { item: "663", amount: "222.00", JE: 13026410, invId: "13026406" }, { item: "373", amount: "444.00", JE: 13026411, invId: "13026406" } ];
Here I am getting invoice ids redundant. So I have to load every invoice every time and then set line level values. How I can avoid loading same invoice multiple times, but set line level data on correct invoices? Please help!
Below is function to update invoice lines: function updateInvoice(JEIDArray) { try {
//var currentInvoiceId = null;
for (var r=0;r<JEIDArray.length;r++)
{
var getRecId=JEIDArray[r].invId;
log.debug("getRecId",getRecId);
//currentInvoiceId=getRecId;
var invoice = record.load({
type: record.Type.INVOICE,
id: getRecId,
isDynamic: false
})
// Get the number of invoice lines
var lineCount = invoice.getLineCount({
sublistId: 'item'
});
log.debug("lineCount", lineCount);
// Loop through each invoice line
for (var i = 0; i < lineCount; i++) {
var currentItemId = invoice.getSublistValue({
sublistId: 'item',
fieldId: 'item',
line: i
});
var currentItemAmount = invoice.getSublistValue({
sublistId: 'item',
fieldId: 'custcol_ay_cts_allocation_amount',
line: i
});
// Loop through each item in JEIDArray
var flag = false;
for (var j = 0; j < JEIDArray.length; j++) {
var jeItem = JEIDArray[j];
if (currentItemId == jeItem.item && currentItemAmount == jeItem.amount) {
//log.debug("Item and amount flag in JEIDArray: ", currentItemId, currentItemAmount);
flag = true;
invoice.setSublistValue({
sublistId: 'item',
fieldId: 'custcol_ay_cts_assoc_ic', // Replace with your desired custom field
line: i,
value: jeItem.JE
});
break;
}
}
if (!flag) {
//log.debug("Item or amount not flag in JEIDArray: ", currentItemId, currentItemAmount);
}
}
// Update the invoice if necessary
var invoiceId=invoice.save();
log.debug('Invoice Updated', `Invoice ID: ${invoiceId}`);
}
Upvotes: 0
Views: 35
Reputation: 8857
Group the data by invId
before iterating. There are a lot of ways to do this; personally I'd use reduce()
on the Array, like so:
JEIDArray.reduce((data, line) => {
data[line.invId] ??= []
data[line.invId].push(line)
return data
}, {})
This will result in an Object like
{
"13020561": [
{
"item": "663",
"amount": "3000.00",
"JE": 13026407,
"invId": "13020561"
},
{
"item": "663",
"amount": "100.00",
"JE": 13026408,
"invId": "13020561"
}
],
"13026406": [
{
"item": "663",
"amount": "111.00",
"JE": 13026409,
"invId": "13026406"
},
{
"item": "663",
"amount": "222.00",
"JE": 13026410,
"invId": "13026406"
},
{
"item": "373",
"amount": "444.00",
"JE": 13026411,
"invId": "13026406"
}
]
}
Once you have this Object, you can iterate through each Invoice via Object.keys()
. All of this assumes you are using SuiteScript 2.1.
Upvotes: 0