Hi! I want to create a script that updates line nu...
# suitescript
j
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?
c
Do you know suitescript? I don’t think we should support ChatGPT generated code at all.
☝️ 1
e
Maybe not, however, what is wrong with this code? if you log.debug the value of lineNum in each iteration, what is it on create?
m
It’s before submit .. you don’t have line number before after record submit As such, you won’t be able to access the line number field in a beforeSubmit script, which is why your code is failing on the first save but works when re-saving the record.
👍 3