How to avoid duplicate comments when copying from SCTASK to RITM?

Matt Cordero1
Tera Guru

Hello all,

I have a requirement that when a user adds a comment to a RITM (Request Item), that comment is pushed (down) to all associated SCTASKs.  In addition, when a comment is added to the SCTASK, it goes up to the RITM.

When I add a comment to the RITM, a comment is added to the SCTASK's work notes:

RITM:find_real_file.png

SCTASK:

find_real_file.png

The issue is when I add a comment to the SCTASK, it adds a comment to the RITM (this is desired), but then the Business Rule adds a comment BACK to the SCTASK (this is not desired).

RITM:

find_real_file.png

SCTASK:

find_real_file.png

Here is the code for the Business Rule.

Table: Task

When to run: Before

Actions: None

Advanced:

(function executeRule(current, previous /*null when async*/) {
// Get the current table
var tableName = current.getTableName();
if(tableName == 'sc_task')
{
// Comments for an SCTASK are being updated. Push them up to the RITM.
var gr_RITM = new GlideRecord('sc_req_item');
gr_RITM.addQuery('sys_id', current.request_item);
gr_RITM.query();

if(gr_RITM.next()){
// Check the Comments to avoid duplicate entries
if(previous.comments == current.comments.getJournalEntry(1))
{
return;
}
else
{
gr_RITM.comments = current.comments.getJournalEntry(1) + "The above comments have been added to " + current.number;
gr_RITM.update();
}
}
}

if(tableName == 'sc_req_item')
{
// Comments for the RITM are being updated. Push them down to all SCTASKs.
var gr_SCTASK = new GlideRecord('sc_task');
gr_SCTASK.addQuery('request_item', current.sys_id);
gr_SCTASK.query();

while(gr_SCTASK.next()) {
gr_SCTASK.work_notes = current.comments.getJournalEntry(1) + "The above comments have been added from " + current.number;
gr_SCTASK.update();
}
}
})(current, previous);

In the Business Rule, I have tried comparing the comments of the previous/current records to see if I can detect a duplication and avoid updating, but that's not working yet.

Any help would be appreciated!  😃

 

 

 

 

 

 

 

 

4 REPLIES 4

Shillu
Kilo Guru

A few things here:

In the first section, where you are updating the RITM record with the comments from STASK record, I would add the line before anything else, like below in red font.

var tableName = current.getTableName();
if(tableName == 'sc_task')
{

 current.setWorkFlow(False);


// Comments for an SCTASK are being updated. Push them up to the RITM.
var gr_RITM = new GlideRecord('sc_req_item');
gr_RITM.addQuery('sys_id', current.request_item);
gr_RITM.query();

 

this will prevent any other business rule(s) that were set to run on sc_task table.

1. For the ease of maintaining, it would be better to split the actions into business rules that run on the two separate tables. You can add conditions etc. down the road based on your needs if you separate the functions into two diffrent business rules acting on two separate tables.

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

So the work notes should be pushed from RITM to SCTASK only when somebody manually adds it and not when something updates from script

for both the RITM business rule and SC TASK business rule add this in condition field so that it only triggers when some user updated work notes and doesn't trigger when update happens from script

gs.getSession().isInteractive()
 
what above means is run the business rule only when it is interactive session
 
so this will avoid looping and updating work notes on both the places

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Matt,

Any update on this?
Can you mark my answer as correct, 👍 helpful if you were able to achieve the requirement & then close the thread. Thanks in advance.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Matt,

Any update on this?
Can you mark my answer as correct, 👍 helpful if you were able to achieve the requirement & then close the thread. Thanks in advance.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader