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
Beauty Kumari 1Beauty Kumari 1 

How can I match a field of parent in child records in a apex trigger

I created a trigger on case. Member_ID__c is a auto-number field on contact object. I am trying to find out repeat cases logged by a contact using custom checkbox Repeat_Caller__c on case. There are many other conditions, but I removed them in this code for simplicity. I am not able to match Member_ID__c field in cases even though multiple cases are created by the same contact. In other words, I am not able to enter the IF condition. I have pasted the trigger below.

Please guide me as to where the problem might be. Any help is appreciated.

 
trigger repeatCase1 on Case (before insert,before update) {
    List<Case> caseList = trigger.new;   
    Set<id> memberIdSet = new set<id>();
    for(case c: caseList){
        memberIdSet.add(c.Contact.Member_ID__c);
    }
      List<Case> allcase = [select Contact.Member_ID__c from case where contact.Member_ID__c IN: memberIdSet];               
      for(Case cs: caseList ){  
        for(Case all: allcase ){ 
            if(cs.contact.Member_ID__c == all.contact.Member_ID__c ){
                    cs.Repeat_Caller__c=True;
            }
        }
    }
    
}
Best Answer chosen by Beauty Kumari 1
Boss CoffeeBoss Coffee
I don't believe trigger.new will have access to its parent's fields, so attempting to access those fields will return null. The following handles before insert. I use a Map to hold the Contacts that the Trigger.new Cases look up to, thus you can access the appropriate Contact Member Id field when you do your checks in the for loop.
if(Trigger.isBefore && Trigger.isInsert) {
        // Get the list of Cases from trigger.new
        List<Case> caseList = trigger.new;   
        
        // Get a set of Contact Ids that will be used for queries
        Set<Id> contactIds = new Set<Id>();
        for(case c: caseList){
            // For each case from the trigger, add its Contact Id to the set
            contactIds.add(c.ContactId);
        }
        
        // Now, get a list of cases that would look up to the same Contact (and safety check to make sure its Member Id field is not null)
        List<Case> allcase = [select Id, ContactId, Contact.Member_Id__c from case WHERE ContactId IN :contactIds AND Contact.Member_Id__c != null];
        
        // Grab a Map of Contacts that are looked up to by the trigger Cases
        Map<Id,Contact> conMap = new Map<Id,Contact>([SELECT Id, Name, Member_Id__c FROM Contact WHERE Id IN :contactIds AND Contact.Member_Id__c != null]);
        
        for(Case cs: caseList ){  
            for(Case all: allcase ){ 
                // For the trigger Case, grab the Contact Member Id field from the Map and compare with the Case already existing
                if(conMap.get(cs.ContactId).Member_Id__c == all.Contact.Member_Id__c ){
                    // If there is a match, then set the field to true
                    cs.Repeat_Caller__c=True;
                }
            }
        }
    }

All Answers

Boss CoffeeBoss Coffee
I don't believe trigger.new will have access to its parent's fields, so attempting to access those fields will return null. The following handles before insert. I use a Map to hold the Contacts that the Trigger.new Cases look up to, thus you can access the appropriate Contact Member Id field when you do your checks in the for loop.
if(Trigger.isBefore && Trigger.isInsert) {
        // Get the list of Cases from trigger.new
        List<Case> caseList = trigger.new;   
        
        // Get a set of Contact Ids that will be used for queries
        Set<Id> contactIds = new Set<Id>();
        for(case c: caseList){
            // For each case from the trigger, add its Contact Id to the set
            contactIds.add(c.ContactId);
        }
        
        // Now, get a list of cases that would look up to the same Contact (and safety check to make sure its Member Id field is not null)
        List<Case> allcase = [select Id, ContactId, Contact.Member_Id__c from case WHERE ContactId IN :contactIds AND Contact.Member_Id__c != null];
        
        // Grab a Map of Contacts that are looked up to by the trigger Cases
        Map<Id,Contact> conMap = new Map<Id,Contact>([SELECT Id, Name, Member_Id__c FROM Contact WHERE Id IN :contactIds AND Contact.Member_Id__c != null]);
        
        for(Case cs: caseList ){  
            for(Case all: allcase ){ 
                // For the trigger Case, grab the Contact Member Id field from the Map and compare with the Case already existing
                if(conMap.get(cs.ContactId).Member_Id__c == all.Contact.Member_Id__c ){
                    // If there is a match, then set the field to true
                    cs.Repeat_Caller__c=True;
                }
            }
        }
    }
This was selected as the best answer
Beauty Kumari 1Beauty Kumari 1
Thanks for the solution! It works perfectly well.
In order to obey governor limits, how can I use map instead of inner for loop in this case? 
Boss CoffeeBoss Coffee
You can create a set that includes the Member Ids, and then instead of an inner loop, you can check if the Member Id already exists in that particular Set.
        // Populate a Set of Member Ids from the allcase Case list
        Set<String> memberIds = new Set<String>();
        for(Case c : allcase) {
            memberIds.add(c.Contact.Member_Id__c);
        }
Then your new for loop will be like the following.
        for(Case cs: caseList ) {  
            // Check if the map contains the Member Id to see if we need to check the repeat caller checkbox
            if(memberIds.contains(conMap.get(cs.ContactId).Member_Id__c)) {
                cs.Repeat_Caller__c=True;
            }
        }
Syed MD AnasSyed MD Anas
<div class="feedcommenttext">Download latest android mod apks: PowerDirector Pro APK (<a href="https://www.modmafia.co/powerdirector-pro-apk/" target="_blank">https://www.modmafia.co/powerdirector-pro-apk/</a>): CyberLink PowerDirector Video Editor v5.4.1 Unlocked APK is Here to download on your Android Device. No Watermark, No restrictions to all premium features. Easy to use with the simple timeline. The best Mobile Video editor for Beginners.<br><br><br> </div></strong>
samir pastagiasamir pastagia
MovieStarPlanet is a game where you can have tons of fun with your best friends msp hack (https://msphackcheats.net/
Do you want to get FREE psn codes for your Playstation? free psn codes 2019 (https://freepsncodes2019.com/)
Help DiskHelp Disk
Thanks for the solution! It works perfectly well.
shared because it provided a lot of knowledge about PUBG NEWS (https://www.pubgnews.website/" target="_blank)
 
Hayato KawajiriHayato Kawajiri
"I'm sorry, Alex, but your position started the moment you signed, and, as such, there are no more questions to be asked, only prompt assumption of orders. So, if you'll both lie in your assigned tables, as B413-A and B413-B, that would be grand, thank you. No need to undress, that will be taken care of けいれん (https://rottenhayato.com/ja/category/216/けいれん/popular/1/)." Alex and Tiffany stared, but quickly stepped up onto their tables, lying back. Liz nodded, and smiled a little. "See you in six months."