Resources / Article
SuiteScript setValue in afterSubmit not working
Why record.setValue in a user event afterSubmit does not change the saved record, and how beforeSubmit or record.submitFields writes the value instead.
A setValue call in an afterSubmit user event does not change the saved record. The write has already reached the database by the time afterSubmit runs, so the script is reading saved data back for further processing (Oracle).
Why setValue in afterSubmit does not change the saved record
The write flow runs in a fixed order. A client sends a write request, from the UI, a web service, a CSV import or server SuiteScript. The application server checks permissions and runs validation during beforeSubmit, and nothing has been written yet at that point. After validation the data is stored. afterSubmit then pulls the saved data and hands it to the application server for further work (Oracle).
Oracle lists email notifications about new data, child records built from new data, and tasks assigned to employees among the jobs afterSubmit handles (Oracle). Each of those works on a record that already exists.
The deployment setting matters too. An asynchronous afterSubmit user event runs only during webstore checkout (Oracle). A deployment set that way will not fire when you save a record in the UI.
What afterSubmit is good for
afterSubmit is the entry point for work that needs a saved record. Oracle's sample creates a phone call for the sales rep when a new customer is created, reading the rep and the phone number from the saved customer (Oracle).
Move the update into beforeSubmit
- Open the script file and find the afterSubmit function.
- Move the setValue lines that target the same record into beforeSubmit.
- Use context.newRecord in both functions. During beforeSubmit the data has not been written yet (Oracle).
- Keep a trigger check so the logic runs only on the events you intend.
- Confirm the return block maps both entry points, as in
return { beforeSubmit: beforeSubmit, afterSubmit: afterSubmit };(Oracle). - Deploy, then test with the Script Debugger. Select Debug Existing, choose your user event script, save a record, and set breakpoints inside beforeSubmit (Oracle).
If the value is a default the user should see and edit on the form, set it in beforeLoad. Oracle's example sets phone and sales rep on a new customer in beforeLoad (Oracle).
Write to the saved record with record.submitFields
When the value needs something that exists only after the save, such as the record's internal id, write it from afterSubmit with record.submitFields. The method updates body fields on an existing record, given its type and internal id, and the record does not have to be loaded or submitted first (Oracle). It covers body fields that support inline editing. Sublist line items and subrecord fields such as addresses are outside it. Oracle lists its cost at 10 units for a transaction record, 2 for a custom record and 5 for any other record.
function afterSubmit(context) {
record.submitFields({
type: context.newRecord.type,
id: context.newRecord.id,
values: { custbody_approval_ref: 'VB-' + context.newRecord.id }
});
}
Use beforeSubmit when the value can be worked out from the data already on the record. Use record.submitFields when the value depends on the saved record.
Example: approval code on a vendor bill
A partner wants every new vendor bill above INR 5,00,000 to carry an internal approval marker, and wants a follow-up task for the finance lead. A bill of INR 7,45,000 is entered against a vendor.
The marker is set in beforeSubmit:
function beforeSubmit(context) {
var isNew = context.type === context.UserEventType.CREATE;
if (!isNew) {
return;
}
var bill = context.newRecord;
if (bill.getValue('total') > 500000) {
bill.setValue('custbody_approval_code', 'PENDING');
}
}
The task is created in afterSubmit, because it acts on a different record and needs the bill to exist:
function afterSubmit(context) {
var isNew = context.type === context.UserEventType.CREATE;
if (!isNew) {
return;
}
var bill = context.newRecord;
if (bill.getValue('custbody_approval_code') === 'PENDING') {
var task = record.create({ type: record.Type.TASK, isDynamic: true });
task.setValue('title', 'Approve vendor bill');
task.save();
}
}
In the first version of this script the setValue sat in afterSubmit. The bill saved with an empty approval code, and the task still appeared.
Writing to a record outside the user event
Oracle's afterSubmit sample builds a phone call record and saves it, which is the pattern for writing to a record other than the one being saved (Oracle). Load the target record and set the field on it, then call save:
var rec = record.load({ type: record.Type.VENDOR_BILL, id: billId });
rec.setValue('custbody_approval_code', 'APPROVED');
rec.save();
The record has been changed error
"How to avoid NetSuite record has been changed error?" (Stack Overflow)
The question comes up often. Walk the documented write flow first: permissions and validation run during beforeSubmit, the data is stored, and afterSubmit reads it back (Oracle). Check the deployment setting as well, because an asynchronous afterSubmit user event runs only during webstore checkout.
Trade-offs
beforeSubmit runs inside the write the user triggered, so the value is on the record as soon as it is written (Oracle). beforeLoad puts the value on the form before the user touches it, so a default can be edited before the save (Oracle). afterSubmit works on records other than the one being saved (Oracle).
Common mistakes
The first mistake is setting values in afterSubmit and expecting them to persist. The write has already happened, so the field stays empty on the saved record.
Two deployment details produce the same symptom. A missing return block leaves an entry point unregistered (Oracle), and an asynchronous afterSubmit deployment fires only during webstore checkout (Oracle).
FAQ
Does setValue in afterSubmit ever save?
No. The write has already happened by the time afterSubmit runs. Use beforeSubmit for the same record, or record.submitFields when the value needs the saved record's internal id.
Why is my afterSubmit not firing at all?
Check the deployment status and the context.type check. If the deployment is asynchronous, it fires only during webstore checkout (Oracle).
Can I use beforeLoad for the same value?
Yes, when the value is a default the user should see on the form. Oracle's example sets phone and sales rep on a new customer in beforeLoad (Oracle).
How do I see what the script is doing?
Use the Script Debugger. Select Debug Existing, choose the user event script, then save a record and set breakpoints inside beforeSubmit (Oracle).
The quick way is mark.: connect the client's account and it reads the script and its deployment, so you know where the setValue goes. Try it free.
Sources
- docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_1512409310.html
- docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_157141180023.html
- docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4387799721.html
- docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4440830813.html
- docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4698044232.html
- docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/subsect_1128020631.html
- docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/subsect_1128020728.html
Have mark. check this against the client's own files
mark. reads the client's files and sets each point against them, with the source beside it.
Twenty minutes on one client folder. Your 21-day trial opens on the call.