Skip to content

Instantly share code, notes, and snippets.

@taf2
Last active September 30, 2022 13:33
Show Gist options
  • Save taf2/3b4415c69052f615df81ce4ddf2e9d8f to your computer and use it in GitHub Desktop.
Save taf2/3b4415c69052f615df81ce4ddf2e9d8f to your computer and use it in GitHub Desktop.
Query Salesforce Used in a picker to select an object in salesforce by user input e.g. phone number or name
const jsforce = require('jsforce');
function phoneQuery(number, objects) {
let digits = number.replace(/\D/g,'').split("");
if (digits.length > 10) {
let country_code = digits.shift();
}
let npa = [];
npa.push(digits.shift());
npa.push(digits.shift());
npa.push(digits.shift());
npa = npa.join("");
let nxx = [];
nxx.push(digits.shift());
nxx.push(digits.shift());
nxx.push(digits.shift());
nxx = nxx.join("");
let lnumber = digits.join("");
//console.log("npa: " + npa + ", nxx:" + nxx + ", lnum:" + lnumber);
const search_number = [npa, nxx, lnumber].join("*");
return `FIND {${search_number}} IN PHONE FIELDS RETURNING ${objects}`;
}
exports.handler = function(event, context, callback) {
const conn = new jsforce.Connection({
instanceUrl : event.salesforce.instanceUrl,
accessToken : event.salesforce.token
});
const objects = process.env.Object.split(",").map ( (o) => {
return `${o}(Id,Name,Email)`
}).join(',');
const callId = event.activity.id;
// the pickers for custom fields will pass an options.input as the user input for the search
let searchQuery = event.options.input;
let sosl = null;
if (searchQuery && searchQuery.replace(/\D/g,'').length > 8) {
// removing all format characters the input is long enough to be a phone number try querying for numbers
sosl = phoneQuery(searchQuery.replace(/\D/g,''), objects)
} else {
// maybe we're looking for a name or maybe they did not provide us with any inputs
if (searchQuery && searchQuery.length) {
// ok maybe they want us to search by name
sosl = `FIND {${searchQuery}} IN NAME FIELDS RETURNING ${objects}`;
} else {
// ok we're doing a default query by phone number
let number = event.activity.caller_number;// convert to a searchable string
sosl = phoneQuery(number.replace(/\D/g,''), objects)
}
}
const changeHandler = function scriptHandler() {
console.log("is the value:", value);
if (window.sforce && window.sforce.opencti && typeof(sforce.opencti.screenPop) == 'function' && value.length > 3) {
// hook a screenPop event for salesforce lightning
sforce.opencti.screenPop({
type: sforce.opencti.SCREENPOP_TYPE.SOBJECT, //Review the arguments section.
params: {recordId: value }//Depends on the SCREENPOP_TYPE. Review the arguments section.
});
}
}.toString();
conn.search(sosl,
function(err, res) {
if (err) { console.log("error:", err); context.done(); return; }
//console.log("call id:" + callId);
const results = res.searchRecords.map( (r) => {
//console.log(r);
// return an object with the following:
// text: information that is displayed to the agent about the selection
// id: an internal reference id for the object being displayed
return {text: `<i>${r.attributes.type}</i>- ${r.Name} <small>${r.Email}</small>`, id: r.Id };
});
console.log(results);
if (!results.length) {
// add 1 empty result so enduser can always type another search query
results.push({text: "no matches", id: null});
}
context.done(null, { results: results, onChangeScript: changeHandler });
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment