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
JNicJNic 

Case Trigger "de-reference a null object" error

Hi,

 

I have an apex trigger on Case object that is supposed to automatically associate Cases to Contact records based on the AccountId field and its properties.

My trigger works well when I update existing Cases, but it throws the "attempt to de-reference a null object" error whenever I create a new Case.

 

This trigger first looks at the Email__c field set on the Account associated (Case.Account.Email__c) and tries to find existing contacts with that email address. If it didn't find a contact with that email address, it creates a Contact with some account info and associates it with the Case accordingly. If it did find contacts with that email address, it will query for them and associate them with the Case.

 

Here is my code (the line causing the error is highlighted in red):

 

trigger caseNSFBefore on Case (before insert, before update) {
    List<String> emailAddresses = new List<String>();
    List<String> accountIds = new List<String>();
    //First exclude any cases where the contact is set
    for (Case caseObj:Trigger.new) {
        if (caseObj.ContactId==null &&
            caseObj.AccountId != null)
        {
            accountIds.add(caseObj.AccountId);
            System.debug('AccountEmail*********' + accountIds);
            List<Account> listAccounts = [Select Id,Email__c, firstName__c, lastName__c, primePhone__c From Account Where Id in :accountIds];
            for (Account a :listAccounts){
            emailAddresses.add(a.Email__c);
            System.debug('AccountEmail*********' + emailAddresses);
            }

        }
    }
    //fetch all the Account records from database
    Map<Id, Account> mapAccounts = new Map<Id, Account>([Select Id, Email__c, firstName__c, lastName__c, primePhone__c From Account Where Id in :accountIds]);

    //Now we have a nice list of all the email addresses.  Let's query on it and see how many contacts already exist.
    List<Contact> listContacts = [Select Id,Email From Contact Where Email in :emailAddresses];
    Set<String> takenEmails = new Set<String>();
    for (Contact c:listContacts) {
        takenEmails.add(c.Email);
    }
    
    Map<String,Contact> emailToContactMap = new Map<String,Contact>();
    List<Case> casesToUpdate = new List<Case>();
    List<Case> casesToUpdate1 = new List<Case>();



    for (Case caseObj:Trigger.new) {
        if (caseObj.ContactId==null &&
            caseObj.AccountId !=null &&
           !takenEmails.contains(mapAccounts.get(caseObj.AccountId).Email__c))
        {
                List<Account> listAccounts = [Select Id,Email__c, firstName__c, lastName__c, primePhone__c From Account Where Id in :accountIds];
                for (Account a :listAccounts){
                    
                Contact cont = new Contact(FirstName= a.firstName__c,
                                            LastName= a.lastName__c,
                                            AccountId = a.Id,
                                            Phone = a.primePhone__c,
                                            Email= a.Email__c);
                emailToContactMap.put(mapAccounts.get(caseObj.AccountId).Email__c,cont);
                casesToUpdate.add(caseObj);
                
                }
        } else {
        casesToUpdate1.add(caseObj);    
        }
    }
    
    List<Contact> newContacts = emailToContactMap.values();
    insert newContacts;
    
    for (Case caseObj:casesToUpdate) {
        Contact newContact = emailToContactMap.get(mapAccounts.get(caseObj.AccountId).Email__c);
        
        caseObj.ContactId = newContact.Id;
    }
        for (Case caseObj:casesToUpdate1) {
        Contact existingContact = [select id from Contact where Email = :mapAccounts.get(caseObj.AccountId).Email__c  limit 1];        
        caseObj.ContactId = existingContact.Id;
    }
}

 

 

I would appreciate your help.

 

 

Thanks,

 

jreehjreeh

It might be possible that your Map is not finding a match for the key you provided it. Therefore getting a de-reference null error.

JNicJNic

Through debug logs I verified Map is getting the correct Account.Email__c.

 

I think I need another Map to carry over the existing Contact with that Map in the initial for loop - similiar to the emailToContactMap.