1

I encounter an error when I creating new Account.

contactFromAccounts: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Class.AccountTriggerHandler.updateContactAddresses: line 7, column 1 Trigger.contactFromAccounts: line 3, column 1

What's wrong this code?

public class AccountTriggerHandler
{
    public static void updateContactAddresses(Map<ID, Account> updatedAccounts, Map<ID, Account> oldAccounts)
    {
        Set<Id> accountIds = new Set<Id>();
        for (Account a : updatedAccounts.values()) {
            Account old = oldAccounts.get(a.Id);
            if (a.billingstreet != old.billingstreet|| a.billingcity !=old.billingcity || a.billingstate !=old.billingstate || a.billingcountry != old.billingcountry) {
                accountIds.add(a.Id);
            }
        }
        if (accountIds.size() > 0) {
            Contact[] updateAddress = [SELECT Id, accountId FROM contact WHERE accountId IN :accountIds];
            for (contact c : updateAddress) {
                account a = updatedAccounts.get(c.accountId);
                c.mailingStreet = a.billingStreet;
                c.mailingcity = a.billingcity;
                c.mailingstate = a.billingstate;
                c.mailingpostalcode = a.billingpostalcode;
                c.mailingcountry = a.billingcountry;
                }
            update updateAddress;
         }
    }
}

trigger contactFromAccounts on Account (after insert, after update) {
    if(Trigger.isInsert || Trigger.isUpdate){
          AccountTriggerHandler.updateContactAddresses(Trigger.NewMap, Trigger.OldMap);    
    }
}

Appreciate any solutions and help.

1

Your oldAccounts variable will be always null in insert context, hence you are getting exception when you are trying to get a value from a map in insert context which would be null. To fix this, you will have to change your logic in the code, so that it works both for insert and update context.

Change your code to below

for (Account a : updatedAccounts.values() { 
    if(oldAccounts !=null) {
        Account old = oldAccounts.get(a.Id); 
        if (a.billingstreet != old.billingstreet|| a.billingcity !=old.billingcity || a.billingstate !=old.billingstate || a.billingcountry != old.billingcountry) { 
            accountIds.add(a.Id); 
        }
    } else {
       accountIds.add(a.Id); 
    }
}
0

You should check containsKey() and isEmpty() before accessing a map value. One thing to note is that if the trigger fires on insert context, oldAccounts map will not having that record. This is why you are getting null pointer exception. Please use the below code to fix. You can optimise the code to improve efficiency.

public class AccountTriggerHandler
{
    public static void updateContactAddresses(Map<ID, Account> updatedAccounts, Map<ID, Account> oldAccounts)
    {
        Set<Id> accountIds = new Set<Id>();
        for (Account a : updatedAccounts.values()) {
            if((!oldAccounts.isEmpty()) && oldAccounts.containsKey(a.Id)){
                Account old = oldAccounts.get(a.Id);
                if (a.billingstreet != old.billingstreet|| a.billingcity !=old.billingcity || a.billingstate !=old.billingstate || a.billingcountry != old.billingcountry) {
                    accountIds.add(a.Id);
                }
            }
        }
        if (accountIds.size() > 0) {
            Contact[] updateAddress = [SELECT Id, accountId FROM contact WHERE accountId IN :accountIds];
            for (contact c : updateAddress) {
                account a = updatedAccounts.get(c.accountId);
                c.mailingStreet = a.billingStreet;
                c.mailingcity = a.billingcity;
                c.mailingstate = a.billingstate;
                c.mailingpostalcode = a.billingpostalcode;
                c.mailingcountry = a.billingcountry;
                }
            update updateAddress;
         }
    }
}

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.