Help with 'Wait for Condition' in Workflow

rhett1
Tera Expert

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?

1 ACCEPTED SOLUTION

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).  


View solution in original post

19 REPLIES 19

I tried this and the workflow skips right on by the Wait for Condition, each time.   The while loop, while not ideal, is working.


Rhett,



did u try with multiple grc_remediation items? it may fail for multiple items. are u trying this on your personal instance? can u share the url?


I apologize, you are correct, part of my requirement that I did not clearly state at the start is that there can be multiple items in the grc_remediation table that are tied to one item in the grc_observation table.   This is what ended up working for me and if there is a better way, please let me know:



//Query for all MAPs to check if all are approved.
var gr = new GlideRecord('grc_remediation');
gr.addQuery('source', current.sys_id);

gr.query();


//For troubleshooting purposes    
//gs.addInfoMessage("Query Response: " + gr.u_state);

while (gr.next()) {
if (gr.u_state != '2') {
  answer = false;
}   else {
  answer = true;
}
}


Rhett,



again, i think the above code may fail for multiple items. if you are doing this on your personal instance share the url, i will check.


I am not doing this in a personal instance, but I will set one up and let you know the URL.