Copying additional comments from SCTASK to RITM and RITM to all SCTASKs

kentgott
Kilo Explorer

I have a project to add additional comments from a SCTASK to the RITM and also add the additional comments from a RITM to all the SCTASKs under that RITM. I'm testing this on my personal dev instance.

I have created these two business rules:

Add Comments from RITM to all SCTASKs

Add Comments from SCTASK to RITM

I have tested them individually and they work as far as pushing the additional comment from the RITM to all the SCTASKs and also from the SCTASK to the RITM. Originally I only had one of them active during my testing. But when I activated both of them, then it adds two comments to the RITM or SCTASK which I don't want to happen.

I'm hoping someone knows what I need to do so the business rules won't execute so the RITM or SCTASK gets two comments. I only want the comment added one time when pushing it to the other item. I have tried multiple codes but still don't have it working.

When I add an additional comment to the SCTASK, I want it added to the SCTASK and to the RITM only once. When I add an additional comment to the RITM, I want it added to the RITM and to all the SCTASKs under that RITM only once in all of them. Apparently the business rules execute a couple times which has caused them to add the comment twice to the RITM or SCTASK. I still haven't figured out how to only add the comment once. I'm really hoping someone has an answer for this.

15 REPLIES 15

Dave Smith1
ServiceNow Employee
ServiceNow Employee

Do you actually want to copy them... or do you want to make their content visible on related records?



i.e.: when someone looks at the RITM, they can see all entries in the related SCTASKs.


Yes, I want the additional comment from the SCTASK added to the RITM. I want the additional comment from the RITM added to all the SCTASKs under that RITM.


sumeet_n
Kilo Guru

You may find below discussion helpful.



How to kill a BR running in loop?


kentgott
Kilo Explorer

This code worked fine when I only had one of them active. But when I made them both active then I got duplicate comments in the RITM and SCTASK.



Here is the code I have tried for "Add Comment from RITM to all SCTASKs". I have tried sctask.setWorkflow(false) but it didn't stop the duplicate comments.


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


      var comments = current.comments;


      var sctask = new GlideRecord('sc_task');


      sctask.addQuery('request_item.number', current.number);


      sctask.query();


      var ritmComments = current.comments.getJournalEntry(1);


      //sctask.setWorkflow(false);


      while (sctask.next()) {


              var sctaskComments = sctask.comments.getJournalEntry(1);


              if (ritmComments.indexOf(sctaskComments) < 0) {


                      //sctask.setWorkflow(false);


                      sctask.comments = comments;


                      sctask.update();


                      //sctask.setWorkflow(false);


              }


      }


})(current, previous);




Here is the code I have tried for "Add Comment from SCTASK to RITM". I have tried ritm.setWorkflow(false) but it didn't stop the duplicate comments.


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


      var ritm = new GlideRecord('sc_req_item');


      ritm.get(current.request_item);


      //ritm.setWorkflow(false);


      var sctaskComments = current.comments.getJournalEntry(1);


      var ritmComments = ritm.comments.getJournalEntry(1);


      if (sctaskComments.indexOf(ritmComments) < 0) {


              var comments = current.comments;


              //ritm.setWorkflow(false);


              ritm.comments = comments;


              ritm.update();


      }


      //ritm.setWorkflow(false);


})(current, previous);