Subrecord Scripting Overview
Subrecords use many of the same methods as records, but you access and save them a little differently. The following topics explain how subrecords work:
When scripting with subrecords, follow these guidelines:
Read-Only Subrecord Access
You can use subrecords in both client and server scripts. But subrecords are read-only if you retrieve their parent records in these ways:
-
Through the context object provided to a client script, or through currentRecord.get().
-
Through the context object provided to a beforeLoad user event script.
For more details, see Supported Deployments for Subrecord Scripting.
Subrecord Summary Fields
To work with a subrecord, you need to know about the summary field that contains it. You'll also need to know if the summary field is on the parent record's body or on a sublist. You need both pieces of information to instantiate the subrecord. If it's a sublist field, you'll also need the sublist ID.
If you are not sure where the field is situated, you can review the record in the UI. You can also check the reference page for the record in the SuiteScript Records Browser. On the reference page for each record type, the Fields table lists all of the record type's body fields. The tables listed under the heading Sublists show sublist fields. For more details, see Finding Details About Parent Record Types.
Subrecord Field and Sublist IDs
Like records, all subrecords have required and optional fields. You need the field IDs to set values for these fields. For sublist fields, you'll also need the sublist ID. You can find this info in the SuiteScript Records Browser. The browser lists subrecord types alongside record types. For each subrecord type, a reference page includes the IDs for all of the elements on the subrecord, including a sublist, if one exists, and all of the subrecord's fields. For more details, see Finding Details About Subrecord Types.
Additionally, if you have the Show Internal IDs preference enabled, you can use the UI to find the IDs for subrecord body fields. To view a field's ID, click its label. This will open a popup with the ID. For help enabling this preference, see Setting the Internal ID Preference.
Record Methods for Subrecords
Scripting with subrecords is similar to scripting with records in many ways.
For example, when your script instantiates a subrecord, the system returns one of the same objects that it uses to represent records. These objects include record.Record and currentRecord.CurrentRecord.
You can use many of the same methods for subrecords as you do for records. For example:
-
In server scripts, use Record.setValue(options) to set values on record or subrecord body fields.
-
In a client script, use CurrentRecord.getValue(options) to retrieve the value stored in either a record or subrecord body field.
Some record methods and properties don't work with subrecords. These exceptions are noted in the property descriptions in the N/record Module and N/currentRecord Module topics.
Saving Subrecords Through Parent Records
After you have created or updated a subrecord, you do not explicitly save it. Rather, after you have set all required fields on the subrecord, you simply save the parent record. Saving the parent record also saves the subrecord.