Skip to content
Sync Comments Between SCTASK and RITM

Sync Comments Between SCTASK and RITM

Overview

This document describes the solution for bi-directional syncing of comments between ServiceNow sc_task (SCTASK) records and their parent sc_req_item (RITM) records.

Two Business Rules are used to keep comments in sync:

  1. Copy Comments from SCTASK to RITM
    Triggered when a comment is added on an SCTASK. The comment is copied to the parent RITM if it is not already present.

  2. Copy Comments from RITM to SCTASK
    Triggered when a comment is added on an RITM. The comment is copied to all active SCTASKs linked to that RITM if it is not already present.

Problem Statement

By default, comments in ServiceNow do not automatically propagate between related SCTASK and RITM records. This can lead to:

  • Incomplete communication between requesters and fulfillers.
  • The need for manual duplication of comments.
  • Potential delays or misunderstandings in request fulfillment.

The goal of this solution is to automatically keep comments synchronized in both directions, reducing manual effort and ensuring consistent communication.

Copy Comments from SCTASK to RITM.js
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
(function executeRule(current, previous /*null when async*/) {
  // Get the latest journal entry (comment) from the SCTASK
  taskCommentRaw = current.comments.getJournalEntry(1);

  // Remove timestamp and author from the SCTASK comment
  var reg_exp = new RegExp('\n');
  var i = taskCommentRaw.search(reg_exp);
  var taskCommentCleaned = '';
  if (i > 0) {
      taskCommentCleaned = taskCommentRaw.substring(i + 1, taskCommentRaw.length).trim();
  }

  // Query for the parent RITM record using the SCTASK's parent field
  var ritmGR = new GlideAggregate('sc_req_item');
  ritmGR.addQuery('sys_id', current.parent);
  ritmGR.query();

  if (ritmGR.next()) {
      // Get the latest journal entry (comment) from the RITM
      ritmCommentRaw = ritmGR.comments.getJournalEntry(1);
      var ritmCommentCleaned = '';

      // Remove timestamp and author from the RITM comment
      var j = ritmCommentRaw.search(reg_exp);
      if (j > 0) {
          ritmCommentCleaned = ritmCommentRaw.substring(j + 1, ritmCommentRaw.length).trim();
      }

      // If the cleaned SCTASK comment is not already present in the RITM comment, copy it over
      if (taskCommentCleaned && taskCommentCleaned !== ritmCommentCleaned) {
          ritmGR.comments = taskCommentCleaned;
          ritmGR.update();
      }
  }

})(current, previous);
Last updated on