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.

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

Thanks! This is very helpful.

Joro makes a good point, so it would look like this:

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

while (current.next()) {

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


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

        // Do not run any other business rules
        gr.setWorkflow(false);

        // Update silently (no audit or metadata updates)
        gr.autoSysFields(false);

        // Update record even if no changes

        gr.setForceUpdate(true);

	// Update the record
	current.update();

}

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

Thank you! I will test this out today.

Hi Paul,

I updated the script you provided:

On line 30, it gives me a warning "do not make functions within a loop" message. Should I ignore?

I am trying to trigger each case on this table to update so my BR can be triggered.

Thank you!
Kyle

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

while (current.next()) {

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

var categoryCountOpen = 0;

var categoryCountClosed = 0;

	var gr = new GlideAggregate('sn_hr_core_case'); gr.addEncodedQuery('stateIN1,10,11,18,20,24^assignment_groupLIKEHRSS - US'); //For All open

gr.addAggregate("COUNT");

gr.query();

if(gr.next()) {//Use if not while

categoryCountOpen = gr.getAggregate("COUNT");

current.u_all_open = categoryCountOpen ;

} 

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

        // Do not run any other business rules
        gr.setWorkflow(false);

        // Update silently (no audit or metadata updates)
        gr.autoSysFields(false);

        // Update record even if no changes

        gr.setForceUpdate(true);

	// Update the record
	current.update();

}