Using SuiteScript 2.x to Create a Body Field Subrecord
Some types of records have body fields that can reference subrecords. Scripting a subrecord that exists in a body field is slightly different from working with those that exist on sublists.
To create a subrecord, your script must use the N/record Module. The script can use either dynamic or standard mode. For details on each approach, see the following sections:
If you are scripting the shipping address or billing address on a transaction, see Scripting Transaction Shipping and Billing Addresses.
Creating a Body Field Subrecord in Dynamic Mode
If your script uses dynamic mode, you can use the following procedure to create a subrecord in a body field.
To learn about SuiteScript scripting modes, see SuiteScript 2.x Standard and Dynamic Modes
To create a body field subrecord in dynamic mode:
-
If you want to add a new record, create it and set the required body fields. If you want to update an existing record, load the record.
-
Create the subrecord with the Record.getSubrecord(options). This method takes one argument: A fieldId, which identifies the field on the record 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, you could use an expression like the following to create an inventory detail subrecord on an assembly build record:
... var inventoryDetailSubrecord = rec.getSubrecord({ fieldId: 'inventorydetail' }); ...
-
Set body fields on the subrecord with the Record.setValue(options). Be aware that not all subrecords have writable body fields.
-
If the subrecord has a sublist, generally you are required to add at least one line to the sublist. For each line, use the following guidelines:
-
Create the line with the Record.selectNewLine(options).
-
Set required values on the line with the Record.setCurrentSublistValue(options).
-
Save the subrecord’s sublist line with the Record.commitLine(options).
For example, if you were creating an inventory detail subrecord, you could use the following expression to create a line on the subrecord’s inventory assignment sublist:
... inventoryDetailSubrecord.selectNewLine({ sublistId: 'inventoryassignment', }); inventoryDetailSubrecord.setCurrentSublistValue({ sublistId: 'inventoryassignment', fieldId: 'receiptinventorynumber', value: '012345' }); inventoryDetailSubrecord.commitLine({ sublistId: 'inventoryassignment' }) ...
-
-
Save the record.
For full script example showing how to create a body field subrecord using dynamic mode, see Creating an Address on a Subsidiary Record Example and Creating an Inventory Detail Subrecord on a Body Field Example.
Creating a Body Field Subrecord in Standard Mode
If your script uses standard mode, use the following procedure to create a subrecord on the body field of a record.
To learn about SuiteScript scripting modes, see SuiteScript 2.x Standard and Dynamic Modes
To create a body field subrecord in standard mode:
-
If you are adding a new record, create it and set the required body fields. If you are updating an existing record, load the record.
-
Create the subrecord with the Record.getSubrecord(options) method. This method takes one argument: A fieldId, which identifies the body field on the record 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, you could use an expression like the following to create an address subrecord on a location record:
... var addressSubrecord = rec.getSubrecord({ fieldId: 'mainaddress' }); ...
-
Set body fields on the subrecord using the Record.setValue(options) method. Be aware that not all subrecords have writable body fields.
-
If the subrecord has a sublist, generally you are required to add at least one line to the sublist. For each line, use the following guidelines:
-
Create the line with the Record.insertLine(options) method.
-
Set required values on the line with the Record.setSublistValue(options) method.
For example, if you were creating an inventory detail subrecord, you could use the following expressions to create a line on the subrecord’s inventory assignment sublist:
... inventoryDetailSubrecord.insertLine({ sublistId: 'inventoryassignment', line: 0 }); inventoryDetailSubrecord.setSublistValue({ sublistId: 'inventoryassignment', fieldId: 'receiptinventorynumber', line: 0, value: '12345' }); ...
-
-
Save the record.