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

JamesEcoStratus
Mega Guru

Hi, I’m not sure what ServiceNow version you’re utilizing or the details of your business case. However, based on my understanding of your question from your provided information, my answer below should do the trick.


To avoid the issue of duplicating comments between HR Case and HR Task, you can modify your ServiceNow business rule to include a condition that checks whether the current comment was created by the business rule itself before copying it to the other record. You can do this by setting a flag when copying a comment to indicate that it has already been processed.

 

Here's a script example for copying comments from HR Case to HR Task and vice versa while preventing duplicates:

  1. Create a field on both HR Case and HR Task tables to store a flag indicating that a comment has been processed by the business rule. Let's call this field comment_copied.
  2. Modify your business rule script on the HR Case table (and similarly on the HR Task table) to include a condition that checks whether the comment has already been copied. If it hasn't, copy the comment and set the comment_copied flag to true. If it has already been copied, do nothing.

Here's a sample script for the HR Case table:

Screenshot 2023-09-06 at 12.14.19.png

 

  1. Similarly, create a business rule on the HR Task table to copy comments from HR Task to HR Case with a similar script, but adjust the table and field names accordingly.

 

By adding the comment_copied flag, you can prevent the business rule from repeatedly copying the same comment back and forth between the HR Case and HR Task. This way, each comment is processed only once by the business rule.

 

Good Luck!

 

James @Ecostratus

 

If I helped you with your question, then please hit the Thumb Icon and mark it as Helpful or Correct.

Hi @JamesEcoStratus 
I haven't checked that yet, but I'm assuming it works based on the provided solution. I'm trying to avoid adding custom fields unless the business approves it. This might be a decent workaround.
Many thanks