The CreatorCon Call for Content is officially open! Get started here.

Remove list of users from list collector from a specified group

jakelaux
Kilo Explorer

Hello all,

I am trying to collect a list of users in a service form's list collector and to remove said users from the group. I am running this script from within the workflow for the service.

The following is the script snippet I am having issues with.

var users =   current.variables.users_remove.toString().split(',');

    for(var i=0; i<users.length; i++){

          var gr = new GlideRecord('sys_user_grmember');

          gr.addQuery('group', current.variables.group);

          //gs.log("user " + i + ": "+ users[i]);

          //gr.addQuery('user', users[i]);

          gr.query();

          //gs.log("gr.next: " + gr.next());

          if(gr.next()){ //while there are records in the recordset

                  var temp = gr.deleteRecord();

                  // gs.log("TEMP: " + temp);

            }

    }

The issue I am currently having is that gr.next() is returning false even though I can log the list of users from the group. If anyone could help me out with this I would be quite appreciative.

Thanks,

Jake

1 ACCEPTED SOLUTION

The result of the list collector is the sys_id of that row of group membership. You will just have to delete that record, I believe.



Again, this was based on my example above, where the variable containing the list of sys_ids of group membership is called "user_test".



function deleteMultipleUsers() {


    //This variable will return a list of sys_ids of group membership to delete


  var users = current.variables.user_test.toString().split(',');


  for(var i=0; i<users.length; i++){


  var gr = new GlideRecord('sys_user_grmember');


  gr.addQuery('sys_id', users[i]);


  gr.query();


  if(gr.next()){


  gr.deleteRecord();


  }


  }


}


deleteMultipleUsers();



Give that a try?


View solution in original post

15 REPLIES 15

Here is my latest version of the code:



function deleteMultipleUsers() {


  var users = current.variables.users_remove.toString().split(',');


  for(var i=0; i<users.length; i++){


  var gr = new GlideRecord('sys_user_grmember');


  gr.addQuery('group', current.variables.group);


  gr.addQuery('user', users[i]);


  gr.query();


  if(gr.next()){


  gr.deleteRecord();


  }


  }


}


deleteMultipleUsers();



Thanks for taking a look at this for me!


- Jake


Hello Jacob,



Is it removing any users from the group?


Try to log users and group values.


If I log the users I get a sysId for each user, so I don't believe it's a problem with obtaining and using the list of the users, however, my query is still failing for some reason.


The result of the query seems to be unidentified and thus the result of next is then false. Do you have any idea why the result to the query might be unidentified?


Thanks,


-Jake


Capture.PNG


Are you getting the group correctly? Add a log for group. Are the users included in the group which is there on the current record?