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

ok so you are creating a loop... the task updates the comments of the item, it cascades those comments back down to the tasks comments.. which promptly feeds them back to the item...



my recommendation would be to have the item add any additional comments to the task WORK NOTES... and the task add additional comments to the items Comments....



you will end up with a work note from the item ONCE on the task that triggered this.. .but this way all subsequently created tasks get that tasks additional comments.


I got my code working by changing it to this. The other options didn't work but when I added a comment in front of the comments then I was able to check the SCTASK number and if it matched then I didn't add it to that task. This prevented it from adding the comment more than once.



"Add Comments from RITM to all SCTASKs"


(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);


      while (sctask.next()) {


              if (comments.startsWith(sctask.number))


                      continue;


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


              var sctaskWorkNotes = sctask.work_notes.getJournalEntry(1);


              if (sctaskWorkNotes.indexOf(comments) < 0) {


                      sctask.work_notes = comments;


                      sctask.update();


              }


      }


})(current, previous);



"Add Comments from SCTASK to RITM"


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


      var comments = current.comments;


      var ritmComment = new GlideRecord('sc_req_item');


      ritmComment.get(current.request_item);


      ritmComment.addQuery('comments_and_work_notes', 'CONTAINS', comments);


      ritmComment.query();


      if (ritmComment.getRowCount() > 0)


              return;


     


      var sctask = new GlideRecord('sc_task');


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


      sctask.query();


      var ritmComment2 = new GlideRecord('sc_req_item');


      ritmComment2.get(sctask.request_item);


      ritmComment2.addQuery('comments_and_work_notes', 'CONTAINS', sctask.comments_and_work_notes.getJournalEntry(1));


      ritmComment2.query();


      if (ritmComment2.getRowCount() > 0)


              return;


     


      var ritm = new GlideRecord('sc_req_item');


      ritm.get(current.request_item);


      ritm.query();


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


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


      while (ritm.next()) {


              ritm.comments = current.number + ' Comments: ' + comments;


              ritm.update();


      }


})(current, previous);


Hi Kent,



I am currently testing your scripts but cannot get them to work in our instance. Are you adding any BR conditions or just a simple after insert BR?


Snehal2
Kilo Guru

Hi Kentgott,

I was also struggling to achieve this and finally found the solution to achieve it and this will not loop the commenting from RITM to SCTASK and vice versa.

You Can try out following:

LOGIC USED:

EG: To copy RITM additional comment to its SCTASKs -> compare the most recent comment on SCTASK with the RITM comment, if no match found then update additional comment on the SCTASK. Use the same logic for vica versa i.e sctask to RITM.

1. BR: Add Comments from RITM to all SCTASKs  (After Insert and update) Condition: on change of additional comments

//To Copy the Additional Comments from RITM to SCTASKs
    var compare,ritm_comment2,ritm_comment,task_comment2,task_comment;
	ritm_comment =current.comments.getJournalEntry(1);
	
	//Remove timestamp and name from additional comment
	var regex= new RegExp('\n');
	var i = ritm_comment.search(regex);
	if (i>0)
	{
		ritm_comment2 = ritm_comment.substring(i+1, ritm_comment.length);	
	}
	
	var task_gr = new GlideRecord('sc_task');
	task_gr.addQuery('parent', current.sys_id);
	task_gr.query();
	
	while(task_gr.next())
	{
		task_comment =task_gr.comments.getJournalEntry(1);
		
		//Remove timestamp and name from additional comment
		var i1 = task_comment.search(regex);
		if(i1 > 0)
		{
			task_comment2 = task_comment.substring(i1+1, task_comment.length);	
		}
		compare = ritm_comment2.indexOf(task_comment2);
		
		if(compare == -1)  // If No match found
		{
			task_gr.comments = ritm_comment2.trim();
			task_gr.update();
		}
	}	

2. BR: Add Comments from SCTASK to RITM(After Insert and update) Condition: on change of additional comments

//To Copy the Additional Comments from SCTASK to RITM
	var compare,task_comment2,task_comment,ritm_comment2,ritm_comment;
	task_comment =current.comments.getJournalEntry(1);
	
	//Remove timestamp and name from additional comment
	var regex= new RegExp('\n');
	var i = task_comment.search(regex);
	if (i>0)
	{
		task_comment2 = task_comment.substring(i+1, task_comment.length);
	}
	
	var ritm_gr = new GlideRecord('sc_req_item');
	ritm_gr.addQuery('sys_id', current.parent);
	ritm_gr.query();
	
	if(ritm_gr.next())
	{		
		ritm_comment =ritm_gr.comments.getJournalEntry(1);
		
		//Remove timestamp and name from additional comment
		var i1 = ritm_comment.search(regex);
		if(i1 > 0)
		{
			ritm_comment2 = ritm_comment.substring(i1+1, ritm_comment.length);
		}
		compare = task_comment2.indexOf(ritm_comment2);
		
		if(compare == -1)  // If No match found
		{
			ritm_gr.comments = task_comment2.trim();
			ritm_gr.update();
		}		
	}

Mark it correct if it helps you. Its tested code.

Regards,

Snehal Madakatti

Rajat15
Tera Contributor

Hi Snehal,

 

Your script is good, but if there are two tasks with the same ritm.. Then it wont work.. because if we update comment in task 1, the same will update in the RITM.. which in turn will update the TASK2.

 

Any Suggestions for that.