Creating a customer record using REST API

Need to create a restlet script and customer details will be sent to the restlet script. A post action needs to be done with the restlet script. Restlet script should create the customer based on the body details sent along with the POST request.

RESTlet script:

/**
 * @NApiVersion 2.x
 * @NScriptType Restlet
 * @NModuleScope SameAccount
 */
/*******************************************************************
 *
 * *****************************************************************
 *
 * Date: 26/08/2021
 *
 * Author: Lakshmi priya, Jobin and Jismi IT Services LLP
 *
 * REVISION HISTORY
 *
 * Revision
 *
 *
 * ****************************************************************/

define([ 'N/record', 'N/error' ], function(record, error) {
    var main = {
        post: function (requestBody) {
          try {
            // if the call contains request body, then create the customer
            if(requestBody) {
                var objRecord = record.create({
                    type: record.Type.CUSTOMER,
                    isDynamic: true
                });
                // map customer details to created customer record
                
                objRecord.setValue({fieldId: "custentity_magentoid", value: requestBody.id});
                objRecord.setValue({fieldId: "companyname", value: requestBody.firstname + " " + requestBody.lastname});
                objRecord.setValue({fieldId: "email", value: requestBody.email});
                if(requestBody.phone!=null){
                objRecord.setValue({fieldId: "phone", value:requestBody.phone.toString()});
                }
               if(requestBody.landlinenumber!=null){
                objRecord.setValue({fieldId: "altphone", value:requestBody.landlinenumber.toString()});
               }

                //map customer address to address subrecord
                objRecord.selectNewLine({
                        sublistId: "addressbook"
                });

                var addressSubrecord = objRecord.getCurrentSublistSubrecord({
                        sublistId: "addressbook",
                        fieldId: "addressbookaddress"
                });
              
                if (requestBody.registrationaddress !== null && requestBody.registrationaddress !== undefined && requestBody.registrationaddress !== ""){
                        addressSubrecord.setValue({
                                fieldId: "addr1",
                                value: requestBody.registrationaddress
                        });
                }
                if (requestBody.registrationcity !== null && requestBody.registrationcity !== undefined && requestBody.registrationcity !== ""){
                        addressSubrecord.setValue({
                                fieldId: "city",
                                value: requestBody.registrationcity
                        });
                }
                objRecord.commitLine({
                        sublistId: 'addressbook'
                });
            }
                var recordId = objRecord.save({
                    enableSourcing : false,
                    ignoreMandatoryFields : false
                });
                } catch (e) {
                    log.debug("error@post", e);
                    return JSON.stringify({
                        status: "FAILURE",
                        reason: "ERROR",
                        error: {name: e.name, message: e.message}
                    });
                }
            }
        }
        return main;
    });

Sample RequestBody:

{

    “id”: 2,

    “key”: 1,

    “group_id”:”1″,

    “created_at”:”2021-08-26 11:44:07″,

    “updated_at”:”2021-08-26 11:44:07″,

    “email”:”testjj12a@mailinator.com”,

    “firstname”:”Testings”,

    “lastname”:”currents”,

     “country”:”Australia”,

    “phone”:null,

    “landlinenumber”:null,

    “contactperson”:null,

   “contactpersonalmobileno”:null,

  “legalname”:null,

  “registrationcity”:null,

 “registrationaddress”:null,

 “traidlicno”:null,

 “vatno”:null,

 “fileupload”:””,

 “fileuploadvat”:””

}

Can use the external url of RESTlet scripts deployment as URL to send POST request to create a new customer with details in Netsuite.

Leave a comment

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