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.

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

Awesome, thanks a bunch Dylan! That ended up working out for me.