Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Copy Additional comments from RITM to SCTASK

Kasia5
Tera Contributor

Hello All!

 

I used solution from https://www.servicenow.com/community/it-service-management-forum/copy-comments-from-ritm-to-catalog-... 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-1669898155928.png

 

In SCTASK as below:

Kasia5_2-1669898155874.png

 

 

May I ask you for help?

I need to change the second business rule (copy add com from RITM to SCTASK) to work as the first one.,

I also need a condition to check if this copied additional comment from RITM is NOT from the same user as from field 'Assigned to' from SCTASK form.

 

How can I achieve this?

 

Thanks in advance for help!

5 REPLIES 5

James-B
Kilo Sage

For the sake of simplicity it would probably be easier to achieve your second via Flow Designer, you could then also easily add a check for your assigned to too. 

 

FYI - You cant create a flow directly on the sc_req_item table but you can on task with a task type of "Request Item".

Saurav11
Kilo Patron
Kilo Patron

Hello,

 

Create a after insert/update BR on sc_req_item table as below:-

 

Saurav11_0-1669899066508.png

 

 

Write the below code in the script:-

 

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

updateTasks();

function updateTasks() {
var compare,task_comment2,task_comment,ritm_comment2,ritm_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 ritm_gr = new GlideRecord('sc_task');
ritm_gr.addQuery('request_item', current.sys_id);
ritm_gr.query();

if(ritm_gr.next())
{
task_comment =ritm_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);
}
compare = ritm_comment2.indexOf(task_comment2);

if(compare == -1) // If No match found
{
ritm_gr.comments = ritm_comment2.trim();
ritm_gr.update();
}
}
}
})(current, previous);

 

 

Then  Wriye an after insert/update BR on sc_task table as below.:-

 

Saurav11_1-1669899066252.png

 

 

I think you already have created the BR for sc_task table with the above condition. So just update the code to below:-

 

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

// Add your code here

var compare,task_comment2,task_comment,ritm_comment2,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.parent);
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);
}
compare = task_comment2.indexOf(ritm_comment2);

if(compare == -1) // If No match found
{
ritm_gr.comments = task_comment2.trim();
ritm_gr.update();
}
}

})(current, previous);

 

Please check the below post correct answer:-

 

https://www.servicenow.com/community/developer-forum/comments-updated-in-sctask-should-reflect-in-ri...

 

Please mark my answer as correct based on Impact.

Kasia5
Tera Contributor

Thank you!

On table sc_task is working fine but it is not working for sc_req_item table..

When I add the additional comment to RITM it is not copied to the SCTASK

Thank you so much!! This worked great for me and witout any issues.