Understanding the N/RECORD Module in NetSuite

The N/RECORD module in NetSuite SuiteScript allows developers to interact programmatically with NetSuite records. This module is essential for creating, reading, updating, and deleting records like customers, transactions, and custom records. It provides robust CRUD (Create, Read, Update, Delete) functionality to handle records efficiently within scripts.

Key Functions of N/RECORD

Creating Records

The create() function allows you to create a new record. This function is used to instantiate a new record, where you can set values and save it.

define(['N/record'], function(record) {
    function createRecordExample() {
        var newRecord = record.create({
            type: record.Type.CUSTOMER, // Record type
            isDynamic: true
        });
        newRecord.setValue({
            fieldId: 'companyname',
            value: 'New Customer'
        });
        var recordId = newRecord.save(); // Save the record
        log.debug('Record ID', recordId);
    }
    return {
        execute: createRecordExample
    };
});

Loading Records

The load() function is used to load an existing record by its ID. Once loaded, you can read or modify the fields as necessary.

define(['N/record'], function(record) {
    function loadRecordExample() {
        var loadedRecord = record.load({
            type: record.Type.CUSTOMER, // Record type
            id: 1234 // Record ID
        });
        var companyName = loadedRecord.getValue({ fieldId: 'companyname' });
        log.debug('Company Name', companyName);
    }
    return {
        execute: loadRecordExample
    };
});

Updating Records

Once a record is loaded, you can update its values using the setValue() method. After making the changes, use save() to commit the updates.

define(['N/record'], function(record) {
    function updateRecordExample() {
        var updatedRecord = record.load({
            type: record.Type.CUSTOMER,
            id: 1234
        });
        updatedRecord.setValue({
            fieldId: 'email',
            value: 'new.email@example.com'
        });
        updatedRecord.save();
    }
    return {
        execute: updateRecordExample
    };
});

Deleting Records

The delete() function allows you to delete a record. You just need to specify the record type and ID to remove it from the system.

define(['N/record'], function(record) {
    function deleteRecordExample() {
        record.delete({
            type: record.Type.CUSTOMER,
            id: 1234
        });
        log.debug('Record Deleted', 'The customer record has been deleted.');
    }
    return {
        execute: deleteRecordExample
    };
});

Working with Subrecords

The N/RECORD module enables you to handle subrecords such as line items or address records. You can work with these nested records by retrieving and modifying them through their parent record.

define(['N/record'], function(record) {
    function subrecordExample() {
        var newRecord = record.create({
            type: record.Type.SALES_ORDER,
            isDynamic: true
        });
        newRecord.setValue({
            fieldId: 'entity',
            value: 123 // Customer ID
        });
        var lineItemSubrecord = newRecord.getSubrecord({
            fieldId: 'item'
        });
        lineItemSubrecord.setValue({
            fieldId: 'quantity',
            value: 10
        });
        newRecord.save();
    }
    return {
        execute: subrecordExample
    };
});

Common Record Operations

  • setValue(): Sets values in specific fields of the record.
  • getValue(): Retrieves the current value from specific fields of the record.
  • save(): Saves the record after creation or modification.
  • delete(): Deletes the record from the system.
  • getSubrecord(): Allows access to subrecords, such as line items or address information, for nested record management.

Performance Considerations

  • Avoid Unnecessary Loads: Loading records too frequently can degrade performance, especially if large datasets are involved. Always ensure that you only load the records you need.
  • Bulk Operations: If you’re dealing with large amounts of data, consider using NetSuite’s N/Task module or other batch processing techniques to minimize performance issues.

Conclusion

The N/RECORD module is a critical component for interacting with NetSuite records programmatically. By using this module, you can automate data entry, streamline workflows, and ensure accurate record management across your NetSuite environment.

Leave a comment

Your email address will not be published. Required fields are marked *