User Criteria help

Wilwod
Tera Expert

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

Wilwod
Tera Expert

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

Swapna Abburi
Mega Sage
Mega Sage

Hi @Wilwod 

Please update your script as below.

 

function eligableUser() {
    var answer = '';
    var user = gs.getUserID();
    var userName = new GlideRecord('sys_user');
    userName.addEncodedQuery('sys_id='+user);
    userName.query(); // Get logged in user record

    while (userName.next()) {
        var userLoc = userName.location.cmn_location_type; //Get user locaiton type
        var grMem = newGlideRecord('sys_user_grmember');
        grMem.addQuery('user', user);
        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;
                return answer;
                break; //exit loop if condition found
            } else {
                answer = false;
                return answer;
            }
        }
    }
}

eligableUser();

Ankur Bawiskar
Tera Patron
Tera Patron

@Wilwod 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Wilwod 

the script I shared should also work

Did you try that?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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.