- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2023 08:20 AM
The specific catalog item should not be visible for the Mexico, Panama, Dominican Republic , Trinidad & Tobago, Colombia, Venezuela, Peru, Ecuador, Brazil, Bolivia, Uruguay, Chile and Argentina countries. I need help in the user criteria script.
Thanks in advance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2023 02:46 AM
Hi @Archana23 ,,
Hope you are doing great.
Please try using below script in user criteria:
(function () {
// Get the country of the user making the request
var country = gs.getUser().getCompany().getCountry();
// Define the list of countries where the catalog item should be hidden
var hiddenCountries = [
'Mexico',
'Panama',
'Dominican Republic',
'Trinidad & Tobago',
'Colombia',
'Venezuela',
'Peru',
'Ecuador',
'Brazil',
'Bolivia',
'Uruguay',
'Chile',
'Argentina'
];
// Check if the user's country is in the list of hidden countries
if (hiddenCountries.indexOf(country) !== -1) {
return false; // Hide the catalog item
} else {
return true; // Show the catalog item
}
})();
Regards,
Riya Verma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2023 02:29 AM
in system property store the sysIds as comma separated value like sysId1,sysId2,sysId3 etc
Also avoid giving space in the name of system property
then update script as this
var location_sys_id = '';
var user_id = gs.getUserID();
var user = new GlideRecord('sys_user');
user.addQuery('sys_id', user_id);
user.query();
if (user.next()) {
location_sys_id = user.location;
}
var final_parent = '';
do {
var loc = new GlideRecord('cmn_location');
loc.addQuery('sys_id', location_sys_id);
loc.query();
if (loc.next()) {
var parentloc = loc.parent; // returns parent sysid
var parentname = loc.getDisplayValue('parent');
var userlocname = loc.name; // returns parent name
var location_type = loc.cmn_location_type;
if (location_type == 'region') {
final_parent = loc.sys_id;
break;
}
}
final_parent = loc.parent;
}
while (parentname == userlocname);
var ans1 = false;
var prop = gs.getProperty('propertyName');
if (prop.indexOf(final_parent) > -1) {
ans1 = true;
}
answer = ans1;
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2023 06:55 AM
As per best practices you should not use gs.getUser()
instead use user_id which gives you logged in user's sysId and then query user table to get the country
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2023 02:46 AM
Hi @Archana23 ,,
Hope you are doing great.
Please try using below script in user criteria:
(function () {
// Get the country of the user making the request
var country = gs.getUser().getCompany().getCountry();
// Define the list of countries where the catalog item should be hidden
var hiddenCountries = [
'Mexico',
'Panama',
'Dominican Republic',
'Trinidad & Tobago',
'Colombia',
'Venezuela',
'Peru',
'Ecuador',
'Brazil',
'Bolivia',
'Uruguay',
'Chile',
'Argentina'
];
// Check if the user's country is in the list of hidden countries
if (hiddenCountries.indexOf(country) !== -1) {
return false; // Hide the catalog item
} else {
return true; // Show the catalog item
}
})();
Regards,
Riya Verma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2023 06:12 AM
Thanks..It worked