SuiteScript Optimization Tip: Prefer Inline Editing
Created: February 9, 2024
This is a sample chapter adapted from the "Basic Optimization with SuiteScript 2.1" Cookbook, part of the SuiteScript by Example series.
Loading and submitting a record is one of the most expensive tasks - in terms of both governance and time - that you can perform in SuiteScript It is crucial to understand that this is not your only option for editing existing records.
When you only need to edit the body fields of a record, you can leverage
inline editing
using the
record.submitFields
method
from the
N/record
module.
This has several advantages:
submitFields
uses fewer governance units thanload() + save()
.submitFields
will generally execute faster because there are likely to be fewer User Event Scripts and Workflows responding to theXEDIT
event it fires.submitFields
is much more concise than repeatedsetValue()
calls.
Turn this:
const rec = record.load({
type: record.Type.EMPLOYEE,
id: '123'
})
rec.setValue({
fieldId: 'firstname',
value: 'Eric'
})
rec.setValue({
fieldId: 'middlename',
value: 'T'
})
rec.setValue({
fieldId: 'lastname',
value: 'Grubaugh'
})
rec.setValue({
fieldId: 'title',
value: 'SuiteScript Strategist'
})
rec.save()
into this:
record.submitFields({
type: record.Type.EMPLOYEE,
id: '123',
values: {
firstname: 'Eric',
middlename: 'T',
lastname: 'Grubaugh',
title: 'NetSuite Developer Advisor'
}
})
submitFields
also contains some options that enable it to run faster as well:
enablesourcing
determines whether sourcing will be performed on the updated record. This defaults totrue
, so sourcing is on by default.ignoreMandatory
determines whether mandatory field validation is performed on the updated record. This defaults tofalse
, so mandatory field validation is performed by default.
You can speed up the submission process further by disabling both of these
options when it makes sense to do so. You pass them in an options
parameter,
like so:
record.submitFields({
type: record.Type.EMPLOYEE,
id: '123',
values: {
firstname: 'Eric',
middlename: 'T',
lastname: 'Grubaugh',
title: 'NetSuite Developer Advisor'
},
options: {
enablesourcing: false, // disable sourcing
ignoreMandatory: true // disable mandatory validation
}
})
There are a few limitations with record.submitFields
to be aware of:
- You cannot use
submitFields
to modify fields in a sublist. - You cannot use
submitFields
to modify fields on a subrecord. - Not all body fields support inline editing. See the
nlapiSubmitField
column of the Records Browser to know whether a specific field supports inline editing. If you use inline editing on a field that doesn't support it, NetSuite will still execute the function properly, but behind the scenes it will load and submit the entire record, thus utilizing more governance and time.
Note that submitFields
will fire an XEDIT
Event type,
whereas load
and save
will fire a VIEW
Event followed by an EDIT
Event. Keep this in mind if you need other Scripts and Workflows to respond
to your submitFields
call.