Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to fetch user location and country

Hari1
Mega Sage

Hi,

I have created a catalog and I am trying to fetch the user details like employee manager, location, country, department.

find_real_file.png

I am using the below script.

Script Include:

var data, obj = {};
		var countryName;
		var userID = this.getParameter('sysparm_user_id');
		var gr = new GlideRecord('sys_user');
		gr.addQuery('sys_id',userID);
		gr.query();
		if(gr.next()){
			
			var json = new JSON();
			countryName = gr.location.country;
			
			gs.log("countryName: " + countryName);
			
			var results = {
				"manager": gr.getValue("manager"),
				"department": gr.getValue("department"),
				"location": gr.location.toString(),
				"country": countryName, //gr.getValue("country"),
				"title": gr.getValue("title")
                 };
			gs.log("Location: " + gr.department + " " + gr.location);
			gs.log("Country:" + JSON.stringify(results.country));
			gs.log("Results" + JSON.stringify(results));
            return json.encode(results);
		}

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

	//Ajax Request for Script Include
	
	var ajax = new GlideAjax('GetUserDetails');
	ajax.addParam("sysparm_name", "RequestUserDetails");
	ajax.addParam('sysparm_user_id', newValue); 
	ajax.getXML(returnResponse);
			
	function returnResponse(response) 
	{
		var answer = response.responseXML.documentElement.getAttribute("answer");
		if (answer) 
		{
			var returneddata = answer.evalJSON(true);
			
			g_form.setValue('employeeManager', returneddata.manager );
			g_form.setValue('department', returneddata.dept );
			g_form.setValue('location', returneddata.loc );
			g_form.setValue('country', returneddata.country );
			g_form.setValue('title', returneddata.title );	
		} 
	}
}

 

I am getting null and Object values in return. Any help! Thanks.

1 ACCEPTED SOLUTION


This is the country field on the location field.  Since location is a reference field you can dot-walk it.  I would change the assignment in the object to this.

"country": gr.location.country.toString(),

 

View solution in original post

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

Hi Hemanth,

Are you getting expected values in all of your SI logs? Try this syntax in the returnResponse - I also corrected 2 of your object names that were different than the SI - department and location.

function returnResponse(response){
  var answer = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
  if (answer){
    g_form.setValue('employeeManager', answer.manager);
    g_form.setValue('department', answer.department);
    g_form.setValue('location', answer.location);
    g_form.setValue('country', answer.country);
    g_form.setValue('title', answer.title);	
  } 
}

 

Hi Brad,

Thanks, I am able to get the department and location.

We have a field on the user table Country

find_real_file.png

But I am not able to find it on the user table. I see the value in the log. When i try to get the country using gr.location.country;  but when i pass it to the object i see it to blank for some reason for which I am not sure of. Please find the below log.

 

find_real_file.png


This is the country field on the location field.  Since location is a reference field you can dot-walk it.  I would change the assignment in the object to this.

"country": gr.location.country.toString(),