Business Rule Mass Update

mstoner
Kilo Contributor

Hey all,

Checking for any input on this one. I changed a business rule on Project that updates a field calculation. The rule executes when the form is saved but I'd like to update the existing records with the new calculation as well.

I found this post Mass update through script without calling Business Rules and Events that talks about running a simple background script and prevent things from running. So my question is, to get the business rules to trigger, could I just run it inverse as:

var gr = new GlideRecord('pm_project');
gr.query();
while (gr.next()){
gr.setWorkflow(true);
gr.update();
}

Or would I even need the setWorkflow bit?

Thanks,
-Mike

13 REPLIES 13

CapaJC
ServiceNow Employee
ServiceNow Employee

You don't need setWorkflow (you only need to use it if you want it to be false), but if you're not changing any values in your script you DO need to do the following, otherwise the update will be ignored:



gr.setForceUpdate(true);


gputker
Tera Contributor

Thanks heaps @CapaJC - legend.


I needed to run my business rules on the sys_user table for all active records without actually updating the records.  This worked perfectly (thanks to you!) and with very minimal system impact:


var gr = new GlideRecord('sys_user');  //call the sys_user table

gr.addQuery('active', 'true');  //only update active records

gr.query();

while(gr.next())



gr.update();    //make update changes - OF WHICH THERE ARE NONE
gr.setForceUpdate(true);  //force the record to update even though no actual changes have been made

}

 

I dropped the above code into a scheduled job and it works perfectly.  Cheers.

mstoner
Kilo Contributor

Thanks for the input. We did some testing and this will work. In the tests I did, it updates between 2 and 3 records per second. Anyone know if this is this the most efficient method for updating tables?

Thanks!
-Mike


CapaJC
ServiceNow Employee
ServiceNow Employee

Well, doing it this way you're running ALL business rules on that table. If you only really want the one thing to happen, you could set workflow to false (so no business rules run), and just use your script to do the calculation of that field and update its value.