Vishal Chaudhari
12/15/2025, 7:43 AM/**
* @NApiVersion 2.1
* @NScriptType ClientScript
*/
define(["N/currentRecord"], (currentRecord) => {
function populatePoItems(soItems) {
if (!soItems || soItems.length === 0) {
setTimeout(() => alert("No items found or selected on the Sales Orders."), 10);
return;
}
var rec = currentRecord.get();
console.log("Received SO Items for Population:", JSON.stringify(soItems));
try {
// NEW: counter for taxdetailsreference suffix
var taxLineCounter = 1;
soItems.forEach(function (item, index) {
if (!item.itemid) {
console.warn("Skipping line with missing item ID:", item);
return;
}
console.log(
Processing Line ${index}: Item ID ${item.itemid}, SO ID (item.tranid) is: ${item.tranid}
);
if (rec.id || rec.isNew) {
rec.selectNewLine({ sublistId: "item" });
var quantity = item.quantity ? parseFloat(item.quantity) : 0;
var rate = item.rate ? parseFloat(item.rate) : 0;
var amount = item.amount ? parseFloat(item.amount) : 0;
rec.setCurrentSublistValue({
sublistId: "item",
fieldId: "item",
value: item.itemid,
ignoreFieldChange: true,
});
rec.setCurrentSublistValue({
sublistId: "item",
fieldId: "custcol_item_number",
value: item.custcol_item_number,
ignoreFieldChange: true,
});
rec.setCurrentSublistValue({
sublistId: "item",
fieldId: "quantity",
value: quantity,
ignoreFieldChange: true,
});
rec.setCurrentSublistValue({
sublistId: "item",
fieldId: "rate",
value: rate,
ignoreFieldChange: true,
});
rec.setCurrentSublistValue({
sublistId: "item",
fieldId: "amount",
value: amount,
ignoreFieldChange: true,
});
rec.setCurrentSublistValue({
sublistId: "item",
fieldId: "custcol1",
value: item.tranid,
ignoreFieldChange: true,
});
console.log(Set custcol1 (SO ID) to: ${item.tranid});
rec.setCurrentSublistValue({
sublistId: "item",
fieldId: "description",
value: item.description,
});
// NEW: dynamic taxdetailsreference per added item
var taxid = rec.id + "_" + taxLineCounter; // e.g. 12345_1, 12345_2, ...
console.log("taxid in populatePoItems:", taxid);
rec.setCurrentSublistValue({
sublistId: "item",
fieldId: "taxdetailsreference",
value: taxid,
});
// increment only when an item line is actually added
taxLineCounter++;
rec.commitLine({ sublistId: "item" });
}
});
setTimeout(() => {
alert(
soItems.length +
" item line(s) added successfully. Please Save the Purchase Order."
);
}, 10);
} catch (e) {
console.error("Fatal Error in populatePoItems:", e);
setTimeout(() => alert("An error occurred while adding items: " + e.message), 10);
}
}
function pageInit(context) {
window.handleSuiteletResponse = populatePoItems;
console.log(
"PO_Add_Items_CS loaded and window.handleSuiteletResponse exposed."
);
}
return {
pageInit: pageInit,
};
});Celigo AI
12/15/2025, 7:43 AMAI generated answer (using internal Slack threads):Based on the conversations, it seems that the issue you're facing is due to the synchronous nature of the
setCurrentSublistValue function in your client script. This function is asynchronous, which means it doesn't wait for the operation to complete before moving on to the next line of code. This can cause issues when you're trying to add a large number of line items to a Purchase Order, as the script doesn't wait for each line item to be added before moving on to the next one.
One way to optimize this process is to use the forceSyncSourcing option in the setCurrentSublistValue function. This option forces the function to complete before moving on to the next line of code, which can improve performance when adding a large number of line items.
Here's how you can modify your code to use `forceSyncSourcing`:
javascript
rec.setCurrentSublistValue({
sublistId: "item",
fieldId: "item",
value: item.itemid,
ignoreFieldChange: true,
forceSyncSourcing: true,
});
Another way to optimize this process is to use a different script type, such as a Suitelet or Scheduled script, which can handle larger operations more efficiently than a Client script. These script types run on the server side, which can handle larger data sets and more complex operations.
Remember to test your changes thoroughly to ensure they improve performance as expected.
******
Disclaimer:
This response was generated by an artificial intelligence assistant based on previous conversations from multiple Slack channels, and may contain inaccuracies or mistakes.
******Celigo AI
12/15/2025, 7:43 AMSlack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.862060547
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.859375
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.859313965
Celigo AI
12/15/2025, 7:43 AM