We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

check if all child are closed

Sharique Azim
Mega Sage

How to find     all   the task(sc_task) under ritm table whose state is say pending .I want this to be reflected in the parent(ritm )[the state of ritm also changes to pending] only if ALL are pending ,else ritm stays in the same state like previously it was.

piece of my code:

var tsk=new GlideRecord("sc_task");

tsk.addQuery('request_item',current.request_item);

tsk.query();

while(tsk.next()){
if(tsk.state=="6"){

var itm= new GlideRecord("sc_req_item");

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

itm.addQuery('active',true);

itm.query();

if(itm.next()){

itm.state= '6';//also has the same value to avoid confusion.

itm.update();

gs.addInfoMessage(itm.number+itm.state.getDisplayValue());

}
}

}

1 ACCEPTED SOLUTION

Abhinay Erra
Giga Sage

here you go. This should be an after business rule on sc_task table.





var tsk=new GlideRecord("sc_task");


tsk.addQuery('request_item',current.getValue('request_item'));


tsk.addQuery('state','IN','6');


tsk.query();


if(tsk.hasNext()){


var itm= new GlideRecord("sc_req_item");


itm.get(current.getValue('request_item'));


itm.state= '6';//also has the same value to avoid confusion.


itm.update();


gs.addInfoMessage(itm.number+itm.state.getDisplayValue());


}


View solution in original post

15 REPLIES 15

Chuck Tomasi
Tera Patron

You can do this with a business rule on sc_task so when the state changes on any child record, it can check if it is time to update the parent record.



Standard disclaimer: The following code is untested, requires review and potential modifications.



(function () {  


      var count = -1;


      var rec = new GlideAggregate('sc_task');  


      rec.addAggregate('COUNT');


      rec.addQuery('state', '!=', 5); // Update this to be the value of YOUR pending state


      rec.addQuery('request_item', parent.sys_id);


      rec.query();  


 


      if   (rec.next()) {  


              count = rec.getAggregate('COUNT');


              if (count == 0) {


                      var parent = new GlideRecord('sc_req_item');


                      if (parent.get(parent.sys_id)) {


                            parent.state = -5; // Update this to be the value of YOUR pending state


                            parent.update();


                      }


                }


      }


})();  


Abhinay Erra
Giga Sage

here you go. This should be an after business rule on sc_task table.





var tsk=new GlideRecord("sc_task");


tsk.addQuery('request_item',current.getValue('request_item'));


tsk.addQuery('state','IN','6');


tsk.query();


if(tsk.hasNext()){


var itm= new GlideRecord("sc_req_item");


itm.get(current.getValue('request_item'));


itm.state= '6';//also has the same value to avoid confusion.


itm.update();


gs.addInfoMessage(itm.number+itm.state.getDisplayValue());


}


current.getValue('request_item') doesnt helps


It sure does. What is the reason you feel it is not working? Can you post the code you are using?