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
VRKVRK 

when child cases Status update then parent case Status also updated in Trigger

Hi Team
can anyone provide code for below scenario by using Triggers:
When all child cases  closed/ cancelled / Completed, then parent case also be Closed /Cancelled / Completed...
Can you pls provide code for this ...........Thanks
Kumaresan.ManickamKumaresan.Manickam
I could suggest the way for you to proceed further:
1) Create a after update trigger on case object which can make a future call to another method by passing a parent case id's of updated cases.
2) From future method make SOQL to pull all child cases of parent case ids and determine the status of child cases and come to the conclusion to update a parent case with right status.
3) I would recommend to use asynch processing in your case as making updates to case table again in the same transaction lead to recurssive problems. You could also perform the same via trigger as well.

Choose this reply as best answer if its helpful

Cheers!!
Ajay K DubediAjay K Dubedi
Hi,

You can use the below code.

<<<<---Apex Class----->>>
 
public class UpdateCase {
    public static void UpdateCaseMethod(List<Case> caseList){
        
        SYstem.debug('caseList  :::'+caseList);
        Map<Id,List<Case>> IdVSCaseListMap = new Map<Id,List<Case>>();
        Map<Id,Case> IdVSParentcaseMap = new Map<Id,Case>();
        List<case> parentCaseList = new List<case>();
        Set<Id> parentcaseIds = new Set<Id>();
        for(Case caseObject : caseList){
            if(!IdVSCaseListMap.containsKey(caseObject.ParentId)){
                List<Case> caseListObject = new List<Case>();
                caseListObject.add(caseObject);
                IdVSCaseListMap.put(caseObject.ParentId,caseListObject);
            }else{
                List<Case> caseListObject =  IdVSCaseListMap.get(caseObject.ParentId);
                caseListObject.add(caseObject);
                IdVSCaseListMap.put(caseObject.ParentId,caseListObject);
            }
            parentcaseIds.add(caseObject.ParentId);  
        }
        System.debug('parentcaseIds  ::'+parentcaseIds);
        System.debug('parent Vs Child List Map  :::'+IdVSCaseListMap);
        parentCaseList = [SELECT Id,Status FROM Case WHERE Id IN:parentcaseIds];
        System.debug('parentCaseList  ::'+parentCaseList.size());
            for(Case caseObj : parentCaseList) {
                IdVSParentcaseMap.put(caseObj.Id,caseObj);
            }  
        
        System.debug('IdVSParentcaseMap  :::'+IdVSParentcaseMap);
        if(!IdVSCaseListMap.isEmpty()){
            for(Id parentcaseId : IdVSCaseListMap.KeySet()){
                String caseStatus = '';
                Integer i=0;
                boolean sameValue = true;
                for(Case caseObject : IdVSCaseListMap.get(parentcaseId)){
                    if(i==0){
                    caseStatus = caseObject.Status;
                        i++;
                    }else{
                        if(!caseStatus.equals(caseObject.Status)){
                          sameValue = false;  
                        }
                    }
                }
                if(sameValue){
                    Case caseUpdate = IdVSParentcaseMap.get(parentcaseId);
                    SYstem.debug('caseUpdate  ::'+caseUpdate);
                    caseUpdate.Status = caseStatus;
                    IdVSParentcaseMap.put(parentcaseId,caseUpdate);
                }
            }
        }
       if(!IdVSParentcaseMap.isEmpty()){
            update IdVSParentcaseMap.values();
        }
        System.debug('IdVSCaseListMap  :::'+IdVSCaseListMap);
    }
}
<<<<----Apex trigger--->>>
 
trigger CaseTrigger on Case (after update) {
    System.debug('Hii Update Trigger');
    if(AvoidRecursion.isFirstTime)
    {
        AvoidRecursion.isFirstTime = false;
        UpdateCase.UpdateCaseMethod(trigger.new);
    }
}


<<<<----Apex Class--->>>

public class AvoidRecursion
{
     public static Boolean isFirstTime = true;
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
VRKVRK
Thank you Ajay for your response...
I tested your code , but it works if any  child case status updated then updating same parent case status.
But my requirment is  all child cases status updated, then only parent case status update..
for example :
i have parent case number # 100
and this parent case 3 child cases ( 101, 102,103).
Now when i update all child cases status is closed then parent case status also be closed,,
once again thanks for your support on this