N/record Module Script Samples

The following script samples demonstrate how to use the features of the N/record module:

Create and Save a Contact Record

The following sample shows how to use the N/record module to create and save a contact record.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

Important:

Some of the values in these samples are placeholders. Before using these samples, replace all hard-coded values, such as IDs and file paths, with valid values from your NetSuite account. If you run a script with an invalid value, the system may throw an error.

Important:

This sample uses SuiteScript 2.1. For more information, see SuiteScript 2.1.

            /**
 * @NApiVersion 2.1
 */
require(['N/record'], record => {
    // Create an object to hold name data for the contact
    const nameData = {
        firstname: 'John',
        middlename: 'Doe',
        lastname: 'Smith'
    };

    // Create a contact record
    let objRecord = record.create({
        type: record.Type.CONTACT,
        isDynamic: true
    });

    // Set the values of the subsidiary, firstname, middlename,
    // and lastname properties
    objRecord.setValue({
        fieldId: 'subsidiary',
        value: '1'
    });
    for (let key in nameData) {
        if (nameData.hasOwnProperty(key)) {
            objRecord.setValue({
                fieldId: key,
                value: nameData[key]
            });
        }
    }

    // Save the record
    let recordId = objRecord.save({
        enableSourcing: false,
        ignoreMandatoryFields: false
    });
}); 

          

Create and Save a Contact Record Asynchronously Using Promise Methods

The following sample shows how to create and save a contact record using promise methods.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

Note:

To debug client scripts like the following, you should use Chrome DevTools for Chrome, Firebug debugger for Firefox, or Microsoft Script Debugger for Internet Explorer. For information about these tools, see the documentation provided with each browser. For more information about debugging SuiteScript client scripts, see Debugging Client Scripts.

Important:

This sample uses SuiteScript 2.1. For more information, see SuiteScript 2.1.

            /**
 * @NApiVersion 2.1
 * @NScriptType ClientScript
 */
require(['N/record'], record => {
    // Create an object to hold name data for the contact
    const nameData = {
        firstname: 'John',
        middlename: 'Doe',
        lastname: 'Smith'
    };
     
    // Create a contact record using the promise method
    let createRecordPromise = record.create.promise({
        type: record.Type.CONTACT,
        isDynamic: true
    });
 
    // When the promise is fulfilled, set the values of the subsidiary,
    // firstname, middlename, and lastname properties, and save the
    // record
    createRecordPromise.then(objRecord => {
        log.debug('Start evaluating promise content...');
        objRecord.setValue({
            fieldId: 'subsidiary',
            value: '1'
        });
        for (let key in nameData) {
            if (nameData.hasOwnProperty(key)) {
                objRecord.setValue({
                    fieldId: key,
                    value: nameData[key]
                });
            }
        }
        let recordId = objRecord.save({
            enableSourcing: false,
            ignoreMandatoryFields: false
        });
    }, function(e) {
        log.error('Unable to create contact', e.name);
    });
}); 

          

Create Multiple Sales Records Using a Scheduled Script

The following sample shows how to use a scheduled script to create multiple sales records and log the record creation progress.

Note:

This script sample uses the define function, which is required for an entry point script (a script you attach to a script record and deploy). You must use the require function if you want to copy the script into the SuiteScript Debugger and test it. For more information, see SuiteScript 2.x Global Objects.

            /**
* @NApiVersion 2.x
* @NScriptType ScheduledScript
*/

// This script creates multiple sales records and logs the record creation progress.
define(['N/runtime', 'N/record'], function(runtime, record) {
    return {
        execute: function(context) {
            var script = runtime.getCurrentScript();
            for (x = 0; x < 500; x++) {
                var rec = record.create({
                    type: record.Type.SALES_ORDER
                });
                script.percentComplete = (x * 100)/500;
                log.debug({
                    title: 'New Sales Orders', 
                    details: 'Record creation progress: ' + script.percentComplete + '%'
                });
            }
         }
    };
}); 

          

Access Sublists and a Subrecord from a Record

The following sample shows how to access sublists and a subrecord from a record. This sample requires the Advanced Number Inventory Management feature.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

            /**
 * @NApiVersion 2.x
 */

require(['N/record'], function(record) {
    function createPurchaseOrder() {
        var rec = record.create({
            type: 'purchaseorder',
            isDynamic: true
        });
        rec.setValue({
            fieldId: 'entity',
            value: 52
        });
        rec.setValue({
            fieldId: 'location',
            value: 2
        });
        rec.selectNewLine({
            sublistId: 'item'
        });
        rec.setCurrentSublistValue({
            sublistId: 'item',
            fieldId: 'item',
            value: 190
        });
        rec.setCurrentSublistValue({
            sublistId: 'item',
            fieldId: 'quantity',
            value: 2
        });
        subrecordInvDetail = rec.getCurrentSublistSubrecord({
            sublistId: 'item',
            fieldId: 'inventorydetail'
        });
        subrecordInvDetail.selectNewLine({
            sublistId: 'inventoryassignment'
        });
        subrecordInvDetail.setCurrentSublistValue({
            sublistId: 'inventoryassignment',
            fieldId: 'receiptinventorynumber',
            value: 'myinventoryNumber'
        });
        subrecordInvDetail.commitLine({
            sublistId: 'inventoryassignment'
        });
        subrecordInvDetail.selectLine({
            sublistId: 'inventoryassignment',
            line: 0
        });
        var myInventoryNumber = subrecordInvDetail.getCurrentSublistValue({
            sublistId: 'inventoryassignment',
            fieldId: 'receiptinventorynumber'
        });
        rec.commitLine({
            sublistId: 'item'
        });
        var recordId = rec.save();
    }
    createPurchaseOrder();
}); 

          
Note:

For additional script samples that include subrecords, see SuiteScript 2.x Scripting Subrecords.

Access Sublists and a Subrecord from a Record Asynchronously Using Promise Methods

The following sample shows how to access sublists and a subrecord from a record using promise methods. This sample requires the Advanced Number Inventory Management feature.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

Note:

To debug client scripts like the following, you should use Chrome DevTools for Chrome, Firebug debugger for Firefox, or Microsoft Script Debugger for Internet Explorer. For information about these tools, see the documentation provided with each browser. For more information about debugging SuiteScript client scripts, see Debugging Client Scripts.

            /**
 * @NApiVersion 2.x
 */

require(['N/record'], function(record) {
    function createPurchaseOrder() {
        var createRecordPromise = record.create.promise({
            type: 'purchaseorder',
            isDynamic: true
        });
        createRecordPromise.then(function(rec) {
            rec.setValue({
                fieldId: 'entity',
                value: 52
            });
            rec.setValue({
                fieldId: 'location',
                value: 2
            });
            rec.selectNewLine({
                sublistId: 'item'
            });
            rec.setCurrentSublistValue({
                sublistId: 'item',
                fieldId: 'item',
                value: 190
            });
            rec.setCurrentSublistValue({
                sublistId: 'item',
                fieldId: 'quantity',
                value: 2
            });
            subrecordInvDetail = rec.getCurrentSublistSubrecord({
                sublistId: 'item',
                fieldId: 'inventorydetail'
            });
            subrecordInvDetail.selectNewLine({
                sublistId: 'inventoryassignment'
            });
            subrecordInvDetail.setCurrentSublistValue({
                sublistId: 'inventoryassignment',
                fieldId: 'receiptinventorynumber',
                value: 'myinventoryNumber'
            });
            subrecordInvDetail.commitLine({
                sublistId: 'inventoryassignment'
            });
            subrecordInvDetail.selectLine({
                sublistId: 'inventoryassignment',
                line: 0
            });
            var myInventoryNumber = subrecordInvDetail.getCurrentSublistValue({
                sublistId: 'inventoryassignment',
                fieldId: 'receiptinventorynumber'
            });
            rec.commitLine({
                sublistId: 'item'
            });
              var recordId = rec.save();
        }, function(err) {
            log.error('Unable to create purchase order!', err.name);
        });
    }
    createPurchaseOrder();
}); 

          

Call a Macro on a Sales Order Record

The following sample shows you how to call a calculateTax macro on a sales order record. To execute a macro on a record, the record must be created or loaded in dynamic mode. Note that the SuiteTax feature must be enabled to successfully execute the macro used in this sample.

For information about record macros, see Overview of Record Action and Macro APIs.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

            /**
 * @NApiVersion 2.x
 */

require(['N/record'],
    function(record) {

     var recordObj = record.create({
         type: record.Type.SALES_ORDER,
         isDynamic: true
     });

     var ENTITY_VALUE = 1;    
     var ITEM_VALUE = 1;
     recordObj.setValue({
         fieldId: 'entity',
         value: ENTITY_VALUE
    });
    recordObj.selectNewLine({
         sublistId: 'item'
    });
    recordObj.setCurrentSublistValue({
         sublistId: 'item',
         fieldId: 'item',
         value: ITEM_VALUE
    });
    recordObj.setCurrentSublistValue({
         sublistId: 'item',
         fieldId: 'quantity',
         value: 1
    });
    recordObj.commitLine({
         sublistId:'item'
    });

    var totalBeforeTax = recordObj.getValue({fieldId: 'total'});

    // get macros available on the record
    var macros = recordObj.getMacros();

    // execute the macro
    if ('calculateTax' in macros)
    {
        macros.calculateTax(); // For promise version use: macros.calculateTax.promise()
    }
    // Alternative (direct) macro execution
    // var calculateTax = recordObj.getMacro({id: 'calculateTax'});
    // calculateTax(); // For promise version use: calculateTax.promise()
    var totalAfterTax = recordObj.getValue({fieldId: 'total'});

    var recordId = recordObj.save({
       enableSourcing: false,  
       ignoreMandatoryFields: false
    });
}); 

          

General Notices