getJournalEntry(1) gives different results in before and after Business Rule

Nia McCash
Mega Sage
Mega Sage

I have a before Business Rule and an after Business Rule on my incident table.   Both Business Rules need to copy the most recent comments to different tables as part of their logic and use the getJournalEntry(1) function call.   I notice the before Business Rule works, but the after Business Rule does not.   The after Business Rule copies the last comment previous to the most recent (and results in a blank comment being copied in the case of the first comment entered).

Additionally, I've noticed that if I disable the before Business Rule, then the after Business Rule runs as expected and copies the correct comment instead of the one previous.

Question: Is there an undocumented limitation to the way one can call getJournalEntry(1) in a before and an after Business Rule which results in the scenario described above? Or is this a bug (ahem... enhancement yet to be completed)?

Hoping one of the ServiceNow expert insiders ctomasi, pradeepksharma, joshnerius, dave.slusher can provide a quick confirmation.

In case it matters, I'm on Jakarta version.

1 ACCEPTED SOLUTION

Hmmm, yeah i get the same result which is unexpected.



However there is a simple solution just change:



gs.warn("DEBUG before BR: " + current.comments.getJournalEntry(1));



to:



gs.warn("DEBUG before BR: " + current.comments);



Since you are in a before BR there is no need to call getJournalEntry as the field has the last value in it.


View solution in original post

6 REPLIES 6

andrew_venables
ServiceNow Employee
ServiceNow Employee

In the use case are you writing to the journal field (i.e. adding data to it)? If so then i think this is expected behaviour since a before business rule runs before the commit to the database and the after business rule runs after the commit to the database.



So calling getJournalEntry with a static index would produce different results based on the data being in the database yet.


So to clarify with an example, it could be that the incident record is being updated with a comment.   The before Business Rule is copying that comment into an Incident Task record.   The after Business Rule is the out-of-box Task Time Worked which creates a task_time_worked entry and copies the comment from the Incident record to the newly created task_time_woked entry.



If I deactivate the before Business Rule, the after Business Rule works as expected and copies the correct comment to the task_time_worked entry.   So, I'm not sure I understand what you mean by "calling getJournalEntry with a static index would produce different results based on the data being in the database yet."   Can you give me an example?


Can you share your before business rule?


I have found that even something as simple as the following before Business Rule will cause the out-of-box Task Time Worked Business Rule to stop working as expected.   Please feel free to test in an out of box instance of ServiceNow.



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


        gs.warn("DEBUG before BR: " + current.comments.getJournalEntry(1));


})(current, previous);