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

saritha9
Giga Expert

Uncomment the query for user.


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




Try to log values of group and users. See if you are getting correct values.


Subhajit1
Giga Guru

Hi Jacob,


As Saritha suggested, uncomment the gr.addQuery('user', users[i]); line.


Also, instead of using the var temp = gr.deleteRecord();, just use gr.deleteRecord().


jakelaux
Kilo Explorer

Saritha and Subhajit,


I noticed those errors yesterday and modified my code accordingly. However, I am still having issues.


The first portion delete just me works, however, the code within the else (which is meant to delete multiple users that were collected in a list collector) does not work.


deleteUsers();




function deleteUsers() {


  if(current.variables.who == 'just_add_me'){


            var me = new GlideRecord('sys_user_grmember');


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


            me.addQuery('user', current.opened_by);


            me.query();


            if(me.next()){


                      me.deleteRecord();


            }


  }


else {


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


  //workflow.scratchpad.users = users;


  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();


            }      


  }


  }


}



My gr.next() is returning false and I believe it is because my gr.query() is returning undefined.



Thanks,


Jake


Hello Jacob!



Could you post your latest version of the code?


The code in the original post looks fine if you've corrected it according to what Subhajit Das and saritha have suggested.