Identifying user by phone passed through URL
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-05-2025 11:11 AM
Hello friends,
We are trying to implement something to our outsourced Service Desk provider, when a user calls the Service Desk, their phone system would send us a URL with some parameters we are going to parse and populate the incident form.
For this I'm using a client script:
(function onLoad() {
// Get full query string
var rawSearch = top.location.search || '';
// Some URLs may have parameters URL-encoded as part of a single sys_id or value
// Example: ?sys_id=-1%26conversationID%3D123%26callerEmail%3Dtest%40email.com
if (rawSearch.includes('%26')) {
// Decode once to extract the embedded parameters
rawSearch = '?' + decodeURIComponent(rawSearch.replace(/^\?/, ''));
}
var params = new URLSearchParams(rawSearch);
var callId = params.get('conversationID') || '';
var email = params.get('callerEmail') || '';
var phone = params.get('callerPhoneNumber') || '';
var grp = params.get('group') || '';
if (callId) g_form.setValue('u_twilio_phone_call_links', callId);
if (grp) g_form.setValue('assignment_group', grp);
if (phone) {
g_form.setValue('contact_type', 'Phone');
} else if (email) {
g_form.setValue('contact_type', 'Chat');
}
if (!email && !phone) return;
var ga = new GlideAjax('GetUserByEmailOrPhone');
var method = '';
if (email) {
method = 'getUserSysIdByEmail';
ga.addParam('sysparm_email', email);
console.log("Found email: " + email);
} else if (phone) {
method = 'getUserSysIdByPhone';
ga.addParam('sysparm_phone', phone);
console.log("Found Phone: " + phone);
} else {
return;
}
ga.addParam('sysparm_name', method);
console.log("Using method: " + method);
ga.getXMLAnswer(function(userSysId) {
if (userSysId) {
g_form.setValue('caller_id', userSysId);
console.log("Trying to find user: " + userSysId);
}
});
})();
Which uses this script include:
var GetUserByEmailOrPhone = Class.create();
GetUserByEmailOrPhone.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserSysIdByEmail: function() {
var email = (this.getParameter('sysparm_email') || '').trim();
return this._findUserByField('email', email);
},
getUserSysIdByPhone: function() {
var inputPhone = (this.getParameter('sysparm_phone') || '').trim();
var normalizedInput = this._last10Digits(inputPhone);
var debug = [];
debug.push('Incoming: ' + inputPhone);
debug.push('Normalized: ' + normalizedInput);
if (!normalizedInput) {
debug.push('No valid input');
this.answer = JSON.stringify({
result: '',
debug: debug
});
return;
}
var userGR = new GlideRecord('sys_user');
userGR.addActiveQuery();
userGR.query();
while (userGR.next()) {
var mobile = this._last10Digits(userGR.getValue('mobile_phone'));
var business = this._last10Digits(userGR.getValue('phone'));
var home = this._last10Digits(userGR.getValue('home_phone'));
debug.push('--- Checking user: ' + userGR.getDisplayValue());
debug.push('Mobile: ' + mobile);
debug.push('Business: ' + business);
debug.push('Home: ' + home);
if (normalizedInput === mobile || normalizedInput === business || normalizedInput === home) {
debug.push('Match found: ' + userGR.getUniqueValue());
this.answer = JSON.stringify({
result: userGR.getUniqueValue(),
debug: debug
});
return;
}
}
debug.push('No match found.');
this.answer = JSON.stringify({
result: '',
debug: debug
});
},
_last10Digits: function(phone) {
var digitsOnly = (phone || '').replace(/\D/g, '');
return digitsOnly.length >= 10 ? digitsOnly.slice(-10) : '';
},
_findUserByField: function(field, value) {
if (!value) return '';
var userGR = new GlideRecord('sys_user');
userGR.addActiveQuery();
userGR.addQuery(field, value);
userGR.query();
return userGR.next() ? userGR.getUniqueValue() : '';
}
});
It's a little messy because I was trying to get the JSON output from
getUserSysIdByPhone into the client script but not working either.
I reverted the Client Script back to what is working for email but not for phone.
Any idea how to make this thing to work for phone as well, mainly using the same function
I reverted the Client Script back to what is working for email but not for phone.
Any idea how to make this thing to work for phone as well, mainly using the same function
_findUserByField?
0 REPLIES 0