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

Joe McCarty1
ServiceNow Employee
ServiceNow Employee

If the workflow is on grc_observation, but the state change is on grc_remediation there is nothing triggering the evaluation of the workflow on the parent record.   You can use the model 'SNC - Run parent workflows' on the task table which calls new Workflow().broadcastEventToCurrentsContexts(gr, 'update', null) to force it the parent workflow to run and evaluate any pending wait activities.


Thank you Joe!   You are correct, the workflow is on the grc_observation table.   So, do I keep the 'Wait for Condition' or based on your recommendation, how do I specifically implement this?   Any pointers are greatly appreciated!


Joe McCarty1
ServiceNow Employee
ServiceNow Employee

I don't have GRC currently installed so I'm basing this on what was shared and don't immediately have a way to verify:



Create an after business rule on the grc_remediation table named 'Run Observation Workflow'


Probably should add a current.u_state.changes() condition because we only need to check when states change.



The script should be something like:



// Run any workflow(s) so this task's state changes can be checked


runWorkflow_task();




function runWorkflow_task() {


    if (!current.source.nil()) {


          var gr = new GlideRecord('grc_observation');


          gr.addQuery("sys_id",current.source.toString());


          gr.query();


          if (gr.next()) {


          new Workflow().broadcastEventToCurrentsContexts(gr, 'update', null);


          }


    }


}



That should force the workflow on the grc_observation table to evaluate when you update the child grc_remediation records.


Thank you Joe.   I have this business rule setup now on the grc_remediation table.



On the grc_observation table side, what then would I do (in the workflow) to evaluate the results of this business rule push of data, if I am looking to see that all associated grc_remediation items are of a u_state of 2 before proceeding forward?