Hi! I want to create a script that updates line number in a custom field on sales orders. I tried myself and then with chatgpt but I get the same error all the time. It works after I re-edit and save the order, but not on the first save. I have tried the same approach but with afterSubmit and that doesn’t give me anything at all.
/**
◦ @NApiVersion 2.1
◦ @NScriptType UserEventScript
◦ @NModuleScope SameAccount
*/
define(["N/record"], function (record) {
function beforeSubmit(context) {
if (context.type === context.UserEventType.CREATE || context.type === context.UserEventType.EDIT) {
var newRecord = context.newRecord;
// Check if the record is a sales order
if (newRecord.type === record.Type.SALES_ORDER) {
var lineCount = newRecord.getLineCount({
sublistId: "item"
});
// Loop through each line in the "item" sublist
for (var i = 0; i < lineCount; i++) {
var lineNum = newRecord.getSublistValue({
sublistId: "item",
fieldId: "line",
line: i
});
// Set custcol_line_number = line number
newRecord.setSublistValue({
sublistId: "item",
fieldId: "custcol_line_number",
line: i,
value: lineNum
});
}
}
}
}
return {
beforeSubmit: beforeSubmit
};
});
What am I doing wrong?