Need to copy comment from RITM to sc_task

s_nandhini
Tera Contributor

I need to copy my additional comment from RITM to sc_task . But my sc_task contain group type "Restriction" Only user part of sc_task can able to see the sc_task. So, comment is copying properly.

 

I tried creating an event + BR + script action but it not working

 

BR: (After - update)

 

(function executeRule(current, previous /*null when async*/ ) {
    if (current.comments.changes()) {
     var rawEntry = current.comments.getJournalEntry(1);
    var latestComment = rawEntry.split("\n")[1];
        var userId = gs.getUserID();
       
    gs.log("BR firing event ritm.comment.added");
    gs.log("Latest comment: " + latestComment);
    gs.log("User ID: " + userId);
        gs.eventQueue("ritm.comment.add", current, latestComment, userId);
    }
})(current, previous);
 
Script action:
(function executeAction(event) {

    gs.log("latest: " + event.parm1);
    gs.log("comm: " + event.parm2);

    if (!event.parm1) {
        gs.log("No comment text passed to Script Action");
        return;
    }
   
    var tasks = new GlideRecord("sc_task");
    tasks.addQuery("request_item", current.sys_id);
    tasks.query();
    if (tasks.next()) {
        gs.log("commenttask:" + event.parm1);
        tasks.comments.setJournalEntry(event.parm1);
        tasks.update();
       
    }else{
        gs.log("commentnotfound:"+ event.parm1 + current.sys_id + gs.getUserName());
    }
})(event) ;
 
But commentnotfound log is running instead of if.

 

1 ACCEPTED SOLUTION

@s_nandhini 

in after update BR on RITM, no script action required

BR Condition: Comments Changes

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

    var sctask = new GlideRecord('sc_task');
    sctask.addQuery('request_item', current.sys_id);
    sctask.setWorkflow(false); // disables query BR and allows script to query that record
    sctask.query();
    while (sctask.next()) {
        var comments = current.comments.getJournalEntry(1).match(/\n.+/gm).join("\n");
        comments = comments.replace('\n', '');
        sctask.comments = comments;
        sctask.update();
    }

})(current, previous);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

16 REPLIES 16

If something i add in sc_task it will get copy to RITM but now if i add testing in sc_task it is copying to RITM but again the same comment copying from RITM to sc_task duplicate comment.

 

s_nandhini
Tera Contributor

s_nandhini_0-1777295971101.png