Fetch the location of logged in User

Rutuja1
Kilo Contributor

I have created catalog Item. There is a variable "Current Location".I want to fetch the location of logged in user and want to auto-populate that. I've tried almost all the ways but not able to fetch that.

In variable's default value == javascript:gs.getUser().getLocation();

this didn't worked. So I've written a client script  'on Load'

function onLoad() {
//Type appropriate comment here, and begin script below
var id = g_form.getValue('user_name'); //variable name

var user = new GlideRecord('sys_user');
user.addQuery('sys_id',id);
user.query();
if ( user.next() )
{

g_form.setValue('current_location', user.location);

}

}

But this code is also not working.

 

Then I had tried using Script Include. And my script include is as follow:

requestor_details.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
requestor_info: function() {
var result = this.newItem("result");

var logged_user = gs.getUserID();
var user_detail = new GlideRecord('sys_user');
user_detail.addQuery('sys_id',logged_user);
user_detail.query();
while(user_detail.next())
{
/result.setAttribute("employee_id",user_detail.getDisplayValue('sys_id'));
//result.setAttribute("manager_name", user_detail.getDisplayValue('manager'));
//result.setAttribute("second_level_manager",user_detail.getDisplayValue('u_l2_manager'));
//result.setAttribute("hr_manager",user_detail.getDisplayValue('u_hr_managr'));
result.setAttribute("current.location",user_detail.getDisplayValue('location'));
}


},


type: 'requestor_details'

});

 

And I've called that script include in the below Client Script,

function onLoad() {

var ga = new GlideAjax("requestor_details");
ga.addParam("sysparm_name", "requestor_info");
ga.getXML(ajaxResponse);

 

function ajaxResponse(serverResponse) {
var result = serverResponse.responseXML.getElementsByTagName("result");
// var user = result[0].getAttribute("employee_id");
// var mgr = result[0].getAttribute("manager_name");
//var l2mgr = result[0].getAttribute("second_level_manager");
var loc = result[0].getAttribute("current_location");
//alert(user+mgr);
// g_form.setValue('employee_id', user);
// g_form.setValue('manager_name', mgr);
// g_form.setValue('second_level_manager', l2mgr);
g_form.setValue('current_location', loc);
}
}

 

 

This is also not working.

1 ACCEPTED SOLUTION

Gaurav Shirsat
Mega Sage

Hi Rutuja

In Addition to Megha,I would Like to add some my views to this.

 

If you are using display value, then you will get location's name Only.because display value will give you only that.

If you are looking for the country only, then you might want to change the script include to point out the country and return it.

You can use this to get the users location.

function myLic(){

var gr = new GlideRecord("sys_user");//User Table 

gr.get(gs.getUserID());

var country=gr.location.country;

 return country;

}

Please Mark Correct and Helpful

Thanks and Regards

Gaurav Shirsat

View solution in original post

15 REPLIES 15

Hello Rutuja 

Did your resolved from the answer,If yes please mark it as correct

Regards

Yash Agrawal

NO. Not yet resolved

Hello Rutuja,

What type of "Current Location" variable is?

it is reference or String.

Please reply so that i can share the exact code with you

Regards

Yash Agrawal

Current_Location is the reference type of cmn_location. But I had resolved this by changing the scope to Global. And now working fine.

But I dont know how to do this without changing the scope.

 

Megha Padale
Giga Guru

Hi Rutuja,

Check below links, it might help you

https://community.servicenow.com/community?id=community_question&sys_id=9a65287ddb54b3c413b5fb243996...

https://community.servicenow.com/community?id=community_question&sys_id=c44d54efdb923700fff8a345ca96...

If my answer helped you in any way, mark answer as helpful and correct.

Thanks and regards,

Megha