How to copy comments from HR Case to HR Task and vice versa

miro2
Mega Sage

Hi,
I'm trying to solve issue with copying comments from HR Case to HR Task and vice versa. My requirement is to copy comments (not work notes).
I'm using business rules on after (update) but when I add comment in HR Case
The problem arises when a comment is added to the HR Case, which is then copied to the HR Task as intended. However, this action of copying the comment to the HR Task triggers the Business Rule again I think and causing the same comment to be copied back to the HR Case. This results in the comment being duplicated in the HR Case. The same is when I add comment in HR Task.
I have checked this community article but I haven't found solution. They provided solution but it was related to copying work notes and commenting OOB script (I don't want to modify anything that is OOB).
Any suggestions what should be changed in scripts?

Business rule: Copy comment from Case to Task

miro2_2-1694022899341.png

 



miro2_1-1694022876544.png

 

 

 

 

 

 

   var hrTask = new GlideRecord('sn_hr_core_task');
    hrTask.addQuery('parent', current.sys_id);
    hrTask.query();

    if (hrTask.next()) {
        var comment = current.comments.getJournalEntry(1);
        hrTask.comments = comment;
        hrTask.update();
    }

 

 

 

 

 

 



Business rule: Copy comments from Task to Case

miro2_3-1694022994250.png

 

miro2_4-1694023033625.png

 

 

 

 

 

 

 

   var hrCase = new GlideRecord('sn_hr_core_case');
    hrCase.addQuery('sys_id', current.parent);
    hrCase.query();
    if (hrCase.next()) {
        var comment = current.comments.getJournalEntry(1);
        hrCase.comments = comment;
        hrCase.update();
    }

 

 

 
1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

We've been doing this for years, with a couple iterations on approaches tweaked from examples found in the Community, so it's tried and true by this point accounting for the target record not having any existing comments, and checking the most recent entry to precent recursion.  This example uses the Requested Item (sc_req_item) and Catalog Task (sc_task) tables, but will operate exactly like the sn_hr_core_case and the sn_hr_core_task tables.  On each rule you'll want to add a Filter Condition, so that the rule only runs when the record is updated with a comment:

BradBowman_0-1694026187875.png

On the task table:

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

On the case table:

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

 

View solution in original post

6 REPLIES 6

Brad Bowman
Kilo Patron
Kilo Patron

We've been doing this for years, with a couple iterations on approaches tweaked from examples found in the Community, so it's tried and true by this point accounting for the target record not having any existing comments, and checking the most recent entry to precent recursion.  This example uses the Requested Item (sc_req_item) and Catalog Task (sc_task) tables, but will operate exactly like the sn_hr_core_case and the sn_hr_core_task tables.  On each rule you'll want to add a Filter Condition, so that the rule only runs when the record is updated with a comment:

BradBowman_0-1694026187875.png

On the task table:

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

On the case table:

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

 

Hi @Brad Bowman  
your solution works like a charm, many thanks

 
 
 

You are welcome!

just use  \n will give error

var regex = new RegExp('\\n');
NOTE:\\n: In JavaScript, the backslash \ is an escape character, so you need to escape the backslash itself by using \\. This ensures that the regex interprets it correctly as a newline character.