You need to sign in to do that
Don't have an account?
Pavan 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); } } }
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
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,
All Answers
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
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,