- 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-17-2021 08:05 AM
Give something like this a try; exit your function if the record is not a new record.
function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '' || !g_form.isNewRecord()) {
return;
}
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]);
}
}
I hope this helps!
If this was helpful or correct, please be kind and remember to click appropriately!
Michael Jones - Proud member of the CloudPires team!
Michael D. Jones
Proud member of the GlideFast Consulting Team!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2021 08:14 AM
Hi Ajay,
There are two things you can do here.
1. Try adding below code in your logic
if (isLoading || newValue === '') {
if(newValue == '' && u_manager!='')
return;
}
2. Put an alert to see if the manager field is populated with correct value or not.
you can keep it after your below code
g_form.setValue('u_manager', ans1[0]);
g_form.setValue('u_title', ans1[1]);
alert('Manager is '+u_manager); // This will help you understand once your script include completed the server call then only you are updating the manager value manually.
Please mark this answer as correct and helpful if it resolves the query and helpful alone if it lead you in right direction.
Thanks,
Mohit Kaushik
Mohit Kaushik
ServiceNow MVP (2023-2025)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2021 08:28 AM
Hi
I have made changes as you said, but the value is still getting cleared and also not getting the alert.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2021 09:36 AM
try this
function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '') {
return;
}
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(",");
if(ans1[0]!='')
g_form.setValue('u_manager', ans1[0]);
if(ans1[1]!='')
g_form.setValue('u_title', ans1[1]);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2021 10:14 PM
Hi
Thank you it is working good. But I have a doubt here like I have a record number field on the form where users are allowed to enter the number which is already present in closed records of same table. When I enter a number in that field, it is populating some fields automatically, same as the values in the record where that number is present (used Include and GlideAjax onChange). Now in the auto populated fields, if user changes any value, and saves the form it is populating the original Value but not the changed one. How to prevent those values from changing to original. I need the values that user changed to.