- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-01-2024 05:17 AM
Hello Everyone,
I have a requirement where I need to populate the Requested by manager with phone number as alert. I have written a client script and script include for that.
And this needs to be executed based on the drop down of variable in the record producer like
if(variable == 'dropdown choice') then written all the client script under it and put the alert just before the if. But am getting the alert as Manager and phone number undefined. And they are populating under the alert of the fucntion.
Can anyone help me in achieving this as in a single alert.
Client script:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-01-2024 12:04 PM
I see now that you are getting each value prior to the combined string. Are the alerts coming in the order expected - Manager name, Phone number, then the one with both? As a matter of clean coding standards, it is best to initialize variables when they are declared:
var manager_name = '';
var phone_number = '';
It would also be best, and a good habit to get into, to make one trip to the server and back. In this case, you can do this by combining the functions in the Script Include, and building an object to return both values. This is an excellent tool to have in your belt for such cases, and simplifies the execution and troubleshooting. Here's what the Client Script would look like:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (g_form.getValue('did_you_inform_your_direct_manager') == 'No') {
var req_user = g_form.getValue('requested_by');
//alert('requested_user is' + req_user);
var req = new GlideAjax('GetUserManager');
req.addParam('sysparm_name', 'getUserInfo');
req.addParam('sysparm_req_user', req_user);
req.getXML(getManager);
function getManager(response) {
var manager_name = '';
var phone_number = '';
var answer = JSON.parse(response.responseXML.documentElement.getAttribute('answer'));
manager_name = answer.user_name;
phone_number = answer.phone_num;
alert('Manager name is: ' + manager_name);
alert('Phone number is :' + phone_number);
alert('Manager is : ' + manager_name + 'Phone number is:' + phone_number);
}
}
And the Script Include:
var GetUserManager = Class.create();
GetUserManager.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getUserInfo: function() {
var user = this.getParameter('sysparm_req_user');
var userGR = new GlideRecord('sn_hr_core_profile');
userGR.addQuery('sys_id', user);
userGR.query();
if (userGR.next()) {
var mgr = userGR.u_hr_manager;
var phoneGR = new GlideRecord('sys_user');
phoneGR.addQuery('sys_id', mgr);
phoneGR.query();
if (phoneGR.next()) {
var results = {
"phone_num": phoneGR.getValue("phone"),
"user_name": userGR.u_hr_manager.getDisplayValue()
};
return JSON.stringify(results);
}
} else {
return null;
}
},
type: 'GetUserManager'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2024 05:00 AM
Great to hear, and you are welcome!