0

I have a requirement to check Terminated__c flag on multiple child records (lead_Custom__c) and if it is true, then update the Terminated__c flag on Parent record (contact).

Below is the apex class:

public with sharing class Lead_TerminationCheck {
    public void UpdateFields(List<Lead_Custom__c> newList) {
        List<Contact> updateList = new List<Contact>();
        Map<id,List<Lead_Custom__c>> mapParentIDAndChild = new Map<id,List<Lead_Custom__c>>();
        boolean flag;
        set<Id> parenetTrue = new set<Id>();
        for(Lead_Custom__c objChild : newList) {
            parenetTrue.add(objChild.Contact__c);
        }
        for(Lead_Custom__c objChild : [select id , Terminated__c , Contact__c from Lead_Custom__c where Contact__c IN: parenetTrue] ){
            if(mapParentIDAndChild.containsKey(objChild.Contact__c)) {
                mapParentIDAndChild.get(objChild.Contact__c).add(objChild);
            } else {
                mapParentIDAndChild.put(objChild.Contact__c,new List<Lead_Custom__c>{objChild});
            }
        }
        for(Id objId : mapParentIDAndChild.keySet()) {
            flag = true;
            for(Lead_Custom__c objChild : mapParentIDAndChild.get(objId)) {
                if(objChild.Terminated__c == false){
                    flag = false;
                    break;
                }
            }
            if(flag == true) {
                updateList.add(new Contact(Id = objId , Terminated__c = true));
            } 
        }
        if(!updateList.isEmpty()) {
            update updateList;
        }

    }
}

The above apex class is working only if all the child records have Termination__c as true. But, is there any way to check the parent has more than one child record and if yes, then even if any one the child record has this flag set to true, then parent's terminated flag should be updated.

Can anyone please suggest the changes to the above apex class so that I can get this done.

Thanks!

0

I would rather do a query on the parent.

First collect the ids of the contacts from new list.

set<Id> parenetTrue = new set<Id>();
for(Lead_Custom__c objChild : newList) {
    parenetTrue.add(objChild.Contact__c);
}

Then

Contact__c[] contacts = [SELECT (SELECT ID,Terminated__c FROM leadCustom_1__r) FROM Contact WHERE Id IN : parenetTrue];
for(Contact contactObj: contacts){
    if(contactObj.leadCustom_1__r.size() > 1){
        for(Lead_Custom__c objChild : contactObj.leadCustom_1__r){
            if(objChild.Terminated__c == true){
                updateList.add(new Contact(Id = contactObj.Id, Terminated__c = true));
                break;
            }
        }
    }
}
6
  • i am getting error that Lead_Custom__r does not exists. The lookup relationship name is leadCustom_1__c. Even when I try, leadCustom_1__r, that didnt work. How to call the child field here? – SK_112 Aug 28 '20 at 16:48
  • Use whatever child relationship name you have. I had preassunrd that.. also as i am checking the condition in directly in the query so you don't need to check explicitly – Rahul Gawale Aug 28 '20 at 16:57
  • 1
    using this given query, even if the contact has one child record with terminated checked, the contact's terminated is getting checked. If I increase the count to 2 and check the size for >1, the contact's terminated is getting checked only if any 2 of child records are marked true for terminated checkbox. As per my requirement, i need to check if the parent has more than one child and if any of the child is marked true, then parent should be true. Please suggest – SK_112 Aug 31 '20 at 7:15
  • 1
    @SK_112 try the updated code – Rahul Gawale Aug 31 '20 at 9:28
  • Thanks @rahul. It is working as expected. As a final suggestion, could you please suggest if there any way i can uncheck the parent terminated checkbox if none of the child's checkbox is true? – SK_112 Aug 31 '20 at 11:36
2

Your logic is backwards.

        flag = true;
        for(Lead_Custom__c objChild : mapParentIDAndChild.get(objId)) {
            if(objChild.Terminated__c == false){
                flag = false;
                break;
            }
        }
        if(flag == true) {
            updateList.add(new Contact(Id = objId , Terminated__c = true));
        } 

You are setting flag to false if any Lead_Custom__c record has the Terminated__c field set to false. That is the opposite of what you stated you want, for Terminated__c to be true if any Lead_Custom__c record has it set to true.

You need to swap the initial value of flag to false, look for objChild.Terminated__c to be true, set flag to be true. You may also wish to store your Contacts and check whether its Terminated flag needs to change, to be efficient in your updates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.