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

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


ajitchandragiri
Giga Expert

Rhett,



in your case, hasNext() returns true if atleast one record is in state 2. it does not consider all the records which are not in state 2. your script should be like.




  1. var rec = new GlideRecord('grc_remediation');
  2. rec.addQuery('source', current.sys_id);
  3. rec.addQuery('u_state', '!=', '2');
  4.  
  5. rec.query();  
  6. if(rec.hasNext()){//if all grc_remediation items match the query  
  7.       //Continue on since all MAPs are approved.  
  8.       answer = false;  
  9.       //current.approval = 'approved';  
  10.          
  11.       //For troubleshooting purposes  
  12.       var latestState = rec.u_state.getGlideObject();  
  13.       gs.addInfoMessage("This is the true state: " + latestState);  
  14. }  
  15.  
  16. else{  
  17.       //Continue waiting since all MAPs are not approved.  
  18.       answer = true;  
  19.          
  20.       //For troubleshooting purposes  
  21.       var latestState = rec.u_state.getGlideObject();  
  22.       gs.addInfoMessage("This is the false state: " + latestState);  
  23. }

I had a similar requirement with Change Request and change tasks. i had to wait for all change tasks to be closed completed to close the change. i used the wait activity on the workflow of change request to check if all change task of that change request are closed. I think the wait will work if the parent table of the tables referred in workflows is same. ex- Task table is parent table of Change request and change task.


Thank you Ajit and you read my mind.   I have been troubleshooting and for some reason your recommendation always takes me to the evaluation of true or the else statement.   Still trying different combinations and looking at if my sys_id is correct between grc_observation to grc_remediation tables.


yeah, you were looking for records with state 2. i thought it would be better to look at records which are not in state 2. i dont have GRC to confirm the sys_id thing but if my code does not work you should try what Joe recommends. that should definitely work. hope you get this to work.