Using SuiteScript 2.x to Create a Body Field Subrecord

Some records have body fields that reference subrecords. Scripting body field subrecords is a bit different from sublist subrecords.

To create a subrecord, your script must use the N/record Module. You can use either dynamic or standard mode. For details on each approach, see the following sections:

Important:

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:

  1. To add a new record, create it and set its required body fields. To update an existing record, load it.

  2. Create the subrecord using the Record.getSubrecord(options). The method requires one argument: A fieldId, which identifies the field on the record holding 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'
    });
    ... 
    
                  
  3. Use the Record.setValue(options) to set body fields on the subrecord. Be aware that not all subrecords have writable body fields.

  4. If the subrecord has a sublist, you usually need to add at least one line to the sublist. For each line, use the following guidelines:

    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'
    })
    ... 
    
                  
  5. Save the record.

Note:

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:

  1. If adding a new record, create it and set its required body fields. If updating an existing record, load it.

  2. Create the subrecord using the Record.getSubrecord(options) method. This method requires 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'
    });
    ... 
    
                  
  3. Use the Record.setValue(options) method to set body fields on the subrecord. Be aware that not all subrecords have writable body fields.

  4. 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:

    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'
    });
    ... 
    
                  
  5. Save the record.

Related Topics

General Notices