RITM Comments are not flowing to task

Manohar Reddy
Tera Contributor

Hi Team,

 

I am having a wierd issue where in if I post a comment on RITM as 'hi' then it is not flowing to catalog task.

 

Again if I post any thing other than 'hi' the RITM is cascading the comments to catalog task as expected.

 

Can someone please assist me on fixing this issue.

 

Thanks in Advance.

6 REPLIES 6

Manohar Reddy
Tera Contributor

Hi @Peter Bodelier @Brad Bowman  ,

 

I have written a before update BR which is running on RITM table and below is the script:

 

    var c_changed1 = current.comments.changes();
    var cmt = current.comments.getJournalEntry(1).toString().split("))");
    var cmt1 = cmt[1].trim();
    
    var ctask = new GlideRecord('sc_task');
    ctask.addActiveQuery();
    ctask.addQuery('parent',current.sys_id);
     ctask.query();
    
    if (ctask.next()) {
        if (c_changed1) {
              ctask.comments = "Requested Item "+ current.number + " had comments added:\n " + current.comments;
            }
       ctask.update();
    }

I would recommend running this after update since you are updating a different record/table, with the Filter Condition = 'Additional comments changes'.  While I don't initially see anything in your script that would not work with 'hi', we have been doing this for years now and are on our second iteration of logic that I found on the Community.  Prior to this, troubleshooting was a major headache due to inconsistent results, so here's a tried and true method.  We have a similar rule on the sc_task table to copy comments from a task to the RITM, so this avoids recursion, duplication, and takes into consideration if this is the first journal entry/comment to be copied - which was a tricky part when introducing the rule to copy from Catalog Tasks to the RITM.

(function executeRule(current, previous /*null when async*/) {
	//To Copy the Additional Comments from RITM to SCTASKs
    var compare = '';
	var ritm_comment2 = '';
	var ritm_comment = '';
	var task_comment2 = '';
	var 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('request_item', current.sys_id);
	task_gr.addQuery('active', 'true');
	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);
			var numberstring = current.number + ' - ';
			var regex2= new RegExp(numberstring);
			var i2 = task_comment2.search(regex2);
			if(i2>=0) {
				task_comment2 = task_comment2.substring(i2+numberstring.length, task_comment2.length);
			}
		} else {
			task_comment2 = 'empty';
		}
		var numberstring2 = task_gr.number + ' - ';
		var regex3= new RegExp(numberstring2);
		var i3 = ritm_comment2.search(regex3);
		if(i3>=0) {
			ritm_comment2 = ritm_comment2.substring(i3+numberstring2.length, ritm_comment2.length);
		}
		
		compare = ritm_comment2.indexOf(task_comment2);
				
		if (compare != 0) {// if no match found
			task_gr.comments = current.number + ' - ' + ritm_comment2.trim();
			task_gr.update();
		}
	}	
})(current, previous);

Here's the accompanying BR after Update on the sc_task table with the same Filter Condition:

(function executeRule(current, previous /*null when async*/) {
	//to copy the Additional Comments from SCTASK to RITM
	var compare = '';
	var task_comment2 = '';
	var task_comment = '';
	var ritm_comment2 = '';
	var 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.request_item);
	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);
			var numberstring = current.number + ' - ';
			var regex2= new RegExp(numberstring);
			var i2 = ritm_comment2.search(regex2);
			if(i2>=0) {
				ritm_comment2 = ritm_comment2.substring(i2+numberstring.length, ritm_comment2.length);
			}
		} else {
			ritm_comment2 = 'empty';
		}
		
		var numberstring2 = ritm_gr.number + ' - ';
		var regex3= new RegExp(numberstring2);
		var i3 = task_comment2.search(regex3);
		if(i3>=0) {
			task_comment2 = task_comment2.substring(i3+numberstring2.length, task_comment2.length);
		}
		
		compare = task_comment2.indexOf(ritm_comment2);
		
		if(compare != 0) {  // If no exact entire match found
			ritm_gr.comments = current.number + ' - ' + task_comment2.trim();
			ritm_gr.update();
		}		
	}
})(current, previous);

Note that these will not copy an identical comment posted consecutively, regardless of time/date difference.