How would I prevent a Business Rule from running twice?

Shant Cancik
Tera Contributor

I have two Business Rules set up.

One business rule is on the sc_req_item table and copies comments on the RITM record as work Notes on the Task record.

Table: sc_req_item

Runs on: After

Condition: current.comments.changes()

The second business rule is on the sc_task table and copies comments on the Task record to comments on the RITM record.

Table: sc_task

Runs on: After

Condition: current.comments.changes()

This is working fine for the most part. If a user comments on the RITM, the first business rule runs and copies that comment as a work note to the Task record. When a fulfiller eventually types a comment on this Task record, that comment gets sent over to the RITM record as a RITM comment. This is great and the functionality I want.

My problem is that when this second business rule runs and updates the RITM record, my first business rule gets triggered because its condition is waiting for that field to change. This leads to duplicate text on my Task record.   I've been trying to fix this but am getting nowhere. Anyone have an idea of what I can do about this?

1 ACCEPTED SOLUTION

Brian Dailey1
Kilo Sage

Hi Shant,



You can place a global variable in your business rule to indicate where the update originated (i.e., from which task).   This variable will persist only while that particular update is being processed.   For example...



For your Business Rule on [sc_task]:


var updateInitiatedBy = updateInitiatedBy || current.sys_id.toString();



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


      if(updateInitiatedBy == current.sys_id.toString()){


              var gr = new GlideRecord('sc_req_item');


              gr.addQuery('sys_id',current.request_item.sys_id.toString());


              gr.query();


              while(gr.next()){


                      gr.comments = current.comments;


                      gr.update();


              }


      }


})(current, previous);




For your Business Rule on [sc_req_item]:


var updateInitiatedBy = updateInitiatedBy || current.sys_id.toString();



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


      if(updateInitiatedBy == current.sys_id.toString()){


              var gr = new GlideRecord('sc_task');


              gr.addQuery('request_item',current.sys_id);


              gr.query();


              while(gr.next()){


                      gr.comments = current.comments;


                      gr.update();


              }


      }


})(current, previous);




Only the rule which fires initially and sets the variable 'updateInitiatedBy' will initiate a GlideRecord and update the other RITM or TASK record associated with its 'current' record.   Also, it is important to make these run as 'Before' business rules (I believe) because you are dealing with Journal Entry fields which will no longer contain a value after the update has been committed to the database.



Give that a try and let me know if you have any questions.




Thanks,


-Brian





Edit:   I changed the global variable's name to 'updateInitiatedBy' for the sake of code clarity.


View solution in original post

15 REPLIES 15

Great Brian, That's work's for me too.Thank you much!!

It didn't worked for Me. My scenario is Case comments should be updated into Incident Work notes and Incident comments to Case comments.

Once comments are added into Incident, same goes to Case as comments and then the BR Case table updates the same back to Incident work notes

BR on Case table

var updateInitiatedBy = updateInitiatedBy || current.sys_id.toString();
(function executeRule(current, previous /*null when async*/ ) {

if(updateInitiatedBy == current.sys_id.toString()){
var inc = new GlideRecord('incident');
inc.addQuery('parent', current.getUniqueValue());
inc.query();
if (inc.next()) {
inc.work_notes = current.comments.getJournalEntry(1).toString().split('\n')[1];
inc.update();
}
}

})(current, previous);

 

BR on Incident table

var updateInitiatedBy = updateInitiatedBy || current.sys_id.toString();
(function executeRule(current, previous /*null when async*/ ) {

if (updateInitiatedBy == current.sys_id.toString()) {
var CaseRec = new GlideRecord('sn_customerservice_case');
CaseRec.addQuery('incident', current.getUniqueValue());
CaseRec.query();
if (CaseRec.next()) {
CaseRec.comments = current.comments.getJournalEntry(1).toString().split('\n')[1];
CaseRec.update();
}
}

})(current, previous);

It didn't worked for Me. My scenario is Case comments should be updated into Incident Work notes and Incident comments to Case comments.

Once comments are added into Incident, same goes to Case as comments and then the BR Case table updates the same back to Incident work notes

BR on Case table

var updateInitiatedBy = updateInitiatedBy || current.sys_id.toString();
(function executeRule(current, previous /*null when async*/ ) {

if(updateInitiatedBy == current.sys_id.toString()){
var inc = new GlideRecord('incident');
inc.addQuery('parent', current.getUniqueValue());
inc.query();
if (inc.next()) {
inc.work_notes = current.comments.getJournalEntry(1).toString().split('\n')[1];
inc.update();
}
}

})(current, previous);

 

BR on Incident table

var updateInitiatedBy = updateInitiatedBy || current.sys_id.toString();
(function executeRule(current, previous /*null when async*/ ) {

if (updateInitiatedBy == current.sys_id.toString()) {
var CaseRec = new GlideRecord('sn_customerservice_case');
CaseRec.addQuery('incident', current.getUniqueValue());
CaseRec.query();
if (CaseRec.next()) {
CaseRec.comments = current.comments.getJournalEntry(1).toString().split('\n')[1];
CaseRec.update();
}
}

})(current, previous);

Raghu Loganatha
Kilo Guru

Shant,



I believe you can use setWorkflow(false); condition in your second BR and prevent other BR's getting triggered.
Note: This will deactivate all BR's on that record after the second one runs. You can use this only if you don't want other BR's to tigger.


Arindam Ghosh
Mega Guru

As you said any of the suggestions are working, you can try the below way:



Create a flag in sc_req_item table. Set default value false. Now add one more condition in business rule (flag = false).and also make the flag true at the end of your business rule code. By this way you can restrict that business rule to run only once.



Thanks,


Arindam