Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

User Criteria help

Community Alums
Not applicable

Hi all, 

 

I am writing a code to group users into a criteria if they are both not a user of groups with the words "Technical Team" in the group title, and their location type is not "X"

 

var userName = new GlideRecord('sys_user');
userName.addEncodedQuery('sys_id', user_id);
userName.query(); // Get logged in user record

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.name);
    grMem.query(); // Find users groups

    while (grMem.next()) //Iterate though associated users' groups
    if (userLoc == 'X' && grMem.group.name.indexOf("Technical Team") > -1) { //See if any groups contain "Technical Team" and user is X user
        answer = true;
        break; //exit loop if condition found
    } else {
        answer = false;
    }
}

However, when putting this Criteria into "Not Available For" on a Catalog item, upon impersonating a matching user, the item still appears and is not hidden as expected. 

 

Could I please ask for some help here, as I am not sure what I am getting wrong. Thank you 

1 ACCEPTED SOLUTION

Community Alums
Not applicable

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;
        }
    }
}

 

 

View solution in original post

6 REPLIES 6

sunil maddheshi
Tera Guru

@Community Alums 

Issues in Your Code

  1. addEncodedQuery('sys_id', user_id) is incorrect

    • addEncodedQuery() requires a properly formatted encoded query string.
    • Use addQuery('sys_id', user_id) instead.
  2. 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.
  3. 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

Community Alums
Not applicable

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;
        }
    }
}