Function to create the customer record

/**
 * @description Create a customer in NetSuite
 * @param {Object} parameterObject
 * @param {String} parameterObject.EMAIL_ID - The Email ID for the Customer
 * @param {String} parameterObject.FIRST_NAME - The First name for the Customer
 * @param {String} parameterObject.LAST_NAME - The Last name for the customer
 * @param {Number|String} parameterObject.PHONE - The phone number for the customer
 * @param {boolean} parameterObject.TAX_EXEMPT - shows the customer is tax exempt or not
 * @param {String} parameterObject.DEFAULT_ADDRESS - the object that contains the default address details
 * @param {String} parameterObject.BILLING_ADDRESS - the object that contains the billing address detains
 * @returns {boolean|Number} - Customer Internal ID if successful, else false
 */
createCustomer({
                   CUSTOMER_ID,
                   EMAIL_ID,
                   FIRST_NAME,
                   LAST_NAME,
                   PHONE,
                   TAX_EXEMPT,
                   DEFAULT_ADDRESS,
                   BILLING_ADDRESS
               }) {
    try {
        log.debug('customer creation starts!!');
        let customerRecord = record.create({type: record.Type.CUSTOMER, isDynamic: true});

        //Field Map for Customer Body Fields
        let bodyFieldArray = [
            {
                fieldId: 'entityid', //Customer ID
                value: `${FIRST_NAME} ${LAST_NAME}_${CUSTOMER_ID}`
            },
            {
                fieldId: 'entitystatus', //STATUS
                value: 13 //CUSTOMER-Active
            },
            {
                fieldId: 'custentity_integration_success',
                value: true
            },
            {
                fieldId: 'isperson', //TYPE
                value: 'F'
            },
            {
                fieldId: 'subsidiary', //SUBSIDIARY
                value: "1"
            },
            {
                fieldId: 'companyname', //COMPANY NAME
                value: `${FIRST_NAME} ${LAST_NAME}`
            },
            {
                fieldId: 'email', //STATEMENT EMAIL
                value: EMAIL_ID
            },
            {
                fieldId: 'phone', //PHONE
                value: PHONE
            },
            {
                fieldId: 'taxexempt',
                value: TAX_EXEMPT
            },
            {
                fieldId: 'currency',//PRIMARY CURRENCY
                value: 1 //USD
            },
            {
                fieldId: 'salesrep',
                value: SALES_REP
            },
            {
                fieldId: 'category',
                value: CUSTOMER_CATEGORY
            },
            {
                fieldId: 'custentity6',
                value: FULFILL_CUST
            }
        ];

        //To set body fields in Customer Record
        for (let field of bodyFieldArray) {
            if (field.value !== null && field.value !== undefined && field.value !== "") {
                try {
                    customerRecord.setValue({fieldId: field.fieldId, value: field.value});
                } catch (er) {
                    log.error('error@createCustomer in setting Customer Body Field skipped', JSON.stringify(er));
                    continue;
                }
            }
        }

        //Field Map for Customer Address Sublist and Subrecord Fields
        let addressBookFields = [
            {
                sublistFields: [
                    {fieldId: 'defaultshipping', value: false},
                    {fieldId: 'defaultbilling', value: true}
                ],
                subrecordFields: [
                    {
                        fieldId: 'country',
                        value: jjUtil.isNumberOrString(BILLING_ADDRESS.country_code) ? BILLING_ADDRESS.country_code : ""
                    },
                    {
                        fieldId: 'attention',
                        value: ""
                    },
                    {
                        fieldId: 'addressee',
                        value: ""
                    },
                    {
                        fieldId: 'addrphone',
                        value: jjUtil.isNumberOrString(BILLING_ADDRESS.phone) ? BILLING_ADDRESS.phone : ""
                    },
                    {
                        fieldId: 'addr1',
                        value: jjUtil.isNumberOrString(BILLING_ADDRESS.address1) ? BILLING_ADDRESS.address1 : ""
                    },
                    {
                        fieldId: 'addr2',
                        value: jjUtil.isNumberOrString(BILLING_ADDRESS.address2) ? BILLING_ADDRESS.address2 : ""
                    },
                    {
                        fieldId: 'addr3',
                        value: ""
                    },
                    {
                        fieldId: 'city',
                        value: jjUtil.isNumberOrString(BILLING_ADDRESS.city) ? BILLING_ADDRESS.city : ""
                    },
                    {
                        fieldId: 'state',
                        value: jjUtil.isNumberOrString(BILLING_ADDRESS.province) ? BILLING_ADDRESS.province : ""
                    },
                    {
                        fieldId: 'zip',
                        value: jjUtil.isNumberOrString(BILLING_ADDRESS.zip) ? BILLING_ADDRESS.zip : ""
                    }]
            },
            {
                sublistFields: [
                    {fieldId: 'defaultshipping', value: true},
                    {fieldId: 'defaultbilling', value: false}
                ],
                subrecordFields: [
                    {
                        fieldId: 'country',
                        value: jjUtil.isNumberOrString(DEFAULT_ADDRESS.country_code) ? DEFAULT_ADDRESS.country_code : ""
                    },
                    {
                        fieldId: 'attention',
                        value: ""
                    },
                    {
                        fieldId: 'addressee',
                        value: ""
                    },
                    {
                        fieldId: 'addrphone',
                        value: jjUtil.isNumberOrString(DEFAULT_ADDRESS.phone) ? DEFAULT_ADDRESS.phone : ""
                    },
                    {
                        fieldId: 'addr1',
                        value: jjUtil.isNumberOrString(DEFAULT_ADDRESS.address1) ? DEFAULT_ADDRESS.address1 : ""
                    },
                    {
                        fieldId: 'addr2',
                        value: jjUtil.isNumberOrString(DEFAULT_ADDRESS.address2) ? DEFAULT_ADDRESS.address2 : ""
                    },
                    {
                        fieldId: 'addr3',
                        value: ""
                    },
                    {
                        fieldId: 'city',
                        value: jjUtil.isNumberOrString(DEFAULT_ADDRESS.city) ? DEFAULT_ADDRESS.city : ""
                    },
                    {
                        fieldId: 'state',
                        value: jjUtil.isNumberOrString(DEFAULT_ADDRESS.province) ? DEFAULT_ADDRESS.province : ""
                    },
                    {
                        fieldId: 'zip',
                        value: jjUtil.isNumberOrString(DEFAULT_ADDRESS.zip) ? DEFAULT_ADDRESS.zip : ""
                    }]
            }];


        //To set Address in Customer Record
        for (let address of addressBookFields) {

            customerRecord.selectNewLine({sublistId: 'addressbook'});

            let addressSubrecord = customerRecord.getCurrentSublistSubrecord({
                sublistId: 'addressbook',
                fieldId: 'addressbookaddress'
            });

            //Set Address Sublist Subrecord values
            for (let subrecordField of address.subrecordFields) {
                if (subrecordField.value !== null && subrecordField.value !== undefined && subrecordField.value !== "") {
                    try {
                        addressSubrecord.setValue({
                            fieldId: subrecordField.fieldId,
                            value: subrecordField.value,
                            ignoreFieldChange: true
                        });
                    } catch (er) {
                        log.error('error@createCustomer in setting Address Fields skipped', JSON.stringify(er));
                        continue;
                    }
                }
            }
            customerRecord.commitLine({sublistId: 'addressbook'});
        }

        let customerID = customerRecord.save({enableSourcing: true, ignoreMandatoryFields: true});
        log.debug('customerID 688', customerID);

        return customerID;

    } catch (err) {
        log.error('err==', err);
        log.error('error@createCustomer', err);
    }
}

Leave a comment

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