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

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;


                  }


      }


Hi Justin,


That one worked perfect!   Thank you.