Using SuiteScript 2.x to Edit a Subrecord that Occurs in a Sublist Field
Your script can edit a subrecord in a sublist field in some cases.
To edit a sublist subrecord, your script must use the N/record Module. You can use either dynamic or standard mode. For details, see the following sections:
To learn about SuiteScript scripting modes, see SuiteScript 2.x Standard and Dynamic Modes
Editing a Subrecord in Dynamic Mode
If your script uses dynamic mode, you can use the following procedure to edit a subrecord that occurs in a sublist field.
To edit a subrecord in dynamic mode:
-
Load the record.
-
Use the Record.selectLine(options) method to identify the sublist and line that contain the subrecord that you want to update.
-
Get the subrecord using the Record.getCurrentSublistSubrecord(options) method. The method requires two arguments:
-
A sublistId.
-
A fieldId, which identifies the field on the sublist that contains the subrecord. In the Records Browser, the field that holds the subrecord is always identified as a field of type summary.
For example, suppose you are working with an entity record, such as an employee or customer. You could use an expression like the following to load an address subrecord from the entity’s Address sublist:
... var addressSubrecord = rec.getCurrentSublistSubrecord({ sublistId: 'addressbook', fieldId: 'addressbookaddress' }); ...
-
-
Update the subrecord's body fields using the Record.setValue(options) method as needed. For example, you could use an expression like the following to update a value on the address subrecord:
... addressSubrecord.setValue({ fieldId: 'city', value: 'St. Petersburg' }); ...
However, note that some subrecords do not have writable body fields.
-
If the subrecord has a sublist you want to modify, follow these steps for each line you want to change:
-
Identify the line you want to change using the Record.selectLine(options) method.
-
For each value you want to change, use the Record.setCurrentSublistValue(options) method to identify the field and the new value.
-
Save your changes to the subrecord’s sublist line using the Record.commitLine(options) method.
-
-
Save the line that holds the subrecord with the Record.commitLine(options) method.
-
Save the record with the Record.save(options) method.
For a full script example that shows editing a sublist subrecord in dynamic mode, see Updating an Order Schedule Sublist Subrecord Example.
Editing a Subrecord in Standard Mode
If your script uses standard mode, you can use the following procedure to edit a subrecord that occurs in a sublist field.
To learn about SuiteScript scripting modes, see SuiteScript 2.x Standard and Dynamic Modes
To edit a subrecord in standard mode:
-
Load the record.
-
Retrieve the subrecord using the Record.getSublistSubrecord(options) method. This method takes three arguments:
-
A sublistId.
-
A fieldId, which identifies the field on the sublist that contains the subrecord. In the Records Browser, the field that holds the subrecord is always identified as a field of type summary.
-
A line number, which identifies the sublist line that contains the subrecord you want to change.
For example, you could use an expression like the following to load an inventory detail subrecord from an item sublist:
... inventoryDetailSubrecord = rec.getSublistSubrecord({ sublistId: 'item', fieldId: 'inventorydetail', line: 0 }); ...
-
-
Use the Record.setValue(options) method to update the subrecord's body fields. Be aware that not all subrecords have writable body fields.
-
If the subrecord has a sublist whose values you want to modify, use the Record.setSublistValue(options) method to update the appropriate value. The method requires four arguments:
-
A sublistId.
-
A fieldId, which identifies the field you want to change.
-
A line number, which identifies the sublist line you want to change.
-
The new value.
For example, if you were updating an inventory detail subrecord, you could use the following expression to update the serial number on the first line of the inventory assignment sublist:
... inventoryDetailSubrecord.setSublistValue({ sublistId: 'inventoryassignment', fieldId: 'receiptinventorynumber', line: 0, value: '56789' }); ...
-
-
Save the record with the save() method.
For a full script example showing how to modify a subrecord in standard mode, see Updating an Address Subrecord Example.