gr.next() not working as expected on Wait for Condition workflow activity
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2024 12:56 PM
I have a workflow for a catalog item that is supposed to:
1. Create approval
2. If rejected, wait for opened_by to add a comment to the RITM
3. Rollback to the approval process
4. Repeat until approved
Here is this script on the Wait for Condition activity:
gs.log('checking for condition', 'testlog');
var appr = new GlideRecord('sysapproval_group');
appr.get('parent', current.sys_id.getValue()); // Get group approval on current RITM
var apprDate = new GlideDateTime();
apprDate = appr.sys_created_on.getValue(); // Set apprDate to the approval's created date
var gr = new GlideRecord('sys_journal_field');
gr.addQuery('sys_created_by', current.opened_by.user_name.getValue());
gr.addQuery('element_id', current.sys_id.getValue());
gr.query(); // Find comment records on the current RITM created by whoever opened the RITM
while (gr.next()) {
gs.log(gr.sys_created_by + " " + gr.element_id + " " + gr.value, 'testlog');
if (gr.sys_created_on > apprDate) { // If comment was added after the approval, condition is true
answer = true;
}
}
This script works completely fine if I run it in Background Scripts. It will find the single comment added by opened_by and set answer to true.
However, when its running in the workflow activity, the while loop only triggers once a second comment is added by the user (see screenshot below). It's almost like the while loop is not running because gr.next() is equating to false and only triggers when there's a second comment after the first.
Based on this thread discussing how .next() works and the fact that the script works as intended in Background Scripts, it seems like it should definitely be catching that first comment.
What am I missing?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2024 03:27 PM
Hey @Oliver Anderson,
By looking at the logs, it appears that at the time when the 'Wait for condition' script is executed, the 'sys_journal_field' record hasn't been created yet (it may be due to the order of script/engine execution). Hence, you are not seeing any log with the first comment and only one from the second comment.
There could be a better way to do things but you may have to write a BR that triggered when a comment is added by the requestor. The BR can either fire an Event which can be used by a 'Wait for WF Event' workflow activity or have the BR update another field in your RITM which the 'Wait for Condition' listens to.
Cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2024 04:43 PM
That's a good idea, I will try that in the morning and Accept as Solution if it works out! Thanks