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.

Update users when removed from group

LRobinson
Giga Contributor

I need to update the user notification field based on the users removal from a specific group.   I have considered a script to run on schedule that checks all users and determines whether or not they are a member of the group.   I have also considered the possibility of a 'script action' to determine if the LDAP Group import Event removes a user from the group and updates the user record.   Can anyone help me with a script to perform this function?  

This is a version of something   I've tried as a Business Rule on all sys_users, but it is not working:

 

Table: sys_user

When: after

 

updateNotification(current.user);

function updateNotification() {

  var gr = new GlideRecord('sys_user');

  gr.addQuery('sys_id','=',userSysID);

  gr.query();

  while (gr.next()) {

 

  //Now see if they are a member of the group

  var grpMbr = new GlideRecord('sys_user_grmember');

  grpMbr.addQuery('name', 'Notification Disabled');

  grpMbr.query();

  if (grpMbr.next()) {

  gr.notification = 1;

  gr.update();

  } else {

  gr.notification = 2;

  gr.update();

  }

  }

}

1 ACCEPTED SOLUTION

Hi, I made a generic function (member()) to check group membership.   It takes a user sys id and a group name and returns true if they are in the group, false if they are not.   It is tested/working in my instance.



This is meant to be used as a scheduled job, not a BR.   You may have to change line 3 so it makes sense for your instance.



//Scheduled Job/Script:


var gr = new GlideRecord('sys_user');


      gr.addQuery('notification', '1');//querying all notification disabled users


      gr.query();


      while(gr.next()) {


          //iterating through disabled users, checking group membership:


          if( !member(gr.sys_id, 'Notification Disabled') ) {


          //if user is NOT a member of Notification Disabled:


              gr.notification = 2; //notification enabled


              gr.update();


          }


      }



function member(user_sys_id, group) {


          var group_check = new GlideRecord('sys_user_grmember');


                  group_check.addQuery('group.name', group);


                  group_check.addQuery('user', user_sys_id);


                  group_check.query();


                  if(group_check.next()) {


                      return true;


                  }else{


                      return false;


                  }


      }


View solution in original post

16 REPLIES 16

Subhajit1
Giga Guru

First set the value of the Notification field for all on the User form to No=2.


Then check for all the users who are Not part of that group by running a GlideQuery on the sys_user_grmember table. Now just set the value for all these Users to Yes=1.



var gr = new GlideRecord ('sys_user');


gr.addQuery('active',true);


gr.query();



while(gr.next())


{


gr.notification=2;


gr.update();


var grMbr = new GlideRecord('sys_user_member');


grMbr.addQuery('user',gr.name);


grMbr.query();


if(grMbr.next())


{


gr.notification=1;


gr.update();


}


}



Kindly let me know if this works,haven't tested it though.


For testing, try putting in a more exclusive filter in line 2 and add some more filters in there.



Thanks,


Subhajit


Hi, thank you for the suggestion, this did not work.


justin_drysdale
Mega Guru

Without digging too deeply, I see that you aren't passing the argument to the function.   The variable 'userSysID' is used instead (I don't see it declared/initialized in the script).   I also refactored the code a tiny bit.



Try:


updateNotification(current.user);



function updateNotification(cu) {


  var gr = new GlideRecord('sys_user');


          gr.addQuery('sys_id',cu);


          gr.query();


  while (gr.next()) {


  //Now see if they are a member of the group


      var grpMbr = new GlideRecord('sys_user_grmember');


      grpMbr.addQuery('name', 'Notification Disabled');


      grpMbr.query();


          if (grpMbr.next()) {


              gr.notification = 1;


          }else{


              gr.notification = 2;      


          }


    gr.update();


  }//end while


}


:


If you get any errors please post it in this thread.


justin_drysdale
Mega Guru

OK, I dug more deeply.   Since you are updating the user record and you already have access to the user in sys_user_grmember, try this:



updateNotification(current.user);



function updateNotification(cu) {


var grpMbr = new GlideRecord('sys_user_grmember');


      grpMbr.addQuery('group', 'Notification Disabled'); //changed 'name' to 'group'


      grpMbr.addQuery('user', cu);          


      grpMbr.query();


              if (grpMbr.next()) {


                  //user is a member of group 'Notification Disabled':


                  grpMbr.user.notification = 1;


              }else{


                  //user is not a member:


                  grpMbr.user.notification = 2;


              }


      grpMbr.update();


}