
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2019 06:56 AM
Hi ServiceNow Community,
I need some help with my Country field, I want it to be auto populated based on selected user from my reference field, here is my form ...
Is there any way to get this? and how? Is it Client Script? Maybe you can also help me with scripting or is it Ui Policy?
Your help will be much appreciated. Thanks!
Solved! Go to Solution.
- Labels:
-
Best Practices
-
Service Catalog

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2019 01:49 AM
why are using two client script here? if you wanna auto populate "Open on behalf of this user" details in other fields then one client script can solve this issue.
let me tell you why your script will not work here, you have dot walked more than one in call back function that's the reason user country did not populate. eg: caller.location.country ( this will not work)
as @mike has written the glide ajax try that way. In script include you can get other field values and then you can add them in your client script.
Updated Script:
Catalog Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var user = g_form.getValue('u_open_on_behalf'); //make sure about the variable.
var ga = new GlideAjax('userdetails');
ga.addParam('sysparm_name','requestor_info');
ga.addParam('sysparm_user_name',user);
ga.getXML(HelloWorldParse);
function HelloWorldParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var ans = answer.evalJSON();
for (var k = 0; k < ans.length; k++) {
alert('Phone Number is ' +ans[k].phone);
g_form.setValue('phone_number',ans[k].phone);
g_form.setValue('employee_location',ans[k].location);
g_form.setValue('employee_id',ans[k].emp);
g_form.setValue('employee_supervison',ans[k].manager);
g_form.setValue('country ',ans[k].country_code);
}
}
}
Script Include: Make sure the script include should be client callable.
var userdetails = Class.create();
userdetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
requestor_info: function() {
var arr=[];
var output='';
var json = new JSON();
var details=this.getParameter('sysparm_user_name');
var user= new GlideRecord('sys_user');
user.addQuery('sys_id',details);
user.query();
while(user.next())
{
var obj = {};
obj.phone=user.mobile_phone.getDisplayValue();
obj.location=user.location.getDisplayValue();
obj.emp=user.emplpyee_number.getDisplayValue();
obj.manager=user.manager.toString();
obj.country_code=user.location.country.toString();
arr.push(obj);
}
gs.log('value is :'+ json.encode(arr));
return (json.encode(arr));
},
type: 'userdetails'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2019 07:02 AM
You can create an onChange script for the "Open on behalf of this user":
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Change the values below to match your field names.
g_form.setValue("country_field", user_field.location.country)
}
Please mark this as helpful/correct if it resolved your issue.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2019 07:21 AM
You can create onChange client script on open on be half of user variable
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('u_userInfoAjax');
ga.addParam('sysparm_name', "getUserCountry");
ga.addParam('sysparm_user', newValue);
ga.getXMLAnswer(function(answer){
var response = JSON.parse(answer);
g_form.setValue("yourcountryvariable", response.cou);
});
}
Script Includes: (Make sure client callable is checked)
var u_userInfoAjax = Class.create();
u_userInfoAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserCountry: function(){
var user = this.getParameter('sysparm_user');
var gr = new GlideRecord("sys_user");
gr.get(user);
var cou = gr.location.country;
var country;
var jr = new GlideRecord("core_country");
jr.addQuery("name", cou);
jr.query();
if (jr.next()) {
country = jr.getValue('sys_id');
}else{
country = '';
}
var response = {};
response.cou = country;
return JSON.stringify(response);
},
type: 'u_userInfoAjax'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2019 12:31 AM
Hi Sir Mike Patel,
I have tried your code but still the country field is not being populated...
Is it because I have this client script on my Open on behalf of this user variable. This client script is suppose to auto populate the other user information. Does it affect the other one, the one that you provided? Is it possible to have a two Catalog Client Script in one variable?
Your reponse will be much appreciated. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2019 01:49 AM
why are using two client script here? if you wanna auto populate "Open on behalf of this user" details in other fields then one client script can solve this issue.
let me tell you why your script will not work here, you have dot walked more than one in call back function that's the reason user country did not populate. eg: caller.location.country ( this will not work)
as @mike has written the glide ajax try that way. In script include you can get other field values and then you can add them in your client script.
Updated Script:
Catalog Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var user = g_form.getValue('u_open_on_behalf'); //make sure about the variable.
var ga = new GlideAjax('userdetails');
ga.addParam('sysparm_name','requestor_info');
ga.addParam('sysparm_user_name',user);
ga.getXML(HelloWorldParse);
function HelloWorldParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var ans = answer.evalJSON();
for (var k = 0; k < ans.length; k++) {
alert('Phone Number is ' +ans[k].phone);
g_form.setValue('phone_number',ans[k].phone);
g_form.setValue('employee_location',ans[k].location);
g_form.setValue('employee_id',ans[k].emp);
g_form.setValue('employee_supervison',ans[k].manager);
g_form.setValue('country ',ans[k].country_code);
}
}
}
Script Include: Make sure the script include should be client callable.
var userdetails = Class.create();
userdetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
requestor_info: function() {
var arr=[];
var output='';
var json = new JSON();
var details=this.getParameter('sysparm_user_name');
var user= new GlideRecord('sys_user');
user.addQuery('sys_id',details);
user.query();
while(user.next())
{
var obj = {};
obj.phone=user.mobile_phone.getDisplayValue();
obj.location=user.location.getDisplayValue();
obj.emp=user.emplpyee_number.getDisplayValue();
obj.manager=user.manager.toString();
obj.country_code=user.location.country.toString();
arr.push(obj);
}
gs.log('value is :'+ json.encode(arr));
return (json.encode(arr));
},
type: 'userdetails'
});