Another question about getJournalEntry/changes

Michael Galat
Tera Contributor

Hi All -

 

I have looked through the forum, and I do not see anything that matches with what I am asking, so here goes.  We have an integration with another system. and in it we want to send work_notes and comments.  So, we have something like this:

 

if(current.work_notes.getJournalEntry(1) && current.work_notes.changes()){
    worknotes = current.work_notes.getJournalEntry(1);
}
if(current.comments.getJournalEntry(1) && current.comments.changes()){
    comments = current.comments.getJournalEntry(1);
}

Then we would package those up in a REST Transaction, and send them to another system.


This seems to work if running the BR as 'after', however, I would like to change this to run 'async' in order to return control back to the user a tad bit quicker.  However, when I change this - it seems both worknotes and comments are both empty - even if work_notes/comments were entered on the transaction.

One idea I have is that it is related to current.work_notes.changes()/current.comments.changes().  Could it be due to the fact that in the 'after' scenario, the transaction knows new data was entered in the fields, while when running 'async' it does not know?  If this is the case, is this even possible - running a transaction like this to capture only what was updated in a work_notes/comments field as an 'async' business rule?  If it is possible - how could it be done?

 

Thanks!

2 REPLIES 2

SanjivMeher
Kilo Patron
Kilo Patron

current.comments.changes() doesnt work in async BRs. It only works in an onBefore or onAfter business rule.

If you want to run it asynchronously, you should still use an onAfter business rule and then call an event. Event will not freeze the screen and will work asynchronously in the background.

And the event should be mapped to a script action. For ex

if(current.work_notes.getJournalEntry(1) && current.work_notes.changes()){
    worknotes = current.work_notes.getJournalEntry(1);
}
if(current.comments.getJournalEntry(1) && current.comments.changes()){
    comments = current.comments.getJournalEntry(1);
}

gs.eventQueue('event name',current,worknotes,comments);

Following is an article I found which can help you understand how it works.

https://www.basicoservicenowlearning.in/2022/01/script-action.html


Please mark this response as correct or helpful if it assisted you with your question.

Amit Gujarathi
Giga Sage
Giga Sage

HI @Michael Galat ,
I trust you are doing great.
To address this, you can modify your approach by explicitly checking for changes within the asynchronous business rule. One way to achieve this is by storing the previous state of the work notes and comments and comparing it with the current state when the asynchronous business rule executes. Here's how you can do it:

// Define a global object to store previous state
var previousState = {};

// Check for changes in work notes and comments
if (current.work_notes.changes() || current.comments.changes()) {
    // Compare with previous state to capture only updated entries
    if (current.work_notes.getJournalEntry(1) !== previousState.workNotes) {
        worknotes = current.work_notes.getJournalEntry(1);
    }
    if (current.comments.getJournalEntry(1) !== previousState.comments) {
        comments = current.comments.getJournalEntry(1);
    }
    
    // Update previous state
    previousState.workNotes = current.work_notes.getJournalEntry(1);
    previousState.comments = current.comments.getJournalEntry(1);
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi