Sync additional comments between sc_task and request

JP6
Tera Contributor

Currently, when a requester submits a request through the ServiceNow portal, they receive a 'Request' number for tracking updates. However, fulfillers can only provide updates on Service Catalog Tasks (Sc_tasks).
Unfortunately, neither requesters nor fulfillers are receiving regular conversation updates.

 

I tried with below BR . It is not working as expected . 

Table : Sc_task

When to run: after 

Update is checked 

 

 

(function executeRule(current, previous /*null when async*/) {
    // Filter condition: Ensure comments have changed and the parent field is valid
    if (current.comments.changes() && current.parent) {
        // Retrieve the latest task comment
        var taskComment = current.comments.getJournalEntry(1);

        // Process the task comment to remove timestamp and user info
        if (taskComment) {
            var regex = new RegExp('\n');
            var i = taskComment.search(regex);
            if (i > 0) {
                var processedTaskComment = taskComment.substring(i + 1, taskComment.length).trim();
               
                // Query the associated request
                var requestGr = new GlideRecord('sc_request');
                requestGr.addQuery('sys_id', current.parent);
                requestGr.query();

                if (requestGr.next()) {
                    // Retrieve the latest request comment and process it
                    var requestComment = requestGr.comments.getJournalEntry(1);
                    var processedRequestComment = null;
                    if (requestComment) {
                        var i1 = requestComment.search(regex);
                        if (i1 > 0) {
                            processedRequestComment = requestComment.substring(i1 + 1, requestComment.length).trim();
                        }
                    }

                    // Synchronize comments if they don't match
                    if (processedTaskComment !== processedRequestComment) {
                        requestGr.comments = processedTaskComment;
                        requestGr.update();
                    }
                }
            }
        }
    }
})(current, previous);
 
 

 

 

 

 

 

 

1 ACCEPTED SOLUTION

J Siva
Tera Sage

Hi @JP6 
Please try the below approach. It'll work.

1. Create one event registry.

Name: Update commnts in RITM

Table: Task [task]

JSiva_0-1745659304360.png

 

2. Create one business rule on Task table as below.

JSiva_1-1745659432880.pngJSiva_2-1745659461033.png

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

    // Add your code here
    var task_cmnts = current.comments.getJournalEntry(1).split("(Additional comments)\n")[1];
    if (current.getTableName() == 'sc_req_item') {
        gs.eventQueue('update.cmts.task', current, task_cmnts, current.sys_id); // Trigger the event by passing the latest comments and task sys_id (RITM sys id)
    }
    if (current.getTableName() == 'sc_task') {
        gs.eventQueue('update.cmts.task', current, task_cmnts, current.request_item.sys_id); // Trigger the event by passing the latest comments and task sys_id (RITM sys id)
    }

})(current, previous);

 

3. Script action: (Async function to make updates as system)

JSiva_3-1745659719377.png

var task_cmnts = event.parm1; 
var task_sys_id = event.parm2;
//SET commnets from RITM to all the active SC Tasks
if (current.getTableName() == 'sc_req_item') {
    var sc_task = new GlideRecord('sc_task');
    sc_task.addQuery('request_item', task_sys_id);
    sc_task.query();
    while (sc_task.next()) {
        sc_task['comments'].setJournalEntry(task_cmnts, current.sys_updated_by);
        sc_task.update();
    }
}
//SET commnets from SC Task to RITM
if (current.getTableName() == 'sc_task') {
    var ritm = new GlideRecord('sc_req_item');
    ritm.get(task_sys_id);
    ritm['comments'].setJournalEntry(task_cmnts, current.sys_updated_by);
    ritm.update();
}

 

Regards,
Siva

 

 

 

View solution in original post

1 REPLY 1

J Siva
Tera Sage

Hi @JP6 
Please try the below approach. It'll work.

1. Create one event registry.

Name: Update commnts in RITM

Table: Task [task]

JSiva_0-1745659304360.png

 

2. Create one business rule on Task table as below.

JSiva_1-1745659432880.pngJSiva_2-1745659461033.png

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

    // Add your code here
    var task_cmnts = current.comments.getJournalEntry(1).split("(Additional comments)\n")[1];
    if (current.getTableName() == 'sc_req_item') {
        gs.eventQueue('update.cmts.task', current, task_cmnts, current.sys_id); // Trigger the event by passing the latest comments and task sys_id (RITM sys id)
    }
    if (current.getTableName() == 'sc_task') {
        gs.eventQueue('update.cmts.task', current, task_cmnts, current.request_item.sys_id); // Trigger the event by passing the latest comments and task sys_id (RITM sys id)
    }

})(current, previous);

 

3. Script action: (Async function to make updates as system)

JSiva_3-1745659719377.png

var task_cmnts = event.parm1; 
var task_sys_id = event.parm2;
//SET commnets from RITM to all the active SC Tasks
if (current.getTableName() == 'sc_req_item') {
    var sc_task = new GlideRecord('sc_task');
    sc_task.addQuery('request_item', task_sys_id);
    sc_task.query();
    while (sc_task.next()) {
        sc_task['comments'].setJournalEntry(task_cmnts, current.sys_updated_by);
        sc_task.update();
    }
}
//SET commnets from SC Task to RITM
if (current.getTableName() == 'sc_task') {
    var ritm = new GlideRecord('sc_req_item');
    ritm.get(task_sys_id);
    ritm['comments'].setJournalEntry(task_cmnts, current.sys_updated_by);
    ritm.update();
}

 

Regards,
Siva