- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi All,
Can someone please suggest why the below criteria's script is not working.
my requirement is, ABC catalog should not be available/ visible for 'User criteria country' and for users with Job Code 'J04437 or J15505' and having ISO Code as 'MEX'.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
update as this
-> you were using reverse logic
-> also use user_id in user criteria script as per best practice
-> set answer = true/false in script
-> I couldn't find User Criteria Country field on that table (why you used that)
Try using below script and uncheck Match All Checkbox
var grUser = new GlideRecord('sys_user');
if (grUser.get(user_id)) {
var code = grUser.getValue('u_job_code');
var iso = grUser.getValue('u_iso_code');
// Hide catalog if: (J04437 OR J15505) AND MEX
if ((code == "J04437" || code == "J15505") && iso == "MEX") {
answer = true; // true = HIDE catalog (Not Available For)
}
}
answer = false; // false = SHOW catalog
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Ankita9793 ,
try this code instead
/** Scripted User Criteria is not cached, and evaluated everytime, so performance is dependent on the script.
* Populate `answer` with true/false or evaluate to true/false
* The script is evaluated in the scope the user criteria is defined
* Don't use `current` in the script or populate the variable
* Don't use `gs.getUser()` or `gs.getUserID()`,
* instead use `user_id` which contains the user sys_id against whom the evaluation is happening.
*/
answer = false;
var grUser = new GlideRecord('sys_user');
if (grUser.get(user_id)) {
var code = grUser.getValue('u_job_code');
var iso = grUser.getValue('u_iso_code');
answer = ((code == "J04437" && iso == "MEX") || (code == 'J15505' && iso == "MEX"))
}
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Chaitanya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Ankita9793 ,
Your script logic was reversed. In a user criteria script, returning true means the catalog item is visible, and returning false means it is hidden. Your script returned true for the users you wanted to block (with Job Code J04437 or J15505 and ISO Code MEX), which made the catalog visible to them instead of hiding it. The corrected script now returns false for those users, so the catalog is hidden only for them and visible for everyone else.
answer();
function answer() {
var user = gs.getUserID();
var grUser = new GlideRecord('sys_user');
grUser.addQuery('sys_id', user);
grUser.query();
if (grUser.next()) {
var jobCode = grUser.getValue('u_job_code');
var isoCode = grUser.getValue('u_iso_code');
if ((jobCode == "J04437" || jobCode == "J15505") && isoCode == "MEX") {
return false;
}
return true;
}
return true; d
}
If you found my solution helpful, please mark it as Helpful or Accepted Solution...!
thanks,
tejas
Email: adhalraotejas1018@gmail.com
LinkedIn: https://www.linkedin.com/in/tejas1018
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
Hi @Ankita9793 ,
can you try this code
var code = gs.getUser().getRecord().getValue('u_job_code'); // if it is reference value then use getDisplayValue instead of getValue()
var iso = gs.getUser().getRecord().getValue('u_iso_code');
if ((code == "J04437" && iso == "MEX") || (code == 'J15505' && iso == "MEX")) {
answer = true;
} else {
answer = false;
}
If this information proves useful, kindly mark it as helpful or accepted solution.
Thanks,
BK
Thanks,
BK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Ankita9793,
what do you receive in the logs?
Be aware that you have marked "Match all" checkbox, which means that not only the script must be evaluated but also the conditions above it... try to uncheck this field and retest if you see any difference.
If it will be still the same behaviour, then ignore this suggestion, if it will start working, then you might need to change the logics, I miss the context behind it and am not familiar wiht the data processing
No AI was used in the writing of this post. Pure #GlideFather only
