Querying the user id through the sys_user_has_role table

Wasd123
Tera Expert

I have the following script to look up the sys_user_has_role table and remove roles from users

var user = new GlideRecord('sys_user');
user.query();
while (user.next()) {

    // Check if user has snc_internal role
    var roleQuery = new GlideRecord('sys_user_has_role');
    roleQuery.addEncodedQuery("user=" + user.sys_id + "^role=7fcaa702933002009c8579b4f47ffbde"); //sys_id of the SNC_internal role 
    roleQuery.query();

    // If user does NOT have snc_internal, delete the role record if it exists (it will remove the correlation from the sys_user_has_role table between the user and the role, so remove it from the suer account)
    if(roleQuery.getRowCount() == 0) {
        var deleteQuery = new GlideRecord('sys_user_has_role');
        deleteQuery.addEncodedQuery("user=" + user.sys_id + '^role=341fa5f60fa51010784dd2d92f767ec6'); //sys_id of the sn_wsd_core.workplace_user role 
        deleteQuery.query();
        if(deleteQuery.next()) {
            deleteQuery.deleteRecord();
        }
    }

}

 I have tried to modify the beginning in this way 

var user = new GlideRecord('sys_user');
user.addEncodedQuery('user_name','NOTLIKE','@emailsuffix.com');
user.query();
while (user.next()) {

and removed that part then 

// Check if user has snc_internal role
    var roleQuery = new GlideRecord('sys_user_has_role');
    roleQuery.addEncodedQuery("user=" + user.sys_id + "^role=7fcaa702933002009c8579b4f47ffbde"); //sys_id of the SNC_internal role 
    roleQuery.query();

as I want to remove a specific role from all the users in case their user id contains tis suffix. How could I do this, as the above approach is giving me null values.

1 ACCEPTED SOLUTION

When addQuery or addEncodedQuery encounters something it doesn't know what to do with, it ignores it so that must be what it's doing in your example.  In a sys_user list view, create the filter using the funnel icon and confirm that the records displayed in the list view are correct, then right-click on the query in the breadcrumbs and choose Copy query.  Paste this into your script and you should get the same results from a GlideRecord.  Also when testing this part, make sure this is the entire script so that you're certain the results pertain to this while loop and are not generated elsewhere.

View solution in original post

4 REPLIES 4

SuhasPSalunkhe
Kilo Guru

One thing I noticed -

 Instead of user.sys_id used user. getValue('sys_id')

 

In loops user. sys_id takes last/first user sysid always.

Brad Bowman
Kilo Patron
Kilo Patron

To add an encoded query to your sys_user GlideRecord would be more like this:

 

user.addEncodedQuery('user_nameNOTLIKE@emailsuffix.com');

 

Then it wouldn't hurt to change your sys_user_has_role encoded query to this, just in case:

 

  roleQuery.addEncodedQuery("user=" + user.sys_id.toString() + "^role=7fcaa702933002009c8579b4f47ffbde"); //sys_id of the SNC_internal role 

 

 

as per the print screen as well, when trying this solution of user_nameNOTLIKE@emailsuffix.com it is still print the users with those email suffixes.  So how could I do that for example it should only check for email suffix and not for the snc_internal role? So if emailsuffix is not like this then remove a role. image.png

When addQuery or addEncodedQuery encounters something it doesn't know what to do with, it ignores it so that must be what it's doing in your example.  In a sys_user list view, create the filter using the funnel icon and confirm that the records displayed in the list view are correct, then right-click on the query in the breadcrumbs and choose Copy query.  Paste this into your script and you should get the same results from a GlideRecord.  Also when testing this part, make sure this is the entire script so that you're certain the results pertain to this while loop and are not generated elsewhere.