Using this code in a clientside suitescript, it is...
# ask-stanley-ai
m
Using this code in a clientside suitescript, it is supposed to add a item fee for each item, but it is not adding all the lines if there are multiple items. What is wrong with my code?
let lineCount = scriptContext.currentRecord.getLineCount({
sublistId: "item",
});
for (let i = 0; i < lineCount; i++) {
const itemId = scriptContext.currentRecord.getSublistValue({
sublistId: "item",
fieldId: "item",
line: i,
});
if (Number(itemId) === restockFeeItemId || !itemId) {
console.log("Skipping line", i);
continue;
}
scriptContext.currentRecord.insertLine({
sublistId: "item",
line: i + 1,
});
scriptContext.currentRecord.setCurrentSublistValue({
sublistId: "item",
fieldId: "item",
value: restockFeeItemId,
});
scriptContext.currentRecord.setCurrentSublistValue({
sublistId: "item",
fieldId: "rate",
value: 0,
});
scriptContext.currentRecord.commitLine({
sublistId: "item",
});
lineCount = scriptContext.currentRecord.getLineCount({
sublistId: "item",
});
i++;
}
c
AI generated answer (using internal Slack threads):
The issue with your code is that you are modifying the sublist (adding lines) while iterating over it. This can cause unexpected behavior because the length of the sublist is changing during the loop execution. When you insert a line, the line count increases, and you also increment the counter
i
. This means that you are effectively skipping the next line in the sublist after each insert. To fix this, you should not increment
i
after inserting a line. The loop will naturally proceed to the next line with
i++
in the
for
loop declaration. Here is the corrected part of your code:
Copy code
javascript
for (let i = 0; i < lineCount; i++) {
  const itemId = scriptContext.currentRecord.getSublistValue({
    sublistId: "item",
    fieldId: "item",
    line: i,
  });

  if (Number(itemId) === restockFeeItemId || !itemId) {
    console.log("Skipping line", i);
    continue;
  }

  scriptContext.currentRecord.insertLine({
    sublistId: "item",
    line: i + 1,
  });

  scriptContext.currentRecord.setCurrentSublistValue({
    sublistId: "item",
    fieldId: "item",
    value: restockFeeItemId,
  });
  scriptContext.currentRecord.setCurrentSublistValue({
    sublistId: "item",
    fieldId: "rate",
    value: 0,
  });
  scriptContext.currentRecord.commitLine({
    sublistId: "item",
  });
  lineCount = scriptContext.currentRecord.getLineCount({
    sublistId: "item",
  });
  // Do not increment i here
}
Remember, when you are modifying a list while iterating over it, you need to be careful about how you manage your loop counter and the list length.
Slack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.865141869
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.864244878
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.863800704
Please provide your feedback on this answer.