The N/record module in SuiteScript 2.0 is used for working with records in NetSuite. It provides methods and properties that allow you to create, update, delete, retrieve, and search for records within your NetSuite account.
To get started with the N/record module, you’ll first need to load the record type you want to work with using the N/record.load() method. Once you have the record loaded, you can use various methods to manipulate the record data, such as getValue(), setValue(), and getField().
Also, to start working with a record, you can use the N/record.create() method. This allows you to create a new instance of a record by specifying its type. Once you have the record object, you can use methods like setValue() to set field values and getValue() to retrieve field values.
Methods:
record.create(): This method allows you to create a new instance of a record by specifying its type. It returns a record object that you can use to set field values and save the record. record.setValue(): This method is used to set field values on a record. You need to provide the field ID and the value you want to set.
record.getValue(): This method is used to retrieve the value of a specific field on a record. You need to provide the field ID to get the corresponding value.
record.save(): This method is used to save the changes made to a record. It persists the record in the NetSuite database.
record.load(): This method allows you to load an existing record by specifying its type and internal ID. It returns a record object that you can use to modify or retrieve field values.
If you want to create a new customer record and set some field values. You can use the N/record.create() method to create the record, and then use the setValue() method to set the field values.
Here’s an example:
var customerRecord = record.create({
type: record.Type.CUSTOMER,
isDynamic: true
});
customerRecord.setValue({
fieldId: ‘entityid’,
value: ‘JohnDoe123’
});
customerRecord.setValue({
fieldId: ‘companyname’,
value: ‘ABC Corp’
});
customerRecord.setValue({
fieldId: ’email’,
value: ‘johndoe@example.com’
});
customerRecord.save();
In this example, we create a new customer record using record.create() and specify the record type as CUSTOMER. Then we use setValue() to set the values for fields like entityid, companyname, and email. Finally, we save the record using save().
If you want to load an existing record and retrieve field values, you can use the record.load() method. Here’s an example:
var existingRecord = record.load({
type: record.Type.CUSTOMER,
id: ‘12345’
});
var entityName = existingRecord.getValue({
fieldId: ‘entityid’
});
var companyName = existingRecord.getValue({
fieldId: ‘companyname’
});
console.log(‘Entity Name:’, entityName);
console.log(‘Company Name:’, companyName);
In this example, we load an existing customer record with the internal ID 12345 using record.load(). Then we use getValue() to retrieve the values of fields like entityid and companyname. Finally, we log the values to the console.
Keep in mind that the N/record module provides different methods and properties depending on the record type you’re working with.