Using SuiteScript 2.x to Create a Subrecord in a Sublist Field
A sublist line can include a field that references a subrecord, depending on the record type and other factors. Often, you need to add the subrecord when creating the sublist line. In other cases, you can add the subrecord later.
To create 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:
Subrecords can also be in a record's body field. For details on working with subrecords when they occur in body fields, see Scripting Subrecords that Occur in Body Fields. For an overview of the difference between these two types of placement, see Body Field Subrecords and Sublist Subrecords.
For more details about the methods referenced in this topic, see Record Object Members.
Creating a Sublist Subrecord in Dynamic Mode
If your script uses dynamic mode, you can use the following procedure to create a subrecord in a sublist field.
To learn about SuiteScript scripting modes, see SuiteScript 2.x Standard and Dynamic Modes.
To create a sublist subrecord in dynamic mode:
-
Create a new record and set its required fields, or load an existing record to update it.
-
Do one of the following:
-
If you want to create a new sublist line, create the line using the Record.selectNewLine(options) method. Set any required values on the sublist line with the Record.setCurrentSublistValue(options) method.
-
If you want to add a new subrecord to an existing sublist line, identify that line using the Record.selectLine(options) method.
-
-
Create the new subrecord using the Record.getCurrentSublistSubrecord(options) method. The method requires two arguments:
-
A sublistId, which identifies the sublist.
-
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, you could use an expression like the following to create an order schedule subrecord on an item sublist:
... var orderScheduleSubrecord = blanketPurchaseOrder.getCurrentSublistSubrecord({ sublistId: 'item', fieldId: 'orderschedule' }); ...
-
-
Use Record.setValue(options) method to set body fields on the subrecord as needed. For example, on an order schedule subrecord, you could use the following expression to set a value for the Create Purchase Orders select field.
... orderScheduleSubrecord.setValue({ fieldId: 'createpurchaseorder', value: 'LEAD' }); ...
Note that not all subrecords have body fields that can be edited.
-
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) method.
-
Set required values on the line with the Record.setCurrentSublistValue(options) method.
-
Save the subrecord’s sublist line with the Record.commitLine(options) method.
For example, if you create an order schedule subrecord, you could use the following expressions to create a line on the subrecord’s schedule sublist:
... orderScheduleSubrecord.selectNewLine sublistId: 'schedule', }); orderScheduleSubrecord.setCurrentSublistValue({ sublistId: 'schedule', fieldId: 'quantity', value: 1 }); orderScheduleSubrecord.setCurrentSublistValue({ sublistId: 'schedule', fieldId: 'trandate', value: dateVariable }); orderScheduleSubrecord.commitLine({ sublistId: 'schedule' }); ...
-
-
Save the sublist line that holds the subrecord with the Record.commitLine(options) method.
-
Save the record with the Record.save(options) method.
For a full script examples, see Creating an Inventory Detail Sublist Subrecord Example and Creating an Address Sublist Subrecord Example.
Creating a Sublist Subrecord in Standard Mode
If your script uses standard mode, you can use the following procedure to create a subrecord in a sublist field.
To learn about SuiteScript scripting modes, see SuiteScript 2.x Standard and Dynamic Modes
To create a sublist subrecord in standard mode:
-
To add a new record, create it and set its required body fields. To update an existing record, load it.
-
If you want to create a new sublist line, create the line using the Record.insertLine(options) method. Set any required fields on the sublist line.
-
Create the new subrecord with the Record.getSublistSubrecord(options) method. This method takes three arguments:
-
A sublistId, which identifies the sublist.
-
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.
For example, you could use an expression like the following to create an inventory detail subrecord on the first line in an item sublist:
... inventoryDetailSubrecord = rec.getSublistSubrecord({ sublistId: 'item', fieldId: 'inventorydetail', line: 0 }); ...
-
-
Use the Record.setValue(options) method to set body fields on the subrecord. 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:
-
Use the Record.insertLine(options) method to create the line.
-
Set values on the line with the Record.setSublistValue(options) method.
For example, if you were creating an order schedule subrecord, you could use the following expressions to create a line on the subrecord’s schedule sublist:
... subrecordInvDetail.setSublistValue({ sublistId: 'inventoryassignment', fieldId: 'receiptinventorynumber', line: 0, value: '012345' }); ...
-
-
Save the record using the Record.save(options) method.
For a full script example of creating a sublist subrecord in standard mode, see Creating a Landed Cost Sublist Subrecord Example.