Get logged in User's country

sreedharkaliset
Mega Expert

Hi,

I am able to get the location of logged in User but I am not able to get the country. Please help.

gs.getUser().getLocation(); is working
gs.getUser().getLocation().country; is not working

Regards,

Sreedhar

10 REPLIES 10

Harish KM
Kilo Patron
Kilo Patron

you can write a script include and return the country

 

Script include name : getCountry

var getCountry= Class.create();
getCountry.prototype = Object.extendsObject(AbstractAjaxProcessor, {

 var user = new GlideRecord("sys_user");
user.addQuery("sys_id", gs.getUserID());
user.setLimit(1)
user.query();
if (user.next()) {
return user.location.country;
}

},
type: 'getCountry'
});

 

or you can fetch both the values location and country of the user by using JSON

 

Script Include
Name: getUserDetails

Script:
var getUserDetails = Class.create();
getUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor,
{
getUser: function()
{
var user = this.getParameter("sysparm_id");
var userInform ={};
var gr = new GlideRecord("sys_user");
gr.addQuery("sys_id", user);
gr.query();
if (gr.next()){
userInform.location = gr.getValue("location");
userInform.country = gr.getValue("country");
}

var json = new JSON();
var result = json.encode(userInform);
return result.toString();
},
type: 'getUserDetails'
});


Client script

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

var requestor = g_form.getValue("u_requestor");

var ga = new GlideAjax("getUserDetails");
ga.addParam("sysparm_name", "getUser");
ga.addParam("sysparm_id", requestor);
ga.getXMLAnswer(response);
}

function response(getResponse) {
var answer = JSON.parse(getResponse);
g_form.setValue("variable1", answer.location);
g_form.setValue("variable2", answer.country);
}

Regards
Harish

Hi

Do not use:

 

"user.lcoation.country"!!

 

Do use

"user.country" instead !!!

 

Example
var user = new GlideRecord("sys_user");
user.addQuery("sys_id", gs.getUserID());
user.setLimit(1)
user.query();
if (user.next()) {
  gs.info(user.country);
}

 

Hope that helps,

 

BR

Dirk

gotcha..:)

Regards
Harish

Omkar Mone
Mega Sage

Hi 

You will get the country in somewhat this fashion,

 

var gr = new GlideRecord('cmn_location');

gr.addQuery('sys_id',gs.getUser().getRecord().getValue('location'));

gr.query();

if(gr.next())

{

   gs.print("Country is :- "+gr.getDisplayValue('country'));

}

 

Regards,

Omkar Mone.

www.dxsherpa.com