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

MrMuhammad
Giga Sage

Hello Rutuja - user_name is reference field or a string field? 

Check this https://community.servicenow.com/community?id=community_article&sys_id=53fa4b0adbd3cc1014d6fb2439961...

Regards,
Muhammad

user_name is a reference field of sys_user and current_location is reffering to cmn_location.

 

See the above article that has step by step implementation process. Let me know if you face any issues.

Regards,
Muhammad

Jaspal Singh
Mega Patron
Mega Patron

Hi Rutuja,

 

Can you pass default value as below.

javascript: gs.getUser().getRecord().getValue('location');

Will look for the script include as well once.