Jira Code: EX-120
- When a new lead is created through the website, check the customer is already existing.
- if it is existing then attach the new lead in the multi-select field.
When a lead is created from a website to netsuite a userevent script is deployed to check existing leads/prospects/customers in Netsuite. We have set email as key to identify existing customers. If there are existing leads/prospects/customers in Netsuite for existing leads from the website, it is added as a reference to existing records. If there are multiple existing leads/prospects/customers in Netsuite for existing leads from the website on create, the newly created lead and the existing records will be updated in each existing records multi select field as reference.
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
*/
/**
/**
* Script Description
*
*/
/*******************************************************************************
* EX | EX-120 | Attach web leads to existing customer
* **************************************************************************
* Author: Jobin & Jismi IT Services LLP
*
* Date Created : 27 May 2020
*
* REVISION HISTORY
*/
define(['N/file', 'N/http', 'N/https', 'N/record', 'N/runtime', 'N/search', 'N/task', 'N/xml', "N/render", 'N/encode', 'N/email'],
function(file, http, https, record, runtime, search, task, xml, render, encode, email) {
var ITEM_OBJ = {};
var main = {
afterSubmit: function(scriptContext) {
log.debug("scriptContext", scriptContext);
log.debug("scriptContext.type", scriptContext.type);
var lead_source = main.checkForParameter(scriptContext.newRecord.getValue({
fieldId: 'leadsource'
}));
log.debug("lead_source", lead_source);
if (lead_source == -6 && scriptContext.type == "create") {
var Cust_id = main.checkForParameter(scriptContext.newRecord.getValue({
fieldId: 'email'
}));
log.debug("Cust_id", Cust_id);
var Cust_exist_id = main.checkForParameter(main.existing_Customers(Cust_id));
log.debug("Cust_exist_id", Cust_exist_id);
if (main.validate_param(Cust_exist_id)) {
for (var i = 0; i < Cust_exist_id.length; i++) {
if (Cust_exist_id[i].customer_Id != scriptContext.newRecord.id) {
var cust_Multiselect = [];
for (var j = 0; j < Cust_exist_id.length; j++) {
if (Cust_exist_id[i].customer_Id != Cust_exist_id[j].customer_Id) {
if (Cust_exist_id[j].customer_leadsource == "-6") {
log.debug("Cust_exist_id[j].customer_leadsource", Cust_exist_id[j].customer_leadsource);
cust_Multiselect.push(Cust_exist_id[j].customer_Id);
}
}
}
log.debug("cust_Multiselect", cust_Multiselect);
if (Cust_exist_id[i].customer_stage = "CUSTOMER") {
var if_Rec = record.submitFields({
type: record.Type.CUSTOMER,
id: Cust_exist_id[i].customer_Id,
values: {
'custentity_jj_web_leadsprospectcustomer': cust_Multiselect
}
});
} else if (Cust_exist_id[i].customer_stage = "LEAD") {
var if_Rec = record.submitFields({
type: record.Type.LEAD,
id: Cust_exist_id[i].customer_Id,
values: {
'custentity_jj_web_leadsprospectcustomer': cust_Multiselect
}
});
} else if (Cust_exist_id[i].customer_stage = "PROSPECT") {
var if_Rec = record.submitFields({
type: record.Type.PROSPECT,
id: Cust_exist_id[i].customer_Id,
values: {
'custentity_jj_web_leadsprospectcustomer': cust_Multiselect
}
});
}
}
}
}
}
},
existing_Customers: function(id) {
var customer_id = "";
var item_Array = [];
var customerSearchObj = search.create({
type: "customer",
filters: [
["stage", "anyof", "CUSTOMER", "LEAD", "PROSPECT"],
"AND", ["email", "is", id]
],
columns: [
search.createColumn({
name: "entityid",
sort: search.Sort.ASC,
label: "Name"
}),
search.createColumn({
name: "email",
label: "Email"
}),
search.createColumn({
name: "internalid",
label: "Internal ID"
}),
search.createColumn({
name: "stage",
label: "Stage"
}),
search.createColumn({
name: "leadsource",
label: "Lead Source"
})
]
});
var searchResultCount = customerSearchObj.runPaged().count;
log.debug("customerSearchObj result count", searchResultCount);
customerSearchObj.run().each(function(result) {
var item = {};
item.customer_Id = main.checkForParameter(result.getValue(customerSearchObj.columns[2]));
item.customer_Name = main.checkForParameter(result.getValue(customerSearchObj.columns[0]));
item.customer_Eamil = main.checkForParameter(result.getValue(customerSearchObj.columns[1]));
item.customer_stage = main.checkForParameter(result.getValue(customerSearchObj.columns[3]));
item.customer_leadsource = main.checkForParameter(result.getValue(customerSearchObj.columns[4]));
item_Array.push(item);
return true;
});
log.debug("item_Array", item_Array);
return item_Array;
},
validate_param: function(parameter) {
if (parameter != "" && parameter !== null && parameter !== undefined && parameter !== false && parameter !== "null" && parameter !== "undefined" && parameter !== 'false' && parameter != " ")
return true;
else {
return false;
}
},
checkForParameter: function(parameter) {
if (parameter != "" && parameter !== null && parameter !== undefined && parameter !== false && parameter !== "null" && parameter !== "undefined" && parameter !== 'false' && parameter != " ") {
return parameter;
} else {
parameter = "";
return parameter;
}
}
}
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;
});