- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2017 03:58 PM
I have requirement to look for and ensure ALL grc_remediation items have been approved using a custom field (u_state). The value of the u_state needs to be '2' in order for the grc_observation workflow to move to the next part of the workflow.
Here is the Wait for Condition in the workflow that does not appear to be working:
<NO CONDITIONS>
<script>
//Query for all grc_remediation items to check if all are approved.
var rec = new GlideRecord('grc_remediation');
rec.addQuery('source', current.sys_id);
rec.addQuery('u_state', '2');
rec.query();
if(rec.hasNext()){//if all grc_remediation items match the query
//Continue on since all MAPs are approved.
answer = true;
//current.approval = 'approved';
//For troubleshooting purposes
var latestState = rec.u_state.getGlideObject();
gs.addInfoMessage("This is the true state: " + latestState);
}
else{
//Continue waiting since all MAPs are not approved.
answer = false;
//For troubleshooting purposes
var latestState = rec.u_state.getGlideObject();
gs.addInfoMessage("This is the false state: " + latestState);
}
What am I doing wrong?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2017 08:00 AM
Finally figured it out and tested it many, many times. Here is what I was missing: What to do when there are no records found, at first. So, I had to create another Wait for Condition before the one you solved for me:
FIRST Wait for Condition Name: Create at Least One Record
//First, navigate to the MAP table
var gr_tsk = new GlideRecord("grc_remediation");
//Lookup only those MAPs associated with this issue
gr_tsk.addQuery('source', current.sys_id);
//Query the MAP table
gr_tsk.query();
//Iterate through the records...
if (gr_tsk.next()) {
//If there is at least one record found, then proceed forward to the next Wait for activity...
answer = true;
} else {
//If there are no records found then exit the wait for activity...
answer = false;
}
Then, here is the next Wait for Condition, based on what you (Ajit) and Joe spoke into. I realized there was one other state I need to search for, which was #4, our Closed state.
SECOND Wait for Condition Name: Remaining Record Submissions and Approvals
//First, navigate to the MAP table
var gr_tsk = new GlideRecord("grc_remediation");
//Lookup only those MAPs associated with this issue
gr_tsk.addQuery('source', current.sys_id);
//Next, look for all states that are not 2 or 4
gr_tsk.addQuery('u_state', '!=', 2);
gr_tsk.addQuery('u_state', '!=', 4);
//Query the MAP table
gr_tsk.query();
//Iterate through the records...
if (gr_tsk.next()) {
//If there is at least one record found with a state that is not 2 or 4, continue waiting...
answer = false;
} else {
//If there's no records found then exit the wait for activity...
answer = true;
}
Then, I created Joe McCarty's provided Business Rule in grc_remediation table that is performed AFTER with Update checked:
CONDITION: current.wf_activity.isNil() && current.active.changesTo(false)
Script:
function onAfter(current, previous) {
forceReqItemUpdate();
function forceReqItemUpdate() {
var wf = new Workflow();
var ri = new GlideRecord("grc_remediation");
if (ri.get(current.number)) {
wf.runFlows(ri, 'update');
}
}
}
All is now right in the world (or at least mine).

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2017 02:05 PM
Assuming your condition is good in the workflow, there shouldn't be anything else you need to do other than to modify a grc_remediation record or grc_observation record that is running the workflow and currently in the wait state to see if it works. The business rule just ensures the condition is evaluated. And whether it succeeds or fails you should see an info message based on your code. If you don't see an info message, I would look for code exceptions in the log.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2017 03:50 PM
Thank you Joe and Ajit. I figured it out with a combination of both your recommendations. Here is what I did to benefit the community:
Joe, I created the business rule on the grc_remediation table and used your javascript code verbatim. The only thing I had to do was ensure that I selected Insert and Update on the When to Run tab.
Ajit, in the workflow this was the final script I used to get it to work in the Wait for Condition:
If there is any easier way or this is overkill, I am open to your suggestions:
<NO CONDITION>
<CONDITION SCRIPT FOLLOWS>
//Query for all MAPs to check if all are approved.
var rec = new GlideRecord('grc_remediation');
rec.addQuery('source', current.sys_id);rec.query();
while(rec.next()) {
if(rec.u_state == 1 || rec.u_state == 3 || rec.u_state == 4 || rec.u_state == 'null') //these are all the states that are not approved
answer = false;
}if(rec.u_state == 2) {
answer = true;
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2017 03:54 PM
Nothing from me. If any of the answers were 'Correct' or 'Helpful' feel free to mark them as such.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2017 04:03 PM
Rhett,
I don't think this will work. since you have used while, it iterates through all the records and sets answer to the state value of the last record. better use
rec.addQuery('source', current.sys_id);
rec.addQuery('u_state', '!=', 2);
rec.query();
joemccarty, correct me if am wrong.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2017 04:12 PM
Yes, sorry, I was focused more on the evaluation of the wait activity versus the condition itself. The code there the last record will win which will be unpredictable. Better to search for any unapproved task and returning false until hasNext is false. The following will continue to wait until all associated have a state of '2'
//Query for all MAPs to check if all are approved.
var rec = new GlideRecord('grc_remediation');
rec.addQuery('source', current.sys_id);
rec.addQuery('u_state', '!=', 2);
rec.query();
if (rec.hasNext()) {
answer = false;
} else {
answer = true;
}