- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2021 07:25 AM
Hi All,
I am populating Manager of user in opened_by field using script include and Glide Ajax. The opened_by field has logged in user by default. So I want it to work on both onLoad and onChange. The thing here is, for suppose user in opened_by field has no manager, and If I enter it manually and then save the form. The Value is getting cleared once the form is loaded. How to prevent it?
Script Include:
var userDetails = Class.create();
userDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
managerandtitle: function() {
var manager = '';
var title = '';
var user1 = this.getParameter('sysparm_user');
var gr1 = new GlideRecord('sys_user');
gr1.addQuery('sys_id', user1);
gr1.query();
if (gr1.next()) {
manager = gr1.manager;
title = gr1.title;
}
}
return manager + ',' + title;
},
type: 'userDetails'
});
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '') {
retrun;
}
var ga1 = new GlideAjax('userDetails');
ga1.addParam('sysparm_name', 'managerandtitle');
ga1.addParam('sysparm_user', newValue);
ga1.getXML(user_info);
function user_info(response) {
var ans = response.responseXML.documentElement.getAttribute('answer');
var ans1 = ans.split(",");
g_form.setValue('u_manager', ans1[0]);
g_form.setValue('u_title', ans1[1]);
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2021 01:52 AM
Hi Ajay,
To keep it simple and easy
I would suggest this
1) Keep default value in both title and manager so that if logged in user has Manager and Title then it auto-populates
2) Then ensure your onChange client script doesn't run on load and the Manager + Title is cleared when User is cleared
Default value for manager:
javascript: gs.getUser().getManagerID();
Default value for title field
javascript: gs.getUser().getRecord().getValue('title');
Updated Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
// if new value is cleared then clear manager and title both
if(newValue == ''){
g_form.clearValue('u_manager');
g_form.clearValue('u_title');
}
else{
var ga1 = new GlideAjax('userDetails');
ga1.addParam('sysparm_name', 'managerandtitle');
ga1.addParam('sysparm_user', newValue);
ga1.getXML(user_info);
function user_info(response) {
var ans = response.responseXML.documentElement.getAttribute('answer');
var ans1 = ans.split(",");
g_form.setValue('u_manager', ans1[0]);
g_form.setValue('u_title', ans1[1]);
}
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2021 12:01 AM
you should use different script for different use, create onload script to populate data onload of form and create onchange script for onchange of field. It will solve your most of the problems.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2021 01:41 AM
Hi
Sorry for this I have tested now and the script you updated is not working.
If I write onLoad and Onchange script differently,
For suppose, opened_by user has no manager, so when onLoad script runs, it will result null value in manager field. If I enter manager name manually, and save the form, the onLoad script will run again right? and It clears the value right as opened_by user not having manager.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2021 01:53 AM
Hi Ajay,
please check my above comment to keep it easy and simple
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2021 07:32 AM