You need to sign in to do that
Don't have an account?

Update Contact other phone field and populate it with the parent Account phone field
Hello everyone,
This is my code, I wanted to write this class with all best practises recommended.
please help me to rectify this code also i want it to work if both account and contact have same country.
public class Update_Contact_Phone {
public static void contactOtherPhone(List<Account> AccountList){
set <id> IdCollect = new set <id>();
List <Contact> Contacts = [SELECT Id,
OtherPhone
From Contact
WHERE AccountId IN :IdCollect];
For(Account acc :AccountList){
If (acc.Phone != null){
IdCollect.add(acc.Id);
}
}
Map <Id, string> conmap = new Map <Id, string>();
For(Account acc : AccountList){
conmap.put(acc.Id , acc.Phone);
}
For(Contact c : Contacts ){
c.OtherPhone = conmap.get(c.AccountId);
}
Update Contacts;
}}
This is my code, I wanted to write this class with all best practises recommended.
please help me to rectify this code also i want it to work if both account and contact have same country.
public class Update_Contact_Phone {
public static void contactOtherPhone(List<Account> AccountList){
set <id> IdCollect = new set <id>();
List <Contact> Contacts = [SELECT Id,
OtherPhone
From Contact
WHERE AccountId IN :IdCollect];
For(Account acc :AccountList){
If (acc.Phone != null){
IdCollect.add(acc.Id);
}
}
Map <Id, string> conmap = new Map <Id, string>();
For(Account acc : AccountList){
conmap.put(acc.Id , acc.Phone);
}
For(Contact c : Contacts ){
c.OtherPhone = conmap.get(c.AccountId);
}
Update Contacts;
}}
You can also use this code.
public class Update_Contact_Phone {
public static void contactOtherPhone(map<Id,Account> newAccMap){
set <id> IdCollect = newAccMap.keySet();
List<Contact> toUpdateconList = new List<Contact>();
if(IdCollect.size() > 0){
List <Contact> conList = [SELECT MailingCountry,OtherPhone,AccountId From Contact WHERE AccountId IN :IdCollect];
if(conList.size()>0){
for(Contact c:conList){
if(c.MailingCountry==newAccMap.get(c.AccountId).BillingCountry){
c.OtherPhone = newAccMap.get(c.AccountId).Phone;
toUpdateconList.add(c);
}
}
}
if(toUpdateconList.size()>0){
update toUpdateconList;
}
}
}
}
Please mark it as best Answer if you find it helpful.
Thank You
Ajay Dubedi
All Answers
You can do with this.
if it helps you mark this as Best Answer
http://lakshmideepak.com (http://lakshmideepak.com" target="https:lakshmideepak.com)
site : http://lakshmideepak.com
If you really want to optimize this code then I suggest you pass a map as a parameter to the method. Then your code will look like this:
Please check this code it runs successfully.
I hope you understand this.
public class Update_Contact_Phone {
public static void contactOtherPhone(List<Account> AccountList){
set <id> IdCollect = new set <id>();
For(Account acc :AccountList){
If (acc.Phone != null){
IdCollect.add(acc.Id);
}
}
List <Contact> Contacts = [SELECT Id,
OtherPhone,
AccountId,
MailingCountry
From Contact
WHERE AccountId IN :IdCollect];
System.debug('Query cont=='+contacts);
Map <Id, string> conmap = new Map <Id, string>();
for(Account acc : AccountList){
conmap.put(acc.Id , acc.Phone);
}
Map <Id, string> conmap1 = new Map <Id, string>();
for(Account acc : AccountList){
conmap1.put(acc.Id,acc.BillingCountry);
}
if(Contacts!=null){
for(Contact c : Contacts ){
if(c.MailingCountry==conmap1.get(c.AccountId)){
c.OtherPhone = conmap.get(c.AccountId);
}
}
}
if(Contacts.size()>0){
Update Contacts;
}
}
}
Please mark it as best Answer if you find it helpful.
Thank You
Ajay Dubedi
You can also use this code.
public class Update_Contact_Phone {
public static void contactOtherPhone(map<Id,Account> newAccMap){
set <id> IdCollect = newAccMap.keySet();
List<Contact> toUpdateconList = new List<Contact>();
if(IdCollect.size() > 0){
List <Contact> conList = [SELECT MailingCountry,OtherPhone,AccountId From Contact WHERE AccountId IN :IdCollect];
if(conList.size()>0){
for(Contact c:conList){
if(c.MailingCountry==newAccMap.get(c.AccountId).BillingCountry){
c.OtherPhone = newAccMap.get(c.AccountId).Phone;
toUpdateconList.add(c);
}
}
}
if(toUpdateconList.size()>0){
update toUpdateconList;
}
}
}
}
Please mark it as best Answer if you find it helpful.
Thank You
Ajay Dubedi
The second you suggested it didnt work quite well this error is coming up.
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger UpdateNumber caused an unexpected exception, contact your administrator: UpdateNumber: execution of BeforeUpdate caused by: line 3, column 30: Method does not exist or incorrect signature: void contactOtherPhone(List) from the type Update_Contact_Phone
This above code is working fine in my Org.
you can use above apex class with given trigger class which is given below.
I hope this run successfully.
// Trigger class //
Trigger updateContactPhone on Account(before update){
Update__Contact__Phone.contactOtherPhone(Trigger.NewMap);
Please mark as solve If you understand this.
Thank You
Ajay Dubedi
Please look into my issue related to similar trigger problem
I am getting below error for the given trigger.
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger AccountPhone caused an unexpected exception, contact your administrator: AccountPhone: execution of BeforeUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Contact.AccountId: Trigger.AccountPhone: line 27, column 1
------------------------------------------------------------------------
Trigger scenerio is :
------------------------------------------------------------------------
whenever a account phone is modified/updated then contact
records with phone fields(otherphone with oldvalue and homephone with newvalue) associated with this account record gets updated as same as account phone field.
-----------------------trigger code-----------------------------------------------
trigger AccountPhone on Account (after update) {
Map<Id,Account> oldmapvalues=Trigger.oldmap;
Map<Id,Account> newmapvalues=Trigger.newmap;
Set<Id> accids=oldmapvalues.keyset();
Set<Id> updatedaccids=new Set<Id>();
//Adding Account ids for those accounts whose phone field is updated.
for(Id a:accids){
if(newmapvalues.get(a).phone!=oldmapvalues.get(a).phone){
updatedaccids.add(a);
}}
List<Contact> clist= new List<Contact>();
for(Contact c:[select lastname,phone from contact where accountid in :updatedaccids]){
c.otherphone=oldmapvalues.get(c.accountid).phone;
c.homephone=newmapvalues.get(c.accountid).phone;
clist.add(c);
}
update clist;
}
.Please let me know where i am wrong.