- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2024 07:24 AM
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'
});
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2024 07:39 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2024 07:39 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2024 10:30 PM
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.