Workflow Wait For Condition. Wait Untill A Task State Changes

MichaelRauch
Giga Guru

I am trying to get a wait for condition to wait until any tasks state changes to 2 (work in progress)

var tasks = new GlideRecord('sc_task');

tasks.addQuery('request_item', current.sys_id);

tasks.query();

while (tasks.next()){

      if(tasks.state == 2){

              answer = true;

      }

      else{

              answer = false;

      }

}

/* Other Tested Code (there is only one task so thought this would also work.)

if(tasks.next()){

      if(tasks.state == 2){

              answer = true;

      }

      else{

              answer = false;

      }

}

else {

    answer = false;

}

*/

1 ACCEPTED SOLUTION

MichaelRauch
Giga Guru

I have figured it out! let me know if this helped anyone.



the issue is when the task is updated the wait for condition doesn't cycle because it only does when the item updates. We added a business rule to force an update for the tasks item below.



Table: sc_task


When: after update


Condition: State changes



(function executeRule(current, previous /*null when async*/) {



forceReqItemUpdate();



function forceReqItemUpdate() {


    var wf = new Workflow();


    var ri = new GlideRecord("sc_req_item");


    if (ri.get(current.request_item)) {


          wf.runFlows(ri, 'update');


    }


}



})(current, previous);



_________________



Then the script inside the wait for condition is



var tasks = new GlideRecord('sc_task');


tasks.addQuery('request_item', current.sys_id);


tasks.query();


//This will work for the first task in the list of tasks. In my particular workflow this is what i wanted


if(tasks.next()){


      if(tasks.state == 2){


              answer = true;


      }


      else{


              answer = false;


      }


}


else {


    answer = false;


}



/* This should work for if you want to find any task where the state has changed to work in progress


while (tasks.next()){


      if(tasks.state == 2){


              answer = true;


      }


}


*/


View solution in original post

2 REPLIES 2

MichaelRauch
Giga Guru

I have figured it out! let me know if this helped anyone.



the issue is when the task is updated the wait for condition doesn't cycle because it only does when the item updates. We added a business rule to force an update for the tasks item below.



Table: sc_task


When: after update


Condition: State changes



(function executeRule(current, previous /*null when async*/) {



forceReqItemUpdate();



function forceReqItemUpdate() {


    var wf = new Workflow();


    var ri = new GlideRecord("sc_req_item");


    if (ri.get(current.request_item)) {


          wf.runFlows(ri, 'update');


    }


}



})(current, previous);



_________________



Then the script inside the wait for condition is



var tasks = new GlideRecord('sc_task');


tasks.addQuery('request_item', current.sys_id);


tasks.query();


//This will work for the first task in the list of tasks. In my particular workflow this is what i wanted


if(tasks.next()){


      if(tasks.state == 2){


              answer = true;


      }


      else{


              answer = false;


      }


}


else {


    answer = false;


}



/* This should work for if you want to find any task where the state has changed to work in progress


while (tasks.next()){


      if(tasks.state == 2){


              answer = true;


      }


}


*/


Daniel Oderbolz
Kilo Sage

I am not entiery sure, but I think the solution given here:

 

https://community.servicenow.com/community?id=community_question&sys_id=02810369db98dbc01dcaf3231f961903

 

Is better:

 

 var gr = new GlideRecord('sc_req_item');		
if (gr.get(current.request_item)) {
     new Workflow().broadcastEventToCurrentsContexts(gr, 'update', null);
}

Cheers

Daniel


If this answer was helpful, I would appreciate if you marked it as such - thanks!

Best
Daniel