- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2020 03:16 AM
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.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2020 01:14 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2020 03:30 AM
Thank you for the solution but javascript: gs.getUser().getRecord().getValue('location'); is not working

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2020 03:33 AM
Strange. Can you check if the Logged in User has valide Location value in User table once.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2020 03:26 AM
Hello Rutuja,
In the refrence specification tab of variabe use gs.getUser().getLocation(); this will help you get the location of logged in user.
Keep posted if you want more help
and
Please Mark it helpful/correct if my answer helps in any way to resolve
your query.
Reach out to me if any more help required.
Regards
Yash.K.Agrawal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2020 03:32 AM
I did this but not working

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2020 03:54 AM
Hello Rutuja,
Please try the below code.
Your refernce qualifier is javascript:new UserObjectUtils().getCountry()
Script include--UserObjectUtils
function in script include--getCountry
-----------------------------------------------------
Script include name : UserObjectUtils
var UserObjectUtils = Class.create();
UserObjectUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
var user = new GlideRecord("sys_user");
user.addQuery("sys_id", gs.getUserID());// gs.getUserID() fetch logged in user
user.setLimit(1)
user.query();
if (user.next()) {
return user.location.country;// return country of location.
}
},
type: 'UserObjectUtils'
});
This will definitely help you.
-------------------------------------------------------------------------
2nd solution
To auto populate the Applicant's Name field with logged in user - While defining the variable, set the default value as javascript:gs.getUserID(); ------- This will give you the current logged in user.
To auto populate the location field of the current user with onload action.
write an onLoad script:
var id = g_form.getValue('applicant_name'); //variable name
var user = new GlideRecord('sys_user');
user.addQuery('sys_id',id);
user.query();
if ( user.next() ) {
g_form.setValue('location', user.location);
g_form.getValue('requestor_manager', user.manager);
g_form.setValue('phone', user.mobile_phone); //to set the phone number.
g_form.setValue('emp_number', user.employee_number); // to set the employee number.
}
-------------------------------------------------------------------
3rd solution
Try this :- gs.getUser(current.variables.new_hire_name).getDisplayValue("location").
All this will work
Please Mark it helpful/correct if my answer helps in any way to resolve
your query.
Reach out to me if any more help required.
Regards
Yash.K.Agrawal