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);
    });
}); 

        

General Notices