I written following code in "wait for condition" in work flow to stop workflow until to close all task records for particular request item, But this not working .

srikanth241
Mega Expert

Hi Team,

I written following code in "wait for condition" in work flow to pause the workflow until to close all task records for particular request item, But this not working, even all the tasks are closed but still it is in wait for condition only. It is not moving to further activities.

All these tasks are created dynamically trough run script from inputs of list collector.

Any Idea or inputs can help me a lot

code:

var gRT = new GlideRecord('sc_task');

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

gRT.addQuery('active',true);

gRT.query();

  if(gRT.hasNext()){

  answer = false;

  gs.log('if'+gRT.hasNext());

  }

  else{

  answer = true;

  gs.log('else'+gRT.hasNext());

  }

Thanks.

Developer CommunityDiscussExperts CornerCommunity CornerConnect

1 ACCEPTED SOLUTION

That's a good point. Workflows only re-check when the record used against the workflow (e.g. RITM, change request) is updated. Updating child records isn't going to trigger the workflow on the parent record. To do this, you could force an update on the request_item when an update to the sc_tasl table is made.



An AFTER BR on the sc_task table something like this



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



var parent = new GlideRecord('sc_req_item');


parent.get(current.request);


parent.setForceUpdate(true);


parent.update();



})(current, previous);


View solution in original post

24 REPLIES 24

Chuck Tomasi
Tera Patron

Hi Abhay,



Have you tried using a GlideAggregate to count instead of checking if there is a next record? For performance reasons, this is a better idea.



(Untested) example:



var gRT = new GlideAggregate('sc_task');


gRT.addAggregate('COUNT');


var count = -1;


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


gRT.addQuery('active',true);


gRT.query();



if (gRT.next()) {


        count = gRT.getAggregate('COUNT');


}



gs.log('found ' + count + ' records');



answer = (count == 0);


Also check to make sure you don't have another condition that is preventing the script from returning 'true'. Per the annotation, all parts MUST evaluate to true. It doesn't do any good if the script is true and the condition field is false. This part should basically be empty:



find_real_file.png


Hi Chuck,


                            Thanks for your quick reply, I tried using your code, It is not working.


Trough log i found that count remains 2(I opened two tasks) even after I closed first task and checked, it is not updating. it is not generating any new log of new count value. the value remains 2 only. I hope some thing is not triggering the" wait for condition " to check when i update the tasks. Is there any business rule that will trigger the workflow or resume the workflow to check the "wait for condition" again.


any Inputs will help me a lot



Thanks.


Hi Abhay,



Did you try my code and print log and check how much count you receive and let me know if any concerns.



Regards


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Have you tried the GlideAggregate example I posted earlier?



Best practice recommends using GlideAggregate if you are simply counting records.



Another option would be to limit the amount of records queried using



gRT.setLimit(1);



That will make the gRT.query() run quicker since you really only want to know if there are 0 or non-zero results.