Subrecord Scripting in SuiteScript 2.x Compared With 1.0
Compared with SuiteScript 1.0, SuiteScript 2.x introduces the following changes in how you script subrecords:
A Single Method Lets You Create and Load Subrecords
In SuiteScript 1.0, there are separate APIs for creating and editing subrecords. For example, you'd use nlapiCreateSubrecord to create a subrecord and nlapiEditSubrecord to edit a subrecord.
In SuiteScript 2.x, the same method can create or edit a subrecord. These methods all have the word get in their names. For example, you use the getSubrecord() method to create or load a subrecord that exists on the body of a record. You use the getSublistSubrecord() or getCurrentSublistSubrecord() method to create or load a subrecord that exists on a sublist line.
When you use any of these methods, the system responds with the following logic:
-
If a subrecord already exists in that field, it gets loaded.
-
If not, a new subrecord is created. Then you can set values on the field. The subrecord is saved when you save the record.
Subrecords Do Not Have to Be Explicitly Saved
In SuiteScript 1.0, you had to explicitly save a subrecord before saving the record. However, in SuiteScript 2.x, after you create a subrecord or make changes to one, you are not required to explicitly save the subrecord (and no methods exist for that purpose). New subrecords are saved when you save the record. This also applies if you update an existing subrecord. Updates are saved when you save the record.
To Create Addresses, You Must Use Subrecord Methods
Before address subrecords existed, addresses were represented as body fields or sublist lines on records.
After address subrecords were introduced, SuiteScript 1.0 was updated to support two ways of working with addresses:
-
You can use subrecord APIs (the preferred method)
-
You can interact with addresses using the legacy approach of setting values for the address body and sublist fields that used to exist. This support was made possible by logic added to the system that read the values set in this manner and created an address subrecord on behalf of the 1.0 script. Because this support exists in 1.0, these deprecated fields are displayed in the SuiteScript Records Browser as available fields.
In SuiteScript 2.x, you must use subrecord methods to create an address. The system doesn't support the old address body and sublist fields. For that reason, to create, edit, or load an address in SuiteScript 2.x, you must instantiate the address subrecord by referencing the appropriate summary field.