Using SuiteScript 2.x to Create a Subrecord in a Sublist Field

Depending on the record type and other variables, a line on a record’s sublist can include a field that references a subrecord. In many cases, you must add the subrecord at the time you are creating the sublist line. In other cases, you can go back and add the subrecord later.

To create 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:

Subrecords can also occur in the body field of a record. 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.

Note:

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:

  1. 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.

  2. Do one of the following:

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

    Be aware that not all subrecords have writable body fields.

  5. 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 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'
    });
    ... 
    
                  
  6. Save the sublist line that holds the subrecord with the Record.commitLine(options) method.

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

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:

  1. 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.

  2. If you want to create a new sublist line, create the line with the Record.insertLine(options) method. Set any required fields on the sublist line.

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

  5. 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 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'
    });
    ... 
    
                  
  6. Save the record with the Record.save(options) method.

Note:

For a full script example of creating a sublist subrecord in standard mode, see Creating a Landed Cost Sublist Subrecord Example.

Related Topics

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

General Notices