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

The SN Nerd
Giga Sage
Giga Sage

If you only want one business rule to run for all records, follow the pattern below:

var current = new GlideRecord('pm_project');
current.setWorkflow(false); //Do not run any other business rules following the update
current.query();

while (current.next()) {

	//COPY BUSINESS RULE SCRIPT HERE
	(function executeRule(current, previous /*null when async*/) {


	})(current, previous);
	//END BUSINESS RULE

	// If before business rule
	current.update();

}

ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

I just created a before update BR and essentially need to “update” all records on the table without putting my User ID as the last updated by. Would your script accomplish this?

Community Alums
Not applicable

Hi Mike,

Try adding gr.autoSysFields(false) so no changes to system fields will be done

 

Cheers,

Joro

Thank you! And I’ll also need to add the below so it actually updates the record? gr.setForceUpdate(true);

Community Alums
Not applicable

I use all of these when doing such things:

 

gr.setWorkflow(false);

gr.autoSysFields(false);

gr.setForceUpdate(true);

 

If this is for single use - well defined condition  ad no further bus. rules to be executed on this record its fine but if you need other BRs to run after that set gr.setWorkflow() to true ( gr.setWorkflow(true) ). Otherwise notifications, approvals, BRs, etc. stop being executed in the current context.