- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 07:50 PM
@Community Alums
try this
var userName = new GlideRecord('sys_user');
userName.addQuery('sys_id', user_id);
userName.query(); // Get logged in user record
var isExcluded = false;
while (userName.next()) {
var userLoc = userName.location.cmn_location_type; // Get user location type
var grMem = new GlideRecord('sys_user_grmember');
grMem.addQuery('user', userName.sys_id);
grMem.query(); // Find user's groups
while (grMem.next()) {
if (grMem.group.name.toString().indexOf("Technical Team") > -1) { // Check if any groups contain "Technical Team"
isExcluded = true;
break; // Exit loop if condition found
}
}
if (userLoc == 'X') { // Check if user location type is 'X'
isExcluded = true;
}
}
if (!isExcluded) {
// User meets the criteria
answer = true;
} else {
// User does not meet the criteria
answer = false;
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2025 01:58 AM
@Community Alums
the script I shared should also work
Did you try that?
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2025 02:09 AM
Hi,
I reviewed yours, but it was not fit for purpose in this use case. I needed the if statement to be correct when both conditions were true, not separate. If A User is both X location type and a member of the technical team, then they are allowed. But if a user is an X user and not a member of the technical team, then they are not allowed. This was highlighted in my original post.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 09:09 PM
@Community Alums
Issues in Your Code
addEncodedQuery('sys_id', user_id) is incorrect
- addEncodedQuery() requires a properly formatted encoded query string.
- Use addQuery('sys_id', user_id) instead.
Wrong comparison logic (&& instead of ||)
- The condition should exclude users if either:
- They are in a group with "Technical Team" OR
- Their location type is "X"
- But your condition (if (userLoc == 'X' && grMem.group.name.indexOf("Technical Team") > -1)) is requiring both conditions to be true.
- The condition should exclude users if either:
Incorrect answer assignment
- The answer variable should start as false and become true if a matching group or location is found.
- Currently, your script might overwrite answer incorrectly inside the loop.
var answer = false; // Default to false
var userName = new GlideRecord('sys_user');
userName.get(user_id); // Fetch the user record
if (userName.isValidRecord()) {
var userLoc = userName.location.getDisplayValue(); // Get the location type
var grMem = new GlideRecord('sys_user_grmember');
grMem.addQuery('user', user_id);
grMem.query(); // Query user's group memberships
while (grMem.next()) {
var groupName = grMem.group.getDisplayValue(); // Get group name
if (groupName.indexOf("Technical Team") > -1 || userLoc == 'X') {
answer = true; // Mark as not available
break; // Exit loop early since condition met
}
}
}
Please mark helpful/correct if this helps you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2025 01:59 AM
This is the code I got working in the end.
var userName = new GlideRecord('sys_user');
userName.addQuery('sys_id', gs.user_id());
userName.query(); // Get logged in user record
while (userName.next()) {
var userLoc = userName.location.cmn_location_type; //Get user locaiton type
var grMem = new GlideRecord('sys_user_grmember');
grMem.addQuery('user', gs.user_id());
grMem.query(); // Find users groups
while (grMem.next()) { //Iterate though associated users' groups
if (userLoc == 'Ship' && grMem.group.name.indexOf("Onboard IT") === -1) { //Check user location type is Ship
answer = true;
} else {
answer = false;
}
}
}