The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Should I use a Before or an After Business Rule?

anirban300
Kilo Guru

Hello Team,

 

I want to copy worknotes from Incident Task to incident. For which I have created a before business rule which is working fine but when I am using the same code in an after business rule then it's not working. Can someone tell me why?

 

Code:

 

(function executeRule(current, previous /*null when async*/) {

var inc = new GlideRecord('incident');
inc.addQuery('sys_id', current.incident);
inc.query();
if(inc.next())
{
inc.work_notes = "Worknotes from " + current.number +':'+"\n\n" + current.work_notes;
inc.update();
}

})(current, previous);

4 REPLIES 4

-Andrew-
Kilo Sage

Hello!

 

 

The difference between a before business rule and an after business rule is the timing of their execution.

 

A before business rule runs before any changes are made to the record, while an after business rule runs after changes have been made to the record. This means that in an after business rule, any changes made to the record will trigger another update and could potentially create an infinite loop if not handled properly.

 

In your code, you are updating the 'incident' record in the after business rule, which triggers another update on the same record. This could cause an infinite loop and potentially cause performance issues.

 

To avoid this, you can move the code to a before business rule or you can add a condition to check if the work notes have already been copied to the incident record before updating it again.

 

For example, you can add the following condition before updating the 'incident' record:

 

 
if (inc.work_notes.indexOf(current.work_notes) == -1) {
// rest of the script
}

This condition checks if the work notes from the incident task have already been copied to the incident record by checking if the work notes exist in the 'work_notes' field of the incident record. If the work notes do not exist, then the code proceeds to copy the work notes to the incident record.

I hope this helps!

Bert_c1
Kilo Patron

Hi,

 

Your script logic should work when run Before or After assuming the BR is defined on the incident_task table.  You can use the Script Debugger, or add gs.info() messages and then check Script Log Statements.  You may want to add:

 

inc.setWorkflow(false);

 

just before the "inc.update();" So none of the incident table related processing runs. Unless for example, you want notifications sent on the incident update for the new worknote added by the BR.

Hi Bert,

 

I used gs.info but it is coming as blank on OnAfter BL.

 

Regards

Anirban

Bert_c1
Kilo Patron

I tested in my instance, and when the BR is set to run after, the 'current.work_notes' is empty.  I tested in Utah Patch 0.  I did get a new work_note, with just "Worknotes from TASK0020599:" (the task from my testing). I don't know why current.work_notes is empty when the BR runs After, it seems other processing may be in play here. Maybe a Support Case is needed at this point in time.