How to retrieve the country of the current logged in user?

Victor Rusch
Kilo Contributor

Hello,

I have to pre-fill a field on a record producer with the Country of the person creating the incident.

I tried using javascript:gs.getUser().getLocation(); , however this only returns the location of the user, I require the country field. I tried using different variants on this script but I can't get it to work.

Can anyone help me to achieve this?

1 ACCEPTED SOLUTION

Try now 

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());
user.setLimit(1)
user.query();
if (user.next()) {
return user.location.country;
}

},
type: 'UserObjectUtils'
});

 

Your refernce qualifier is javascript:new UserObjectUtils().getCountry()

View solution in original post

5 REPLIES 5

Anais Geisen
Tera Expert

Hi,

You should create a script include function that you will call to retrieve the country passing the user's location in parameter.

Regards

Harish Murikina
Tera Guru

Hi,

You can create script include , client callable true

Here i created

Script include name : UserObjectUtils

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

getCountry: function() {
var user = new GlideRecord("sys_user");
user.addQuery("sys_id", gs.getUserID());
user.setLimit(1)
user.query();
if (user.next()) {
return user.getValue("please provide country variable name here");
}

},
type: 'UserObjectUtils'
});

 

Your refernce qualifier is javascript:new UserObjectUtils().getCountry()

Thanks for helping Harish. This script retrieves the 'Country' field from the sys_user table.

However I require the country field that is available on the location table 'cmn_location'.

 

How would I retrieve this value, as I can't seem to dot-walk here?

Try now 

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());
user.setLimit(1)
user.query();
if (user.next()) {
return user.location.country;
}

},
type: 'UserObjectUtils'
});

 

Your refernce qualifier is javascript:new UserObjectUtils().getCountry()