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
Rustin FinckeRustin Fincke 

Update parent record status when all children status closed

Hey all,

I've been looking for a solution to this and cannot seem to find anything to accomplish what I am trying to do.  I'm trying to update a Parent in the Case object and change the status to Completed only after all child cases and related cases have been closed. (We use a record type called Master Case in the case Object and create addtional case record types for various tasks for different teams. These tasks can include other cases in the case object or other Custom Objects)  

We're trying to update the Parent case status when all the related cases and objects are completed.    

I found the following code but we're not using the milestone feature at this time: https://developer.salesforce.com/forums/?id=906F0000000As65IAC

 
Best Answer chosen by Rustin Fincke
NagaNaga (Salesforce Developers) 
Hi Rustin,

Please see a similart code which updates parent's records when the child records are closed.
Please let me know if it helps

User-added imageBest Regards
Naga kiran

All Answers

NagaNaga (Salesforce Developers) 
Hi Rustin,

Please see a similart code which updates parent's records when the child records are closed.
Please let me know if it helps

User-added imageBest Regards
Naga kiran
This was selected as the best answer
Santosh kyatanavarSantosh kyatanavar
Hi All,

It will work, if all child object records status is set  to 'closed',  parent object we can set as closed.


Trigger ChildObjTrigger on childObj(after insert,after update){
    List<ParentObj> ParentObjList = new List<ParentObj>();
    List<ParentObj> ParentObjListToUpdate = new List<ParentObj>();
    Set<Id> parentId = new set<Id>();

    for(childObj child : Trigger.new){
        if(child.Status == 'Closed'){
            parentId.add(child.parentObj);
        }
    }

    if(parentId.size() > 0)
    {
        ParentObjList = [SELECT Id,Status,(SELECT Id,Status
                                                FROM childObj__r) FROM ParentObj WHERE Id IN : parentId];
        for(ParentObj po : ParentObjList){
            Boolean setToClosed = true;
            for(childObj co : po.childObj__r){
            //put condtion which all status you dont want to set Closed
            //any other conditions also we can check
                if(co.Status == 'Open' || co.Status == 'Canceled'|| co.Status == 'Completed'){
                    setToClosed = False;
                    break;
                }                
            }
            if(setToClosed){
                po.Status = 'Closed';
            }
            ParentObjListToUpdate.add(c);
        }
    }
    if(!ParentObjListToUpdate.isEmpty() || ParentObjListToUpdate.size() > 0){
        update ParentObjListToUpdate;
    }
}



Thanks,
Santosh K