Query user role in advanced user criteria
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi experts,
Please help. I need to validate, in an advanced user criterion (script), that the user has role knowledge and their manager has the title of IT Manager. I've tried this code, but it doesn't work. Any recommendations, please?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
did you try debugging by adding that script in background script by hard-coding user sysId?
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
3 weeks ago
Hi @Ankur Bawiskar, It works in the background, but not in the user criteria.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Try doing this
1) add role in roles field in User Criteria
2) then use advanced script for manager title
3) ensure "Match All" is selected
var gr = new GlideRecord("sys_user");
gr.addQuery("sys_id", user_id);
gr.addQuery("manager.title", "IT Manager");
gr.query();
answer = gr.hasNext();
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
3 weeks ago - last edited 3 weeks ago
Hi @Santiago Ruales ,
As you mentioned you need to work this script in the User Criteria then you need to modify the script part as below
(function() {
var userId = gs.getUserID(); //Get the logged in user
var hasRole = false;
// Check if the user has the specific role
var roleGR = new GlideRecord("sys_user_has_role");
roleGR.addQuery("role", "xxxxda36c0a8016b888c7f06a1ce7e14"); // Replace with actual role sys_id
roleGR.addQuery("user", userId);
roleGR.query();
if (roleGR.next()) {
hasRole = true;
}
if (hasRole) {
var userGR = new GlideRecord("sys_user");
if (userGR.get(userId) && userGR.manager) {
var managerGR = new GlideRecord("sys_user");
if (managerGR.get(userGR.manager.toString())) {
answer = (managerGR.title == 'IT Manager');
return;
}
}
}
answer = false;
})();
Avoid using sys_ids in the script directly, If this solution helped, please mark it as helpful and accept the answer.
Regards,
Raviteja
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Santiago Ruales ,