Brad Bowman
Mega Patron

If you want to get a little fancier, here's the current iteration of the scripts we have been using to which add the Task/RITM number for reference, prevent recursion, and won't copy duplicates when users repeatedly enter the same comment for whatever reason.

 

The first rule, on the sc_req_item table after Update when Additional comments changes:

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

 

The second rule, on the sc_task table after Update when Additional comments changes:

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

 

View solution in original post

Thank you for your response.  These scripts may be overkill for what we are trying to accomplish.  Essentially, I just need the script I provided in my initial question modified so any SCTASK entered additional comment rolls up and is added to the RITM additional comments in the same manner I have set with comments rolling from RITM to SCTASK.  

See Brad's solution here:

 

https://www.servicenow.com/community/developer-forum/how-to-update-sc-task-and-ritm-when-requested-f...

 

Much simpler than what he proposed above.

We had to depart from the simple approach once we had task comments updating the RITM and vice-versa as comments were duplicating in a recursive manner.

Brad,

Thank you for providing these script examples, this ended up working better than my initial solution.  I was noticing the duplicate entries and it was rather confusing to parse information in the activity feed.  Your above solution works perfect in that it allows us to roll comments up and down respectively and eliminate duplicate entries.  This will be huge for our dept to have implemented!  Many thanks again