To execute specific business rule on table.

Tarasingh26
Tera Expert

Hi All,

 

My requirement is to run one specific business rule in script. Ideally there are numerous BRs implemented on table. In order to disable them, we write setWorkflow(false) but my requirement is to disable all rules but execute one business rule. How this can be achieved ? Thanks in advance.

 

Thanks,

Tara Singh

1 REPLY 1

Markus Kraus
Kilo Sage

Best Practice Option (this is also how the Asset and CI Field Sync works btw):
If you have full control over the table and business rule:

Create a new field, e.g. named 'perform_update' (True/False), then you can use this field to control if the business rules should run.
I do not recommend ever using setWorkflow(false) as this typically shows a design flaw in your application.

 

Bad Practice Option 1: Make the Business Rule you want to run first use the lowest order. Then, inside the one Business Rule which *should* run, add a "current.setWorkflow(false)" at the bottom - this will stop further business rule processing

 

Bad Practice Option 2:

You can (but you shouldn't) execute the specific business rule manually 

var taskGr = new GlideRecord('task');
taskGr.addQuery('number', 'INC......');
taskGr.setLimit(1);
taskGr.query();
if (taskGr.next()) {
  taskGr.work_notes += 'Some Dummy Update';
  taskGr.setWorkflow(false);
  taskGr.update();

  // execute business rule manually (note: previous cannot be used)
  var scriptGr = new GlideRecord('sys_script');
  if (scriptGr.get('SYS_ID_OF_BUSINESS_RULE')) {
    var gse = new GlideScopedEvaluator();
    gse.putVariable('current', taskGr);
    gse.putVariable('previous', null);
    gse.evaluateScript(scriptGr, 'script');
  }
}