- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2025 03:26 AM
Good Morning,
I'm writing a script for the user criteria to check if the company = a specific sys ID and grade is either two sys IDs
var userGR = new GlideRecord('sys_user'); // Create a new GlideRecord object for the sys_user table
userGR.get(user_id);
var encoded = gr.addEncodedQuery('company=e1c9d9a31b15d4d09732dd39cd4bcbbe^u_grade=122a41e01b2998947ca8620e6e4bcb62^ORu_grade=d22a41e01b2998947ca8620e6e4bcb62^ORu_grade=5a2a41e01b2998947ca8620e6e4bcb62^ORu_grade=1e2a41e01b2998947ca8620e6e4bcb62'); // Filter to only show users with the grade 6/SCS and in the company HMRC
if (userGR == encoded)
answer = true;
else
answer = false;
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2025 04:48 AM
the working code I already shared, did you check that?
var userGR = new GlideRecord('sys_user'); // Create a new GlideRecord object for the sys_user table
userGR.addQuery('sys_id', user_id);
userGR.addEncodedQuery('company=e1c9d9a31b15d4d09732dd39cd4bcbbe^u_grade=122a41e01b2998947ca8620e6e4bcb62^ORu_grade=d22a41e01b2998947ca8620e6e4bcb62^ORu_grade=5a2a41e01b2998947ca8620e6e4bcb62^ORu_grade=1e2a41e01b2998947ca8620e6e4bcb62'); // Filter to only show users with the grade 6/SCS and in the company HMRC
userGR.query();
answer = userGR.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
03-10-2025 03:37 AM - edited 03-10-2025 03:38 AM
Hi @Bradley Can you try the below code and let me know if this works for you?
var userGR = new GlideRecord('sys_user'); // Create a new GlideRecord object for the sys_user table
userGR.get('user_id');
var encoded = userGR.addEncodedQuery('company=e1c9d9a31b15d4d09732dd39cd4bcbbe^u_grade=122a41e01b2998947ca8620e6e4bcb62^ORu_grade=d22a41e01b2998947ca8620e6e4bcb62^ORu_grade=5a2a41e01b2998947ca8620e6e4bcb62^ORu_grade=1e2a41e01b2998947ca8620e6e4bcb62'); // Filter to only show users with the grade 6/SCS and in the company HMRC
if (userGR == encoded)
return true;
else
return false;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2025 03:38 AM
Please try with below updated code:
var userGR = new GlideRecord('sys_user');
userGR.addEncodedQuery('company=e1c9d9a31b15d4d09732dd39cd4bcbbe^' +
'u_grade=122a41e01b2998947ca8620e6e4bcb62^OR' +
'u_grade=d22a41e01b2998947ca8620e6e4bcb62');
userGR.addQuery('sys_id', user_id); // Ensure the query applies to the current user
userGR.query();
var answer = userGR.next(); // If a record is found, answer is true
Please mark correct/helpful if this helps you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2025 03:42 AM
Thank you for that. I've tested on my side and still not working sorry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2025 03:46 AM
@Bradley I did a mistake, could you try now
var userGR = new GlideRecord('sys_user');
userGR.addEncodedQuery('company=e1c9d9a31b15d4d09732dd39cd4bcbbe^' +
'u_grade=122a41e01b2998947ca8620e6e4bcb62^OR' +
'u_grade=d22a41e01b2998947ca8620e6e4bcb62');
userGR.addQuery('sys_id', gs.getUserID()); // Ensure the query applies to the current user
userGR.query();
if(userGR.next()){
var answer=true;
}