work notes duplicating when copying between task and item

chrisp1
Mega Sage

Hi,

I have a requirement to have comments and work notes copied from items to task and vice-versa, so I have created 2 business rules to do this (as below) - the issue is that i get duplicate entires (eg a work note added to the item is copied to the task, but then as the work notes on teh task have changed this is then copied back to the item again)

Is there a better way of doing this which will avoid the duplication?

( i have tried adding the request item comments and work notes to the task form, but there does not seem to be the option to go the other way, and this still contains duplications)

thanks in advance

- before update on sc_req_item (when add comm/wn change)

var gr = new GlideRecord('sc_task');

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

gr.query();

while(gr.next()){

gr.comments = current.comments;

gr.work_notes = current.work_notes;

gr.update();

}

- before update on sc_task (when wn change)

var gr = new GlideRecord('sc_req_item');

gr.get(current.request_item);

gr.work_notes = current.work_notes;

gr.update();

11 REPLIES 11

Hello friends,





I had the same issue with the duplication of the comments.
Well, my scenario was slightly different.
My requirement was to ensure that the comments left in the Request (sc_request) will become visible in the Catalog Task (sc_task) Activity item and vise versa,
or in other words: the comments of the Catalog Task (sc_task) will become visible in the Activity item of the Request (sc_request).



So, we have 3 layers here:


Request (sc_request, Layer 1) > Requested Item / RITM (sc_req_item, Layer 2) > Catalog Task / TASK (sc_task, Layer 3).
I needed to ensure the forwarding of the comments from Layer 1 to Layer 3 and from Layer 3 to Layer 1 will go smoothly without any duplicate comments to appear.



To do so, I created two Business Rules. One to run against Request (sc_request) table and the other one to run against Catalog Task (sc_task) table.
All went fine, but I noticed duplication of the comments being forward between the tables. I tried everything I can think of to deal with this problem, but nothing gave a result. So, I started checking the ServiceNow Community for any useful suggestions. I found several threads on the same topic, but there was no real solution provided in them. Thus, I continued to "hit my head in the wall" for 24 hours more. At the end, I finally came up with a solution.
I decided to be smarter than the system and added the below line of code into the scripts of both the Business Rules of mine:


  gr.comments = '###     :     '+current.comments;


This way - the automatic forwarding, to say so, of duplicate comments between Request (sc_request) and Catalog Task (sc_task) tables and the other way around stopped.
Then, I decided to modify the above line of code, in order to optimize the work of my Business Rules in front of the end customers.
To do so, I changed the above code in both the Business Rules in the following way:



- Business Rule against Request (sc_request) table, which sends the comments of it to the Catalog Task (sc_task) table's Activity Item:
gr.comments = '### Customer reply: ' + current.comments;



- Business Rule against Catalog Task (sc_task) table, which sends the comments of it to the Request (sc_request) table's Activity Item:
gr.comments = '### Fulfiller reply: ' + current.comments;



Now, thanks to this modification - there are no duplicate comments, from one side and the comments coming from the other tables are better looking.
Please check the below screenshots for references:



1. A view from the Activity Item of the Request (sc_request) table which will be visible to the customers. Whenever a fulfiller (a user having access to the Catalog Task (sc_task) table replies to / write a comment -> the customer will see the following in the Request (sc_request) view):


Customer1.png


2. A view from the Activity Item of the Catalog Task (sc_task) table which will be visible to the fulfillers. Whenever a customer (a user having access to the Request (sc_request) table replies to / write a comment -> the fulfiller will see the following in the Catalog Task (sc_task) view):
Customer2.png



As you see from the screenshots - adding the above line of code into both the Business Rules has successfully resolved the issue with the duplication of the comments between the two tables!




I believe this information might be useful / helpful to you, and have the same positive result in your scenarios too, guys!
You just need to use my scenario and implement the appropriate changes in your ones (scenarios).




For more references, below you will find the scripts of my Business Rules:



1. Business Rule against Request (sc_request) table which moves the comments added there into the Activity Item of the Catalog Task (sc_table):


Table: Request (sc_request),
When to Run: Before, Update;


Request1.png



2. Business Rule against Catalog Task (sc_task) table which moves the comments added there into the Activity Item of the Request (sc_request):
Table: Catalog Task (sc_task);
When to Run: Before, Update;


Task1.png




P.S: Both the Business Rules above can be modified to cover different scenarios and to work not only for comments but also for work_notes.
To do so, just take under consideration the Condition and the scripts. Change both in the appropriate way and everything will be OK without any duplicate comments to appear.
You also may decide what text to add in the prefix. It's up to you




Best Regards,


Georgi Mavrodiev



IT Consultant


Do IT Wise



You may visit us in our Web Site: www.doitwise.com


jadona
Giga Contributor

Maybe the problem is that OOTB the comments are being copied in the work notes field, so when you add a text in the comments fields both the OOTB functionality and your new BR are doing a similar action.


I suggest you to change your BR to something like this:



Condition: !current.work_notes.nil() || !current.comments.nil()


Script: updateNotes();



function updateNotes(){


if(!current.comments.nil()){
text += 'Additional Comments added from Source Task: ' + current.comments + '\n';
}
if(!current.work_notes.nil()){
text += 'Work notes added from Source Task: ' + current.work_notes + '\n';
}
taskgr.work_notes = text;
taskgr.update();

}



This updates in the work notes field the changes done in both the comments and the work notes field.



I hope it helps!