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
Pavan SonarePavan Sonare 

nullPoint exception

I am trying to write a trigger for an account object which will copy phone to its related contact phone. While the code is clear in syntax but I am getting an error while execution of null point exeption
public class UpdatePhoneOnContacts { // this is helper class and logic
    
    public static void methodForThisClass(list<account> accList, map<id, account> oldMap){
        list<contact> conList = new list<contact>();
        map<id, account> mapOfAcc = new map<id, account>();
        for(account acc : accList){
            if((acc.Phone != null && acc.Phone!=oldMap.get(acc.Id).phone)&& oldMap!=null){
                mapOfAcc.put(acc.Id, acc);
            }
        }
        for(contact con:[select id, name, homePhone, accountId from contact where accountId =: mapOfAcc.keySet()]){
            if(mapOfAcc.containsKey(con.accountId)){
                con.homePhone = mapOfAcc.get(con.Id).phone;
                conList.add(con);
            }
            if(!conlist.isEmpty()){
                update conList;
            }
        }
    }

}
 
trigger RealtedRecords on Account (after update) {// this is its trigger
    
    if(trigger.isAfter){
        if(trigger.isUpdate){
            UpdatePhoneOnContacts.methodForThisClass(trigger.new, trigger.oldMap);
        }
    }
}

Error Screenshot
Best Answer chosen by Pavan Sonare
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Pavan,

There are two small errors in your code as highlited.

Fiistly you need to check the size of the mapOfAcc.
Secondly the map contains key as AccountId so you have to use mapOfAcc.get(con.accountId).phone
 
public class UpdatePhoneOnContacts { // this is helper class and logic
    
    public static void methodForThisClass(list<account> accList, map<id, account> oldMap){
        list<contact> conList = new list<contact>();
        map<id, account> mapOfAcc = new map<id, account>();
        for(account acc : accList){
            if((acc.Phone != null && acc.Phone!=oldMap.get(acc.Id).phone)&& oldMap!=null){
                mapOfAcc.put(acc.Id, acc);
            }
        }
        if(mapOfAcc.size()>0 ){       
        for(contact con:[select id, name, homePhone, accountId from contact where accountId =: mapOfAcc.keySet()]){
            if(mapOfAcc.containsKey(con.accountId)){
                con.homePhone = mapOfAcc.get(con.accountId).phone;
                conList.add(con);
            }
            if(!conlist.isEmpty()){
                update conList;
            }
        }
        }
    }

}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Pavan,

There are two small errors in your code as highlited.

Fiistly you need to check the size of the mapOfAcc.
Secondly the map contains key as AccountId so you have to use mapOfAcc.get(con.accountId).phone
 
public class UpdatePhoneOnContacts { // this is helper class and logic
    
    public static void methodForThisClass(list<account> accList, map<id, account> oldMap){
        list<contact> conList = new list<contact>();
        map<id, account> mapOfAcc = new map<id, account>();
        for(account acc : accList){
            if((acc.Phone != null && acc.Phone!=oldMap.get(acc.Id).phone)&& oldMap!=null){
                mapOfAcc.put(acc.Id, acc);
            }
        }
        if(mapOfAcc.size()>0 ){       
        for(contact con:[select id, name, homePhone, accountId from contact where accountId =: mapOfAcc.keySet()]){
            if(mapOfAcc.containsKey(con.accountId)){
                con.homePhone = mapOfAcc.get(con.accountId).phone;
                conList.add(con);
            }
            if(!conlist.isEmpty()){
                update conList;
            }
        }
        }
    }

}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer
Pavan SonarePavan Sonare
Thanks Sai Praveen; The issue is now resolved.