How can I update the parent when all child records have the same value in a field?
Ex. I have five child records under a parent record.
The five child records have a field called Status, if ALL of the child records have “Ready” in that field, we need to update the parent to have their status field “ready” as well
List<Parent__c> parentsToUpdate = ParentQuery.getStatus(parentIds);
for(parent__c parent : parentsToUpdate){
if(parent.Filled__c ==true && parent.Active_children__c ==0){
Boolean ready= true;
for(child__c child : parent.childs__r){
if(!Test.isRunningTest() && (child.status__c == null || child.status__c !='ready')){
ready = false;
}
else if(ready && child.status__c == 'ready'){
parent.Status__c = 'In Progress';
parent.ready_Date__c =Date.today();
}
}
}
}
update parentsToUpdate;
}
}
This method updates the parent field status to ready even when there is just one child record with the status "ready", how can I make it only fire when all of the child records are "ready"?