
- 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-08-2019 02:30 AM
Hi Sir Harshvardhan,
First, I want to thank you for the script, it is now auto populating the user details using just one script. Thankyou!
but unfortunately the Country field is still not being populated... and there is an error occured.
Maybe you can help me with this one too. Your help will be much appreciated. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2019 03:14 AM
in your script include prototype can you please update below line.
replace the below line
userdetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
to
userdetails.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
now coming back to the country population. can you confirm the user you are validating which has the country mentioned in his location .

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2019 04:10 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2019 04:13 AM
just confirming here, the country field you have a reference type variable. can you make it to single line text and see if it's setting or not.
because location country field is string .

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2019 04:25 AM