Add a button in lead record

Jira Code: IZN-386

  • Add a button in lead/prospect/ Customer record in Netsuite using workflow.
  • Deploy a workflow action script to search if laying record already exist, if exist return true to show button in lead/prospect/customer record else return false to hide button.
  • The button should be visible if the record doesn’t have any laying plan created.
  • Button needed in View mode & independent of status .
  • While clicking the button we have to create a Laying plan in All In View.
  • Deploy a workflow action script to create lead record.
/**
 * @NApiVersion 2.x
 * @NScriptType WorkflowActionScript
 */

define(['N/search', 'N/record', 'N/file', "N/render", "N/encode"],

    function(search, record, file, render, encode) {
        var main = {
            onAction: function(scriptContext) {
                log.debug("scriptContext.newRecord.id", scriptContext.newRecord.id);
                if (scriptContext.type == "create" || scriptContext.type == "edit" || scriptContext.type == "view") {
                    /*log.debug("scriptContext.newRecord.id",scriptContext.newRecord.id);*/
                   var record_exist= main.check_Laying_record(scriptContext.newRecord.id);
                   log.debug("record_exist1",record_exist);
                   if(record_exist){
                   	log.debug("record_exist2",record_exist);
                   	return true;
                   }
                   else{
                    log.debug("record_exist3",record_exist);
                   	return false;
                   }

                }

            },
            check_Laying_record: function(id) {
            	var laying_recordid;
                var customrecord_laying_plan_for_leadSearchObj = search.create({
                    type: "customrecord_laying_plan_for_lead",
                    filters: [
                        ["custrecord_parent_lead.internalid", "anyof", id]
                    ],
                    columns: [
                        search.createColumn({ name: "internalid", label: "Internal ID" })
                    ]
                });
                var searchResultCount = customrecord_laying_plan_for_leadSearchObj.runPaged().count;
                log.debug("customrecord_laying_plan_for_leadSearchObj result count", searchResultCount);
                 customrecord_laying_plan_for_leadSearchObj.run().each(function(result) {
                        laying_recordid = result.getValue(customrecord_laying_plan_for_leadSearchObj.columns[0]);
                        
                        return true;
                    });
                 if(searchResultCount>0){
                 	log.debug("Record Found");
                 	return laying_recordid ;
                 	
                 }
                 else{
                 	log.debug("Record Not Found");
                 	return "";
                 }
            }
        }
        for (var key in main) {
            if (typeof main[key] === 'function') {
                main[key] = trycatch(main[key], key);
            }
        }

        function trycatch(myfunction, key) {
            return function() {
                try {
                    return myfunction.apply(this, arguments);
                } catch (e) {
                    log.debug("e in  " + key, e);
                }
            }
        };

        return main;

    });
/**
 * @NApiVersion 2.x
 * @NScriptType WorkflowActionScript
 */

define(['N/https', 'N/search', 'N/record', 'N/file', "N/render", "N/encode"],

    function(https, search, record, file, render, encode) {
        var qualifiedStatus = 7,
            authorizeValue = 'Basic YW5qdUBqb2JpbmFuZGppc21pLmNvbTpEd1NOczJzQitk';
        var allInViewURLHeader = "https://demo.all-in-view.com";
        var headerObj = {
            'Accept': 'application/json',
            'authorization': authorizeValue,
            'Content-Type': 'application/x-www-form-urlencoded'
        };
        var main = {
            onAction: function(scriptContext) {
                log.debug("scriptContext.newRecord.id", scriptContext.newRecord.id);
                log.debug("scriptContext", scriptContext);
                log.debug("scriptContext.type", scriptContext.type);
                /*if (scriptContext.type == "create" || scriptContext.type == "edit" || scriptContext.type == "view") {*/
                if (scriptContext.type == "button") {
                    var custom_recordexist = main.checkIfLPCreatedForTheLead(scriptContext.newRecord.id);
                    log.debug("custom_recordexist", custom_recordexist);
                    if (!custom_recordexist) {
                        var selectLine = scriptContext.newRecord.selectLine({
                            sublistId: 'addressbook',
                            line: 0
                        });

                        var addrSubrecord = scriptContext.newRecord.getCurrentSublistSubrecord({
                            sublistId: 'addressbook',
                            fieldId: 'addressbookaddress',
                        });
                        var street_houseNum = addrSubrecord.getValue({
                            fieldId: 'addr1'

                        });
                        var postcode = addrSubrecord.getValue({
                            fieldId: 'zip'

                        });
                        var city = addrSubrecord.getValue({
                            fieldId: 'city'

                        });
                        var addition = "",
                            houseNumber = "",
                            houseNumToSend;
                        log.debug("addrSubrecord", addrSubrecord)
                        log.debug("street_houseNum", street_houseNum)
                        log.debug("postcode", postcode)
                        log.debug("city", city)

                        var street, houseNum;
                        var arrayOfStreet = street_houseNum.split(" ")
                        for (var i = 0; i < arrayOfStreet.length - 1; i++) {

                            if (i != 0)
                                street = street + " " + arrayOfStreet[i];
                            else
                                street = arrayOfStreet[i];

                        }
                        houseNum = arrayOfStreet[arrayOfStreet.length - 1];

                        log.debug("street", street)
                        log.debug("houseNum", houseNum)


                        for (var i = 0; i < houseNum.length; i++) {
                            if (isNaN(houseNum[i])) {
                                houseNumber = houseNum.substring(0, i);
                                log.debug("houseNumber", houseNumber)
                                addition = houseNum.substring(i, houseNum.length);
                                break;

                            }
                        }
                        if (main.checkif(houseNumber)) {
                            houseNumToSend = '&number=' + houseNumber + '&addition=' + addition;
                        } else {
                            houseNumToSend = '&number=' + houseNum
                        }


                        postcode = postcode.replace(" ", "+");
                        log.debug("postcode - Encoded", postcode)



                        var requestToAllInView = https.get({
                            url: allInViewURLHeader + '/api/0.9/search?street=' + street + '&postcode=' + postcode + '&city=' + city + houseNumToSend,
                            headers: headerObj

                        });
                        log.debug("REQ", allInViewURLHeader + '/api/0.9/search?street=' + street + '&postcode=' + postcode + '&city=' + city + houseNumToSend)
                        log.debug("requestToAllInView", requestToAllInView)


                        var retnValue = JSON.parse(requestToAllInView.body);
                        log.debug("retnValue", retnValue)
                        if (main.checkif(retnValue.error)) {
                            log.debug("if condition 1");
                            scriptContext.newRecord.setValue({
                                fieldId: 'custentity_error_in_all_in_view',
                                value: retnValue.error
                            })
                        } else if (retnValue.reply.length > 0) {
                            /*log.debug("if condition 2");
                            log.debug("retnValue.reply[0].lat", retnValue.reply[0].lat);
                            log.debug("retnValue.reply[0].lon", retnValue.reply[0].lon);*/
                            scriptContext.newRecord.setValue({
                                fieldId: 'custentity_latitude',
                                value: retnValue.reply[0].lat
                            });
                            scriptContext.newRecord.setValue({
                                fieldId: 'custentity_longitude',
                                value: retnValue.reply[0].lon
                            })
                            scriptContext.newRecord.setValue({
                                fieldId: 'custentity_error_in_all_in_view',
                                value: ""
                            });


                            scriptContext.newRecord.setValue({
                                fieldId: 'custentity_count',
                                value: 1
                            });
                            /*log.debug("createDesign retnValue.reply[0].lat", retnValue.reply[0].lat);
                            log.debug("createDesign retnValue.reply[0].lat", retnValue.reply[0].lon);
                            log.debug("createDesign id", scriptContext.newRecord.id);*/
                            var reqToAllInView = main.createDesign(retnValue.reply[0].lat, retnValue.reply[0].lon, scriptContext.newRecord.id);

                        } else {
                            log.debug("if condition 3");
                            scriptContext.newRecord.setValue({
                                fieldId: 'custentity_error_in_all_in_view',
                                value: "Latitude & longitude are unavailable for this address"
                            })

                        }
                    }


                }

            },
            checkIfLPCreatedForTheLead: function(id) {

                var customrecord_laying_plan_for_leadSearchObj = search.create({
                    type: "customrecord_laying_plan_for_lead",
                    filters: [
                        ["custrecord_parent_lead.internalid", "anyof", id]
                    ],
                    columns: [
                        search.createColumn({
                            name: "name",
                            sort: search.Sort.ASC,
                            label: "Name"
                        }),
                        search.createColumn({
                            name: "id",
                            label: "ID"
                        }),
                        search.createColumn({
                            name: "scriptid",
                            label: "Script ID"
                        }),
                        search.createColumn({
                            name: "custrecord_parent_lead",
                            label: "Lead"
                        }),
                        search.createColumn({
                            name: "custrecord_laying_plan_id",
                            label: "Laying plan"
                        }),
                        search.createColumn({
                            name: "custrecord_name_of_laying_plan",
                            label: "Name of Laying Plan"
                        }),
                        search.createColumn({
                            name: "custrecord_image_url",
                            label: "Image URL"
                        }),
                        search.createColumn({
                            name: "custrecord_err_msg",
                            label: "Error message"
                        }),
                        search.createColumn({
                            name: "custrecord_jj_laying_plan_image",
                            label: "Laying Plan Image"
                        })
                    ]
                });
                var searchResultCount = customrecord_laying_plan_for_leadSearchObj.runPaged().count;
                log.debug("customrecord_laying_plan_for_leadSearchObj result count", searchResultCount);
                customrecord_laying_plan_for_leadSearchObj.run().each(function(result) {
                    // .run().each has a limit of 4,000 results
                    return true;
                });
                if (searchResultCount > 0) {
                    return true;
                } else {
                    return false;
                }

            },
            createDesign: function(lat, lon, id) {
                try {
                    /*log.debug("lat createDesign", lat);
                    log.debug("lon createDesign", lon);
                    log.debug("id createDesign", id);*/

                    var layingPlanID = 'LP_' + id + '_0'
                    var requestToAllInView = https.post({
                        url: allInViewURLHeader + '/api/0.9/scenario',
                        body: 'lat=' + lat + '&lon=' + lon + '&remote_id=' + layingPlanID,
                        headers: headerObj

                    });
                    log.debug("requestToAllInView in Create design", requestToAllInView)
                    if (requestToAllInView.code == 200) {

                        // to save this into custom record

                        var customRec = record.create({
                            type: 'customrecord_laying_plan_for_lead'

                        })

                        //laed id -->   custrecord_parent_lead
                        customRec.setValue({
                                fieldId: "custrecord_parent_lead",
                                value: id
                            })
                            //laying plan id -->    custrecord_laying_plan_id
                        customRec.setValue({
                            fieldId: "custrecord_laying_plan_id",
                            value: layingPlanID
                        });

                        customRec.setValue({
                            fieldId: "name",
                            value: layingPlanID
                        });
                        var customRecID = customRec.save();

                    }

                } catch (error) {
                    log.debug("err@ create design", e)
                    log.error("err@ create design", e)

                }
            },
            checkif: function(parameter, parameterName) {
                if (parameter !== null && parameter !== undefined && parameter !== false && parameter !== "null" && parameter !== "undefined" && parameter !== "false" && parameter !== "" && parameter !== " ")
                    return true;
                if (parameterName)
                    log.debug("Empty Value found", "Empty Value for parameter " + parameterName);
                return false;
            }
        }
        for (var key in main) {
            if (typeof main[key] === 'function') {
                main[key] = trycatch(main[key], key);
            }
        }

        function trycatch(myfunction, key) {
            return function() {
                try {
                    return myfunction.apply(this, arguments);
                } catch (e) {
                    log.debug("e in  " + key, e);
                }
            }
        };

        return main;

    });

Leave a comment

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