- 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:50 AM - edited 03-31-2024 07:52 AM
Hello @PatriciaA987250 ,
user following code for return value :
return (user.u_country.toString());
Please let me know if it works for you or not.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2024 10:35 PM
Hi Unique45,
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 even with/without the toString() method.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2024 08:14 AM
Hi @PatriciaA987250 ,
Client Script :
var ga = new GlideAjax('UserObjectUtils'); ga.addParam('sysparm_name', 'userObject'); ga.getXML(HelloWorldParse); function HelloWorldParse(response) { var answer = response.responseXML.documentElement.getAttribute("answer"); // alert(answer); g_form.setValue('PASS COUNTRY VARIABLE NAME HERE',answer); }
Script include name : UserObjectUtils
Client callable : true
Script:
var UserObjectUtils = Class.create(); UserObjectUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, { userObject:function() { var user = new GlideRecord("sys_user"); user.addQuery("sys_id", gs.getUserID()); user.setLimit(1) user.query(); if (user.next()) { return user.location.country; } } , _privateFunction: function() { // this function is not client callable } });
If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!
Thanks & Regards,
Sumanth Meda
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2024 10:36 PM
Hi Sumanth,
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.