function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Surender GiriSurender Giri 

A parent case should be closed only if all its child cases are closed. Parent case shouldn’t be allowed to be closed by the user. Once the last Child case is closed, the Parent Case should automatically be closed.

Hi All,
A parent case should be closed only if all its child cases are closed. Parent case shouldn’t be allowed to be closed by the user. Once the last Child case is closed, the Parent Case should automatically be closed.

I written below trigger but is is working if any child case is closed then parent case is automatically closing instead of to close all the child case.

trigger CloseParentCaseTrigger on Case (after update) {
    list<id>idList=new list<id>();
    for(case c:Trigger.New){
        idList.add(c.parentId);
    }
    list<case>csList=new list<case>();
    List<case>caseList=[select id,status,(select id,status from cases) from case where id=:idList];
    system.debug('$$$$caseList: '+ caseList);
    try{
        for(case c:caseList){
            for(case c1:c.cases){
                if(c1.status=='closed'){
                    c.status='closed';
                    csList.add(c);
                    
                }
            }
        }
    }
    catch(exception e){
    }
    update csList;
}
ANUTEJANUTEJ (Salesforce Developers) 
Hi Surender,

I think you can check before update and then in case if there are any child cases which are not in close status you can add an error to that case record, can you try checking if the below code once:
 
trigger parentcase on case(before update)
{
	if(trigger.isbefore && trigger.isupdate)
	{
	set<id> sid=new set<id>();
	for(case c: trigger.new)
	{
	sid.add(c.id);
	}
	
	//pcaseid is the lookup field to the parent case

	list<case> clist= [select id, pcaseid,status from case where id in :sid and status!='close']
	map<id,number> macase= new map<id,number>();
	
	for(case c: clist)
	{
	if(macase.containskey(c.pcaseid))
	{
	number i= macase.get(c.pcaseid);
	i=i+1;
	macase.put(c.pcaseid,i);
	}
	else
	{macase.put(c.pcaseid,1);}
	
	}

	for(case c: trigger.new)
	{
	if(macase.get(c.id)!=0)
	{
	c.adderror('you cannot delete until the child cases are closed!!');
	}
	}
	
	}
}

I hope this helps and in case if this comes in handy can you please choose this as the best answer so that it can be used by others in the future.

Regards,
Anutej 
Surender GiriSurender Giri
Thanks ANUTEJ ,

But i don't want to delete or display any error message because our requirement is if all child have case status then parent should also update with close status.
ANUTEJANUTEJ (Salesforce Developers) 
I tried making few changes to the code you have used can you try checking it once:
 
trigger CloseParentCaseTrigger on Case (after update) {
    list<id>idList=new list<id>();
    for(case c:Trigger.New){
        idList.add(c.parentId);
    }
    Boolean testbool;
    list<case>csList=new list<case>();
    List<case>caseList=[select id,status,(select id,status from cases) from case where id=:idList];
    system.debug('$$$$caseList: '+ caseList);
    try{
        for(case c:caseList){
        testbool=true;
            for(case c1:c.cases){
                if(c1.status!='closed'){
                    testbool=false;
                  }
            }
            if(testbool==true)
            {
            c.status='closed';
            csList.add(c);
            }
        }
    }
    catch(exception e){
    }
    update csList;
}

P.S: Just added the code to block for better clarity