logo

NJP

Copy Additional comments from RITM to SC Task and Vice Versa

Import · Apr 07, 2022 · article

Step 1 ) Create a business rule as below. (for Copying additional comments to SC Task from RITM)

Table : sc_req_item

When : After , update

Filter condition : Additional comments : changes

Advanced 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);

Step 2 ) Create a business rule as below. (for Copying additional comments From SC Task to RITM)

Table : sc_task

When : After , update

Filter condition : Additional comments : changes

Advanced Script :

(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);

Hope you will find it as helpful. Don’t forget to Mark it Helpful and Bookmark article so you can easily find on your profile.

Thank you,Dinesh Kumar Raghu,

Capgemini India Pvt Ltd.

GmailID : [email protected]

View original source

https://www.servicenow.com/community/itsm-articles/copy-additional-comments-from-ritm-to-sc-task-and-vice-versa/ta-p/2300159