I am working on a NetSuite Purchase Order customiz...
# suitescript
v
I am working on a NetSuite Purchase Order customization. I have created a custom button on the Purchase Order. When the button is clicked, it displays all open Sales Orders. After selecting one or more Sales Orders and submitting, all line items from the selected Sales Orders are added to the Purchase Order using a Client Script. The issue is that when a Sales Order contains 90–100 line items, it takes a very long time to add those lines to the Purchase Order. How can I optimize this process to improve performance and reduce the time taken to add line items? Client Script
Copy code
/**
 * @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,
 };
});
b
Nothing suitescript will really make adding 100 lines bearable. forceSyncSourcing will make the script more consistent since you arent trying to commit lines before they are done sourcing.
b
Yeah this feels like a better use case for a suitelet / MR then the processing happens in the background and the user isn't waiting forever.