On Change BR - how to trigger for existing records?

Zod
Giga Guru

Hi,

I have a new on change BR that will fire on change of a field.

Now I want all existing records (on sys_user_group) to run the BR once ... so the field X should be updated (by X again) ... .

When creating a job, that runs through all records and update the field with it's own value, no update will be done I assume ...

So how does the best practice would look like here to achieve the run of the BR?

Would be cool if you could provide some script I could modify.

THank you

1 ACCEPTED SOLUTION

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

var roleID = "011ba5aa0a0a0b3001a439c580549134"; //role_delegator



var userGroup = new GlideRecord('sys_user_group');


userGroup.addNotNullQuery('manager');


userGroup.query();


while(userGroup.next()) {


      // add role to new manager


      var gr = new GlideRecord("sys_user_has_role");


      gr.addQuery("user", userGroup.manager);


      gr.addQuery("role", roleID);


      gr.addQuery("granted_by", userGroup.sys_id);


      gr.query();


      if (gr.next())


              gs.log(userGroup.manager.getDisplayValue() + " already has the role_delegator role for the " + userGroup.name + " group - not adding");


      else {


              gr.initialize();


              gr.user = userGroup.manager;


              gr.role = roleID;


              gr.granted_by = userGroup.sys_id;


              gr.inherited = false;


              gr.insert();


              gs.addInfoMessage(gs.getMessage("role_delegator role granted to") + " " +


              userGroup.manager.getDisplayValue() + " " + gs.getMessage("in") + " " + userGroup.name + " " + gs.getMessage("group"));


      }


}


View solution in original post

21 REPLIES 21

Yes, with the business rule you need a trigger, instead the scheduled job could be launched on demand.



Regards,


Valentina


?! How to do that on all groups ... ?


Add the following script to a new scheduled job:



var group = new GlideRecord('sys_user_group');

      group.addActiveQuery(); // add more condition to limit the results if you need


      group.query();


     


      while(group.next()){


    //add your update

      }



Modify the script based on your scope, save and press the button 'Execute now' that appears on the top right.



Regards,


Valentina


Please mark helpful/correct.


As I said, I do not want to change anything, just force the update.


Without changing a value - the update has to be forced ...


As written above .. I think this should be working, but somehow it does not ... any obvious mistake here?




multipleUpdate();


function multipleUpdate() {


var gr = new GlideRecord('sys_user_group');


gr.addQuery('managerISNOTEMPTY');


gr.query();


while(gr.next()) {


gr.setForceUpdate('manager');


}


}


Hi Vem,



Use below script :



multipleUpdate();  


function multipleUpdate() {


var gr = new GlideRecord('sys_user_group');


gr.addQuery('managerISNOTEMPTY');  


gr.query();  


while(gr.next()) {


gr.setForceUpdate(true); // to update records forcefully


}


}



Regards,


Govind Sharma