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

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
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

Ajay K DubediAjay K Dubedi

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){
                List<Case> caseListObject = new List<Case>();
                List<Case> caseListObject =  IdVSCaseListMap.get(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) {
        System.debug('IdVSParentcaseMap  :::'+IdVSParentcaseMap);
            for(Id parentcaseId : IdVSCaseListMap.KeySet()){
                String caseStatus = '';
                Integer i=0;
                boolean sameValue = true;
                for(Case caseObject : IdVSCaseListMap.get(parentcaseId)){
                    caseStatus = caseObject.Status;
                          sameValue = false;  
                    Case caseUpdate = IdVSParentcaseMap.get(parentcaseId);
                    SYstem.debug('caseUpdate  ::'+caseUpdate);
                    caseUpdate.Status = caseStatus;
            update IdVSParentcaseMap.values();
        System.debug('IdVSCaseListMap  :::'+IdVSCaseListMap);
<<<<----Apex trigger--->>>
trigger CaseTrigger on Case (after update) {
    System.debug('Hii Update Trigger');
        AvoidRecursion.isFirstTime = false;

<<<<----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
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