Copy comments from Ritm to catalog task without duplicates

pot
Tera Contributor

Hi all ,

Need help with script copying comments from Ritm to catalog task .I found some scripts in community but many are causing to duplicate comments in Ritm . And few posts says to use before business rule , I think it's not a best practise to use before br and updating records . So can any one help me without using before br and not causing duplicate comments .it would be very help ful.

Thanks in advanvce

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Never use before Business rule to update or insert in such scenarios.

Since you want to update another table i.e. sc_task when RITM comments gets updated you should always use After update BR

BR: Condition

Additional Comments Changes

Script:

use below script to copy the latest comments from RITM to SC Task

(function executeRule(current, previous /*null when async*/) {

	var sctask = new GlideRecord('sc_task');
	sctask.addQuery('request_item', current.sys_id);
	sctask.query();
	while(sctask.next()) {
		sctask.comments = current.comments.getJournalEntry(1);
		sctask.update();
	}
})(current, previous);

Regards
Ankur

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

View solution in original post

4 REPLIES 4

Jaspal Singh
Mega Patron
Mega Patron

Hi,

 

You can try below Before Insert/Update business rule that runs on RITM table when Comments | changes

(function executeRule(current, previous /*null when async*/ ) {

    var com = "RITM Comments for " + current.number + " : " + current.comments;
    if(com.indexOf('RITM Comments')>-1 && com.indexOf('Catalog Task comments')==-1)
{
    var ritmis = new GlideRecord('sc_task');
    ritmis.addQuery('request_item', current.sys_id);
    ritmis.query();
        if (ritmis.next()) {
           ritmis.comments= "RITM Comments for " + current.number + " : " + current.comments;
           ritmis.update();
        }
    }
})(current, previous);

 

Then the existing Catalog task business rule that copies comments need to be changed in format as above where simply replace 'RITM Comments' with 'Catalog Task comments'. In short simply use below for existing Catalog task business rule that copies comments.

(function executeRule(current, previous /*null when async*/ ) {

    var com = "Catalog Task Comments for " + current.number + " : " + current.comments;
    if(com.indexOf('RITM Comments')>-1 && com.indexOf('Catalog Task comments')>-1)
{
    var catis= new GlideRecord('sc_req_item');
    catis.addQuery('sys_id', current.request_item);
    catis.query();
        if (catis.next()) {
           catis.comments= "Catalog Task Comments for " + current.number + " : " + current.comments;
           catis.update();
        }
    }
})(current, previous);

To make it understand what we do is as below.

1. When comments are copied from RITM to Catalog task we add static text 'RITM comments'. Once done we check it in IF statement for the keyword. So, if keyword has 'RITM comments' & does not have 'Catalog Task' as keyword. Only then we will copy it to Catalog Task's comments field. 

2. So, now when Catalog Task business rule that copies comments to RITM is processed it will check for keyword that has 'Catalog Task comments' & does not have 'RITM comments' thus ensuring the comments are not copied back & forth thus avoiding duplication.

Hope it helps.

I have tried this, and have found the Before works, and After does not.

So have tried the above, in fact from another post you posted, however the RITM to Task works and the Task to RITM does not. Cannot see where the issue may lie.

 

Current instance version is Yokohama

 

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Never use before Business rule to update or insert in such scenarios.

Since you want to update another table i.e. sc_task when RITM comments gets updated you should always use After update BR

BR: Condition

Additional Comments Changes

Script:

use below script to copy the latest comments from RITM to SC Task

(function executeRule(current, previous /*null when async*/) {

	var sctask = new GlideRecord('sc_task');
	sctask.addQuery('request_item', current.sys_id);
	sctask.query();
	while(sctask.next()) {
		sctask.comments = current.comments.getJournalEntry(1);
		sctask.update();
	}
})(current, previous);

Regards
Ankur

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

Hello Ankur!

 

I used your solution because I need to copy additional comments from RITM to active SCTASK/SCTASKs ans it is working but not fully as I expected 😞

So from the beginning:

I have a business rule which copy additional comments from SCTASK to RITM and it is working good:

When to run: Before Insert Update; Condition: Additional comments changes, Parent Release is not empty

Script: 

(function executeRule(current, previous /*null when async*/ ) {

/* var gr_parent = new GlideRecord('sc_req_item');

if (gr_parent.get(current.parent))
{
gr_parent.comments = current.comments;
gr_parent.update();
}
*/

var gr_parent = new GlideRecord('sc_req_item');

if (gr_parent.get(current.parent)) {
gr_parent.comments = current.comments;
gr_parent.update();
}

})(current, previous);

 

And the second Business rule is to copy Additional comment from RITM to active SCTASK:

When to run: After Insert Update; COndition: Additional comments changes

Script:

(function executeRule(current, previous /*null when async*/ ) {

var sctask = new GlideRecord('sc_task');
sctask.addQuery('request_item', current.sys_id);
sctask.query();
while (sctask.next()) {
sctask.comments = current.comments.getJournalEntry(1);
sctask.update();
}
})(current, previous);

 

So currently when I add the comment to RITM it looks as below:

Kasia5_1-1669887477782.png

In SCTASK as below:

Kasia5_2-1669887529207.png

 

May I ask you for help?