- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2024 08:22 AM
Hi,
I have this Script include and client script to fulfill the user mobile phone on a form on load.
var GetUserPhone = Class.create();
GetUserPhone.prototype = {
initialize: function() {},
getMobilePhone: function(userID) {
try {
var gr = new GlideRecord('sys_user');
if (gr.get(userID)) {
return gr.mobile_phone.toString();
}
} catch (error) {
gs.error("Error in GetUserPhone.getMobilePhone: " + error.message);
return ''; // Return empty in case of error
}
},
type: 'GetUserPhone'
};
Client script:
function onLoad() {
var userID = g_form.getValue('u_requested_for');
if (userID) {
alert('id found: ' + userID);
var ga = new GlideAjax('GetUserPhone');
ga.addParam('sysparm_name', 'getMobilePhone');
ga.addParam('sysparm_userID', userID);
ga.getXMLAnswer(handleResponse);
}
}
function handleResponse(response) {
var mobilePhone = response;
alert('Mobile phone found: ' + mobilePhone);
if (mobilePhone) {g_form.setValue('mobile_swt', mobilePhone);
} else {
g_form.clearValue('mobile_swt');
}
}
The mobilePhone is returning NULL in the alert, I confirmed that the user ID I selected (and its returning on the first alert) has a value on the field.
Can you find the issue?
Thank you in advance!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2024 11:40 PM
Hello @CátiaA
In the Script Include, use this.getParameter('sysparm_userID') to fetch the parameter sent from the client script.
So please make the required changes as following code then executes ..
Script include --
var GetUserPhone = Class.create();
GetUserPhone.prototype = {
initialize: function() {},
getMobilePhone: function() {
try {
var userID = this.getParameter('sysparm_userID'); // Correctly retrieve the user ID parameter
var gr = new GlideRecord('sys_user');
if (gr.get(userID.toString())) {
return gr.mobile_phone.toString();
}
} catch (error) {
gs.error("Error in GetUserPhone.getMobilePhone: " + error.message);
return ''; // Return empty in case of error
}
return ''; // Return empty if no record is found
},
type: 'GetUserPhone'
};
Client Script --
function onLoad() {
var userID = g_form.getValue('u_requested_for');
if (userID) {
alert('ID found: ' + userID);
var ga = new GlideAjax('GetUserPhone');
ga.addParam('sysparm_name', 'getMobilePhone');
ga.addParam('sysparm_userID', userID); // Ensure this matches the Script Include parameter
ga.getXMLAnswer(handleResponse);
}
}
function handleResponse(response) {
var mobilePhone = response;
alert('Mobile phone found: ' + mobilePhone);
if (mobilePhone) {
g_form.setValue('mobile_swt', mobilePhone);
} else {
g_form.clearValue('mobile_swt');
}
}
If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Thank You
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2024 12:10 AM
Hello @CátiaA ,
Please give a try to the updated script below and see how it works for you.
function onLoad() {
var userID = g_form.getValue('u_requested_for');
if (userID) {
alert('ID found: ' + userID);
var ga = new GlideAjax('GetUserPhone');
ga.addParam('sysparm_name', 'getMobilePhone');
ga.addParam('sysparm_userID', userID);
ga.getXMLAnswer(handleResponse);
}
}
function handleResponse(response) {
var mobilePhone = response.responseXML.documentElement.getAttribute('answer');
alert('Mobile phone found: ' + mobilePhone);
if (mobilePhone) {
g_form.setValue('mobile_swt', mobilePhone);
} else {
g_form.clearValue('mobile_swt');
}
}