Check that all tasks are closed and date passed

Dan Brown2
Kilo Sage

Some of my incidents have child tasks associated to them.   I had it set up, so that when all the child tasks were closed, the parent incident closed too using a business rule. All working fine.

I have had to change this, so that parent incident can close when:

1.) all their child tasks are closed and

2.) the incident follow_up date has passed

My thought is to use a scheduled job (scheduled script execution) that runs nightly and for any incidents that meet these requirements are closed.   I am having trouble setting this up.   Can anyone please advise if this is the best method and also any resources to assist?

Many thanks,

Dan

1 ACCEPTED SOLUTION

I might do something a little more like this. I have a habit of using encoded queries when I work with dates, mainly so I can test it first in the condition builder and don't have to worry about what is supported where.



var inc = new GlideRecord('incident');


inc.addEncodedQuery('follow_up<javascript:gs.minutesAgoStart(0)');


inc.addActiveQuery();


inc.query();



while (inc.next()) {


      var rec = new GlideRecord('u_tasks');


      rec.addQuery('parent', inc.getValue('sys_id'));


      rec.addQuery('state', 'IN', '3,4');


      rec.query();


      if (!rec.hasNext()) {


              gs.log("incident resolved " + inc.number);


              //close incident


      }


}


View solution in original post

5 REPLIES 5

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Dan,



Since you're checking a date I would go ahead and do this through a scheduled job. I would query the incident table for incidents where the date is in the past, then iterate through each incident and query the incident task table for any open tasks related to that incident.


Thanks Brad,



Can you confirm I am on the right lines doing something similar below?



var inc = new GlideRecord('incident');


inc.addQuery('follow_up', '<', gs.minutesAgoStart(0));


inc.query();



while(inc.next()) {



    var rec = new GlideRecord('u_tasks');  


    rec.addQuery('parent', inc);


    rec.addQuery('state', 'IN', '3, 4');    


    rec.query();  


    if(rec.next()){  


          //If we find any active change tasks do nothing  


    }  


    else{  


      gs.log("incident resolved " + inc.number);  


      //close incident


    }


I might do something a little more like this. I have a habit of using encoded queries when I work with dates, mainly so I can test it first in the condition builder and don't have to worry about what is supported where.



var inc = new GlideRecord('incident');


inc.addEncodedQuery('follow_up<javascript:gs.minutesAgoStart(0)');


inc.addActiveQuery();


inc.query();



while (inc.next()) {


      var rec = new GlideRecord('u_tasks');


      rec.addQuery('parent', inc.getValue('sys_id'));


      rec.addQuery('state', 'IN', '3,4');


      rec.query();


      if (!rec.hasNext()) {


              gs.log("incident resolved " + inc.number);


              //close incident


      }


}


Thank you, that has worked great.