Intermittent issue in Service Portal widget – incorrect “Latest Reply By” user displayed post postin

AviKadam
Tera Contributor

We are facing an intermittent issue in a custom Service Portal Community widget where the UI occasionally displays the wrong user in the “Latest Reply By” section.

 

Scenario

We have a question/article page that shows:

“Latest reply by”

After posting a new reply/comment:

  • sometimes the latest reply to user updates correctly,
  • but on certain records/pages, the UI still shows the previous commenter instead of the newly posted user.

The issue is inconsistent and difficult to reproduce reliably.

 

Current Architecture / Approach

The widget:

  • loads initial question + latest reply data from server,
  • fetches nested replies asynchronously using REST calls,
  • uses spUtil.recordWatch() for live updates,
  • and updates the latest reply object on new comments.

Simplified version of the current logic:

// Initial load
c.latestReply = c.post.latestReply;

Then later during reply to updates:

function placeNewComment(newComment) {
    c.latestReply = newComment;
}

We also use a record watcher similar to:

spUtil.recordWatch(
    $scope,
    "kb_social_qa_answer",
    "question=" + question,
    function(response) {

        if (response.data.action === "entry") {
            getNewReplyDetails(response.data.sys_id);
        }
    }
);

And fetch reply to details asynchronously:

$http.get(apiBaseUrl + resourceUrl)
    .then(function(response) {

        var newComment = response.data.result.data;

        placeNewComment(newComment);
    });

What We Observed

Example:

  • Article A → latest reply to user updates correctly.
  • Article B → after posting a new reply, UI still shows old user as latest reply author.

Same widget/code path, different behavior.

We added temporary logs and observed:

  • initial page load sometimes contains correct latestReply,
  • but occasionally stale/older reply data seems to persist,
  • especially during async/real-time updates.

Questions

Has anyone encountered:

  • stale latestReply values in Service Portal widgets?
  • incorrect realtime updates using spUtil.recordWatch()?
  • race conditions while updating scope objects asynchronously?

Also:

  • is there a recommended pattern for safely maintaining “latest reply” state in AngularJS Service Portal widgets?
  • would deriving the latest reply from the actual comments collection be safer than relying on a cached latestReply object?

Any guidance or similar experiences would be very helpful.

Thanks in advance.

1 REPLY 1

Naveen20
ServiceNow Employee
Most certainly a race condition, not a recordWatch bug. Check below -

 

1. Make the comments collection the single source of truth. Don't maintain c.latestReply as separate state. Derive it:
function getLatestReply(comments) {
if (!comments || !comments.length) return null;
return comments.slice().sort(function(a, b) {
return new Date(b.sys_created_on) - new Date(a.sys_created_on);
})[0];
}
// then on every entry event after fetch:
c.comments.push(newComment);
c.latestReply = getLatestReply(c.comments);

 

When two replies post in quick succession, your $http.get calls can resolve out of order, so the older one wins and overwrites the newer latestReply. Deriving from a sorted array eliminates that.

 

2. Verify the recordWatch filter. Confirm the question sys_id is populated before registering — if it's empty at controller init, the filter resolves to question= and matches inserts on every kb_social_qa_answer, mixing in answers from other questions.

 

Also the documented callback signature is function(name, data) with data.action, not function(response) with response.data.action — double check which shape you're actually receiving.