Update user field when added to new group

LRobinson
Giga Contributor

I want to update the "notification" field on the sys_user record when the user is added to a specific group.

I am trying with a Business Rule but haven't been able to make it work.   Is a Business Rule the best way or should I use a Client Script?

This is the rule I have created on the Group [sys_user_group] :

 

updateNotification();

 

function updateNotification() {

      var user = current.user.getRefRecord();

      if (!user.isValid())

              return;

      var group = new GlideRecord("sys_user_group");

      if (current.group =='Notification Disabled') {

              user.notification = 1;

      }

      user.update();

}

 

Thanks for any insight!

1 ACCEPTED SOLUTION

ben_ennis
ServiceNow Employee
ServiceNow Employee

Hello,



There are a few ways to accomplish this task. I think the simplest one would be to use a business rule. Here is some sample code that should accomplish what you are trying to do. You will of course have to replace the sys_id I have hard defined here with the sys_id of the group you would like this to apply to. Let me know how this works out.



updateNotification(current.user);


function updateNotification(userSysID) {


  if(current.group == '8a5055c9c61122780043563ef53438e3'){


  var gr = new GlideRecord('sys_user');


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


  gr.query();


  while (gr.next()) {


  gr.notification = 2;


  gr.update();


  }


  }  


}



BRCap.PNG


View solution in original post

4 REPLIES 4

ben_ennis
ServiceNow Employee
ServiceNow Employee

Hello,



There are a few ways to accomplish this task. I think the simplest one would be to use a business rule. Here is some sample code that should accomplish what you are trying to do. You will of course have to replace the sys_id I have hard defined here with the sys_id of the group you would like this to apply to. Let me know how this works out.



updateNotification(current.user);


function updateNotification(userSysID) {


  if(current.group == '8a5055c9c61122780043563ef53438e3'){


  var gr = new GlideRecord('sys_user');


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


  gr.query();


  while (gr.next()) {


  gr.notification = 2;


  gr.update();


  }


  }  


}



BRCap.PNG


LRobinson
Giga Contributor

This works perfect, Thank you!


LRobinson
Giga Contributor

The "correct answer" worked for my initial question.   However, I now realize I need to update the "notification" field on the SN sys_user record when it is updated from LDAP because the user was added to the group in the AD.   Would it require a transform script that runs when the LDAP upload completes?



Thanks,


Lorna


Final working solution: I made the BR script "client callable", and added the function to a transform script in my Group Import Transform Map, to run "onComplete".


Now I just have to figure out the best way to update notification if a user is removed from the group...in AD.



Lorna