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.

Need to remove multiple inactive users from groups

anatara
Giga Contributor

Hi All,

I have a requirement to remove inactive users who are all more than 15 days from today from the group member table by using scheduled job. I have written a script for this. But whenever I execute it, It removes only few inactive users from groups..Assume I have 700 Inactive Grp members, first time - 692, second time - 678..Like that, It is working...But I want it in a single shot. Below is My script., Do we have any functionality ? Because I cannot modify the 'slice' each time.

Please post your suggestions.

Thanks In Advance

gs.log("********Removing Inactive Users from Groups********");

var nowdate = gs.now();

var gr=new GlideRecord("sys_user");

gr.addQuery('active', false);

gr.query();

gs.log('RemovingInactive users: ' + gr.getRowCount());

while(gr.next()){

      var result = gs.dateDiff(nowdate, gr.u_term_date, false);

      var res = result.slice(1,4);

              if(res > 15){

              //gs.log('Day :' + res);

              var grmember = new GlideRecord('sys_user_grmember');

              grmember.addQuery('user', gr.sys_id);

              grmember.query();

              while(grmember.next()){

                      grmember.deleteMultiple();

                      grmember.update();

              }

      }

}

   

5 REPLIES 5



I just commented update() and tried but still no luck. I am getting the days format which is shown in the above image. So I have used Slice(1,4) to neglect '-' from the format.