Using SuiteScript 2.x to Edit a Subrecord that Occurs in a Sublist Field

In some cases, your script can make changes to a subrecord that occurs in a sublist field.

To edit a sublist subrecord, your script must use the N/record Module. The script 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:

  1. Load the record.

  2. Use the Record.selectLine(options) method to identify the sublist and line that contain the subrecord that you want to update.

  3. Retrieve the subrecord with the Record.getCurrentSublistSubrecord(options) method. This method takes 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'
    });
    ... 
    
                  
  4. As appropriate, update body fields on the subrecord with the Record.setValue(options) method. 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.

  5. If the subrecord has a sublist whose values you want to modify, use the following steps for each line you want to change:

    1. Identify the line you want to change with the Record.selectLine(options) method.

    2. For each value you want to change, use the Record.setCurrentSublistValue(options) method to identify the field and the new value.

    3. Save your changes to the subrecord’s sublist line with the Record.commitLine(options) method.

  6. Save the line that holds the subrecord with the Record.commitLine(options) method.

  7. Save the record with the Record.save(options) method.

Note:

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:

  1. Load the record.

  2. Retrieve the subrecord with 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
    });
    ... 
    
                  
  3. Update body fields on the subrecord with the Record.setValue(options) method. Be aware that not all subrecords have writable body fields.

  4. If the subrecord has a sublist whose values you want to modify, use the Record.setSublistValue(options) method to update the appropriate value. This method takes 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'
    });
    ... 
    
                  
  5. Save the record with the save() method.

Note:

For a full script example showing how to modify a subrecord in standard mode, see Updating an Address Subrecord Example.

Related Topics

Scripting Subrecords that Occur on Sublist Lines
Using SuiteScript 2.x to Create a Subrecord in a Sublist Field
Using SuiteScript 2.x to Retrieve a Sublist Subrecord

General Notices