The Zurich release has arrived! Interested in new features and functionalities? Click here for more

How to determine country of logged in user

PatriciaA987250
Tera Expert

Hi,

I've a custom field named as u_country (type is choice ) which contain values for two countries.  As an user login, i want to get the value of the current user's country from the custom field. I've tried include script  as below but it return null. Kindly advise how to obtain the value of the country?

 

Script

var UserCountryUtil = Class.create();
UserCountryUtil.prototype = Object.extendsObject(AbstractAjaxProcessor,{
    getUserCountry: function() {
    var user = new GlideRecord('sys_user');
user.addQuery('sys_id', gs.getUserID());
    user.setLimit(1);
    user.query();
    if (user.next()) {
     return user.u_country;
    }     
  },

    type: 'UserCountryUtil'
});

 

 

1 ACCEPTED SOLUTION

Maddysunil
Kilo Sage

@PatriciaA987250 

Ensure that the user executing this script has the necessary permissions to read the u_country field on the sys_user table.

Make sure that the u_country field is populated for the user you are testing with. If it's empty, the script will return null.

 

var UserCountryUtil = Class.create();
UserCountryUtil.prototype = Object.extendsObject(AbstractAjaxProcessor,{
    getUserCountry: function() {
        var user = new GlideRecord('sys_user');
        user.addQuery('sys_id', gs.getUserID());
        user.setLimit(1);
        user.query();
        if (user.next()) {
            var country = user.u_country.toString();
            gs.info("User Country: " + country);
            return country;
        } else {
            gs.error("User not found or country field is empty.");
            return null;
        }     
    },

    type: 'UserCountryUtil'
});

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

View solution in original post

6 REPLIES 6

Maddysunil
Kilo Sage

@PatriciaA987250 

Ensure that the user executing this script has the necessary permissions to read the u_country field on the sys_user table.

Make sure that the u_country field is populated for the user you are testing with. If it's empty, the script will return null.

 

var UserCountryUtil = Class.create();
UserCountryUtil.prototype = Object.extendsObject(AbstractAjaxProcessor,{
    getUserCountry: function() {
        var user = new GlideRecord('sys_user');
        user.addQuery('sys_id', gs.getUserID());
        user.setLimit(1);
        user.query();
        if (user.next()) {
            var country = user.u_country.toString();
            gs.info("User Country: " + country);
            return country;
        } else {
            gs.error("User not found or country field is empty.");
            return null;
        }     
    },

    type: 'UserCountryUtil'
});

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

Hi Maddy,

 

Thank you for your feedback. The issue was caused by data that was missing or empty. After fixing it, the script is working as expected.