Jira Code: TM 67
Show the existing lead, prospect searchable list, this should be added in the central tab, only TM 37 sales person role can see this, the user cannot edit it. Newly created lead, the prospect should be highlighted somehow. The salesperson can see only the leads & prospects coming under her/him.
Suitelet
/**
* @NApiVersion 2.x
* @NScriptType Suitelet
* @NModuleScope SameAccount
* @Script TM-67 JJ SL Suitelet For Prospects
* @ScriptId customscript_tm_67_jj_sl_search
* @Dependencies TM-67 SL JJ Search for Exist Prospects, TM-67 JJ CS Search Ext
* Prospects.js
* @Description To search the existing customers for the custom sales center
* @Date 19 February 2018
*/
define([ 'N/record', 'N/search' ],
/**
* @param {record}
* record
* @param {search}
* search
*/
function(record, search) {
/**
* Definition of the Suitelet script trigger point.
*
* @param {Object}
* context
* @param {ServerRequest}
* context.request - Encapsulation of the incoming request
* @param {ServerResponse}
* context.response - Encapsulation of the Suitelet response
* @Since 2015.2
*/
function onRequest(context) {
try {
var company = context.request.parameters.custscript_company;
if (context.request.method == 'POST') {
var companyData = [];
var myPostDataObj = {};
// search for existing customers with key words
var customerSearchObj = search
.create({
type : "customer",
filters : [
[ "stage", "anyof",
"PROSPECT" ], "AND",
[ "companyname", "contains", company ] ],
columns : [ search.createColumn({
name : "entityid",
sort : search.Sort.ASC
}), "salesrep", "companyname", "lastsaledate" ]
});
var searchResultCount = customerSearchObj.runPaged().count;
customerSearchObj.run().each(function(result) {
var extCompName = result.getValue({
name : 'companyname'
});
var extSalesRep = result.getText({
name : 'salesrep'
});
var extCusSaleActivity = result.getValue({
name : 'lastsaledate'
});
var myPostDataObj = {};
myPostDataObj.extCompName = extCompName;
myPostDataObj.extSalesRep = extSalesRep;
myPostDataObj.extCusSaleActivity = extCusSaleActivity;
// cart.push({element: element});
companyData.push(myPostDataObj);
// alert(extSalesRep);
// .run().each has a limit of 4,000 results
return true;
});
var companyData = JSON.stringify(companyData);
log.debug({
title : 'companyDatass',
details : companyData
});
context.response.write(companyData);
// End of search for existing customers with key words
}
} catch (e) {
log.debug({
title : 'search customer',
details : e.message
});
}
}
return {
onRequest : onRequest
};
});
Backend suitelet
/**
* @NApiVersion 2.x
* @NScriptType Suitelet
* @NModuleScope SameAccount
* @Script TM-67 SL JJ Search for Exist Prospects
* @ScriptId customscript_tm_67_jj_sl_prospect_search
* @Dependencies TM-67 JJ SL BE Suitelet For Customers, TM-58 JJ CS Search Ext
* Customer.js
* @Description To create the suitelet page to search the existing customers for
* the custom sales center
* @Date 19 February 2018
*/
define([ 'N/record', 'N/search', 'N/ui/serverWidget' ],
/**
* @param {record}
* record
* @param {search}
* search
* @param {serverWidget}
* serverWidget
*/
function(record, search, serverWidget) {
/**
* Definition of the Suitelet script trigger point.
*
* @param {Object}
* context
* @param {ServerRequest}
* context.request - Encapsulation of the incoming request
* @param {ServerResponse}
* context.response - Encapsulation of the Suitelet response
* @Since 2015.2
*/
var CLIENT_SCRIPT_FILE_ID = 1012;
function onRequest(context) {
if (context.request.method == 'GET') {
try {
var form = serverWidget.createForm({
title : 'Prospects Search'
});
form.clientScriptFileId = CLIENT_SCRIPT_FILE_ID;
var field = form.addField({
id : 'custpage_textfield',
type : serverWidget.FieldType.TEXT,
label : 'Company Name'
});
var button = form.addButton({
id : 'custpage_searchbutton',
label : 'Search',
functionName : 'searchExistProspects()'
});
var sublist = form.addSublist({
id : 'custpage_prospectstable',
type : serverWidget.SublistType.INLINEEDITOR,
label : 'Existing Prospects'
});
// Add columns to be shown on Page
sublist.addField({
id : 'custpage_compname',
label : 'Company Name',
type : serverWidget.FieldType.TEXT
});
sublist.addField({
id : 'custpage_salesrep',
label : 'Sales Rep',
type : serverWidget.FieldType.TEXT
});
sublist.addField({
id : 'custpage_lastsaledate',
label : 'Last Sales Activity',
type : serverWidget.FieldType.TEXT
});
context.response.writePage(form);
} catch (e) {
log.debug({
title : 'form for search',
details : e.message
});
}
}
}
return {
onRequest : onRequest
};
});
Client script
/**
* JS file Name :- TM-67 JJ CS Search Ext Prospects.js FOLDER:- SuiteScripts :
* JJSuiteScripts : TM67 Ext Prospects DEPENDENCIES:- TM-67 SL JJ Search for Exist
* Prospects, TM-67 JJ SL BE Suitelet For Prospects DESCRIPTIN: Used to add and
* remove lines in the sublist in the existing Prospects search for sales center
*
*/
define(
[ 'N/record', 'N/search', 'N/currentRecord', 'N/url', 'N/https','N/ui/dialog' ],
/**
* @param {record}
* record
* @param {search}
* search
*/
function(record, search, currentRecord, url, https,dialog) {
/**
* Function to be executed after page is initialized.
*
* @param {Object}
* scriptContext
* @param {Record}
* scriptContext.currentRecord - Current form record
* @param {string}
* scriptContext.mode - The mode in which the record is
* being accessed (create, copy, or edit)
*
* @since 2015.2
*/
function searchExistProspects(scriptContext) {
try {
var records = currentRecord.get();
var custscript_company = records.getValue({
fieldId : 'custpage_textfield'
});
if(custscript_company=="")
{
dialog.alert({
title: 'Alert',
message: 'You must enter a company name.'
});
}
var numLines = records.getLineCount({
sublistId : 'custpage_prospectstable'
});
for (var j = numLines - 1; j >= 0; j--) {
records.removeLine({
sublistId : 'custpage_prospectstable',
line : j,
ignoreRecalc : true
});
}
var output = url.resolveScript({
scriptId : 'customscript_tm_67_jj_sl_search_prospect',
deploymentId : 'customdeploy_tm_67_jj_sl_search_prospect',
returnExternalUrl : true,
});
var response = https
.post({
url : "https://system.na1.netsuite.com/app/site/hosting/scriptlet.nl?script=73&deploy=1",
body : {
'custscript_company' : custscript_company
}
});
var result = response.body;
result = JSON.parse(result);
var resultLength=result.length;
if(resultLength==0)
{
dialog.alert({
title: 'Alert',
message: 'No matching search results.'
});
}
var extCompName;
for (var i = 0; i <= result.length; i++) {
var resultobc = result[i];
extCompName = resultobc.extCompName;
extSalesRep = resultobc.extSalesRep;
extCusSaleActivity = resultobc.extCusSaleActivity;
records.selectNewLine({
sublistId : 'custpage_prospectstable'
});
records.setCurrentSublistValue({
sublistId : 'custpage_prospectstable',
fieldId : 'custpage_compname',
value : extCompName,
ignoreFieldChange : true,
fireSlavingSync : false
});
records.setCurrentSublistValue({
sublistId : 'custpage_prospectstable',
fieldId : 'custpage_salesrep',
value : extSalesRep,
ignoreFieldChange : true,
fireSlavingSync : false
});
records.setCurrentSublistValue({
sublistId : 'custpage_prospectstable',
fieldId : 'custpage_lastsaledate',
value : extCusSaleActivity,
ignoreFieldChange : true,
fireSlavingSync : false
});
records.commitLine({
sublistId : 'custpage_prospectstable'
});
}
return true;
} catch (e) {
log.debug({
title : 'search',
details : e.message
});
}
}
return {
searchExistProspects : searchExistProspects
};
});