Auto Populate Country field based on selected user in reference field

Jehiellb
Giga Contributor

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 ...

 

find_real_file.png

 

find_real_file.png

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!

1 ACCEPTED SOLUTION

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'
	});

 

 

 

 

View solution in original post

13 REPLIES 13

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. 

 

find_real_file.png

 

Maybe you can help me with this one too. Your help will be much appreciated. Thanks!

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 . 

 

find_real_file.png

Is this what you ask? I hope I get your question right. 

 

Your time and effort helping me is much appreciated. Thanks!

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 . 

Good News it is now populated, but when I change the user the country is remains as USA and not changing based on user country

find_real_file.png