close the task when ritm is closed

tushar_ghadage
Tera Contributor

Hi , i have been trying to close the parent ritm when all the childs are closed. 

here is the before update BR with condition state changed to closed complete

var count=0;
var tasks = new GlideRecord('sc_task');
tasks.addQuery('request_item',current.request_item);
tasks.query();
var taskcount = tasks.getRowCount();
while(tasks.next())
{
if(tasks.state == 3)
{
count++
}
}
if(count == taskcount)
{
var rq = new GlideRecord('sc_req_item');
rq.addQuery('sys_id',current.request_item);
rq.query();
if(rq.next())
{
rq.state = 3;
rq.update();
}

but its not working as expected as when state changed to WIP then its moving to close complete state in RITM 

do advise as what is wrong.

3 REPLIES 3

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @tushar_ghadage 

 

If I am not wrong it is OOTB, when task get closed the RITM get close via flow only.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Ankur Bawiskar
Tera Patron
Tera Patron

@tushar_ghadage 

you can use business rule or flow designer for this

BR approach: I shared the script few years ago; check this

How can we close RITM when all the catalog task is closed for that item? 

you can use after update BR on sc_task

Condition: State [IS ONE OF] Closed Complete/Closed Incomplete AND Request Item.Item == Your Catalog Item

Script:

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


        // Add your code here

        var gr = new GlideRecord('sc_task');

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

        gr.addQuery('active', true);

        gr.query();

        if(!gr.next()){

               var ritm = current.request_item.getRefRecord();

               ritm.state = 3;

               ritm.update();

        }


})(current, previous);

Flow Designer approach: I shared solution few years ago

I have shared solution below using flow designer to close parent INC when all child INCs are closed.

Enhance it for sc_req_item and sc_task table

If all child incident is closed then after Parent incident should be closed. How to achieve through....

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@tushar_ghadage 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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