Restlet for Lead record integration

/**
 * @NApiVersion 2.x
 * @NScriptType restlet
 */
define(["N/record","N/search"], function (record,search) {
    var mapConfig = {
        lead_first_name: {
            destiny: "firstname",
            type: "VAL"
        },
        lead_last_name: {
            destiny: "lastname",
            type: "VAL"
        },
        email_address: {
            destiny: "email",
            type: "VAL"
        },
        contact_no: {
            destiny: "phone",
            type: "VAL"
        },
        companyname: {
            destiny: "companyname",
            type: "VAL"
        },
        leadsource: {
            destiny: "leadsource",
            type: "TEXT"
        },
        campaignevent: {
            destiny: "campaignevent",
            type: "TEXT"
        },
        salutation: {
            destiny: "salutation",
            type: "VAL"
        },
    }


    var address = {
        country: {
            destiny: "country",
            type: "VAL"
        },
        street1: {
            destiny: "addr1",
            type: "VAL"
        },
        street2: {
            destiny: "addr2",
            type: "VAL"
        },
        city: {
            destiny: "city",
            type: "VAL"
        },
        pin_code: {
            destiny: "zip",
            type: "VAL"
        }

    };


    return {
        post: function (body) {

            try {

                log.debug("body",body);

                var newLead = record.create({
                    type: "lead",
                    isDynamic: true
                });

                var inputJSON = body;

                for (var key in mapConfig) {

                    // if (!inputJSON[key]) {
                    //     return {
                    //         "code": "VALUE_NOT_FOUND",
                    //         "message": "please fill value in " + key
                    //     };
                    // }

                    if (mapConfig[key].type == "TEXT" && inputJSON[key])
                        newLead.setText(mapConfig[key].destiny, inputJSON[key]);
                    else
                        newLead.setValue(mapConfig[key].destiny, inputJSON[key]);

                }

                var checkAddr = false;
                for (var key in address) {
                    if (inputJSON[key]) {
                        checkAddr = true;
                        break;
                    }
                }

                if (checkAddr) {
                    newLead.selectNewLine('addressbook');

                    var sublistSubRecord = newLead.getCurrentSublistSubrecord({
                        sublistId: 'addressbook',
                        fieldId: 'addressbookaddress'
                    });


                    for (var key in address) {

                        // if (!inputJSON[key]) return {
                        //     "code": "VALUE_NOT_FOUND",
                        //     "message": "please fill value in " + key
                        // };

                        if (address[key].type == "TEXT")
                            sublistSubRecord.setText({
                                sublistId: "addressbook",
                                fieldId: address[key].destiny,
                                text: inputJSON[key],
                                line: 0
                            });
                        else
                            sublistSubRecord.setValue({
                                sublistId: "addressbook",
                                fieldId: address[key].destiny,
                                value: inputJSON[key],
                                line: 0
                            });

                    }

                    newLead.commitLine('addressbook');

                }

                var id = newLead.save({
                    ignoreMandatoryFields: true
                });

               var tranid =  search.lookupFields({
                type:"lead",
                id:id,
                columns:["entityid"]
                })["entityid"]

                log.debug("tranid",tranid);

                return {
                    "message": "Lead Record added",
                    "responseCode": "1",
                    "data": [{
                        "lead_rec_id": tranid
                    }]
                };


            } catch (err) {
                log.debug("error@post", err);
                log.error("error@post", err);
                return {
                    "code": err.name,
                    "message": err.message
                };
            }
        }
    }
});

Leave a comment

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