Example of a RESTlet that Adds Multiple Records

Here’s an example of using a RESTlet to create multiple contact records in one call.

RESTlet

            /**
 * @NApiVersion 2.x
 * @NScriptType Restlet
 */
define([ 'N/record' ], function(record) {
    return {
        post: function(restletBody) {
            var restletData = restletBody.data;
            for (var contact in restletData) {
                var objRecord = record.create({
                    type : record.Type.CONTACT,
                    isDynamic : true
                });
                var contactData = restletData[contact];
                for (var key in contactData) {
                    if (contactData.hasOwnProperty(key)) {
                        objRecord.setValue({
                           fieldId : key,
                           value : contactData[key]
                        });
                    }
                }
                var recordId = objRecord.save({
                    enableSourcing : false,
                    ignoreMandatoryFields : false
                });
                log.debug(recordId);
            }
        }
    }
}); 

          

Example Post Call

To create records with this RESTlet, use the post method and send your values for the new contacts in the request body. For example:

            {"data":[{"firstname":"John","middlename":"Robert","lastname":"Smith","subsidiary":"1"},{"firstname":"Anne","middlename":"Doe","lastname":"Smith","subsidiary":"1"}]} 

          

Related Topics

General Notices