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
Akash Choudhary 17Akash Choudhary 17 

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;
    }}
Best Answer chosen by Akash Choudhary 17
Ajay K DubediAjay K Dubedi
Hi Akash,

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

Lakshmi Deepak InkurthiLakshmi Deepak Inkurthi

You can do with this.

 

if it helps you mark this as Best Answer 
http://lakshmideepak.com (http://lakshmideepak.com" target="https:lakshmideepak.com)

public class Update_Contact_Phone {
    public static void contactOtherPhone(List<Account> AccountList){
       List<Contact> conlist = new List<Contact>();
     for(Account acc : [Select Id , Phone , (Select id , OtherPhone FROM Contacts)  From Account WHERE ID IN:AccountList ]){
     for(Contact c : acc.Contacts){
   c.OtherPhone = acc.Phone;
conlist .add(c);
}
if(conlist.size() > 0){
update conlist ;
}
}
    }}
Akash Choudhary 17Akash Choudhary 17
I want to use Map for this
Lakshmi Deepak InkurthiLakshmi Deepak Inkurthi
There is no other way. By using accountlist as a Key parameter for source

site : http://lakshmideepak.com
public class Update_Contact_Phone {
    public static void contactOtherPhone(List<Account> AccountList){
      Map<Id,String> accMap = new Map<Id,String>();
	  for(Accounty acc : AccountList){
		if(acc.Phone !=null){
		accMap.put(acc.id,acc.phone);
		}
	  }
	  List<Contact> conlist = new List<Contact>();
	  for(Contact Con : [Select Id,OtherPhone,AccountId FROM Contact WHERE AccountID IN:accMap.keyset()]){
		con.OtherPhone = accMap.get(con.accountId);
		conlist.add(con);
	  }
	  if(conlist.size() > 0 ){
	  update conlist;
	  }
}
    }}

 
Narender Singh(Nads)Narender Singh(Nads)
Hi,
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:
 
public class Update_Contact_Phone {
    
    public static void contactOtherPhone(Map <Id, account> Accmap){
        Contact[] ListToupdate=new contact[]{};
        List <Contact> Contacts = [SELECT Id,OtherPhone From Contact WHERE AccountId IN :Accmap.keyset()];
        
&nbsp;       For(Contact c : Contacts ){
            if(Accmap.get(c.AccountId).otherphone!=NULL){
            	c.OtherPhone = Accmap.get(c.AccountId).otherphone;
            	ListToupdate.add(c);
            }
        }
&nbsp;      if(ListToupdate.size()>0)
            Update ListToupdate;
    }
}

 
Narender Singh(Nads)Narender Singh(Nads)
Remove &nbsp; from the code *
Ajay K DubediAjay K Dubedi
Hi Akash,

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
Ajay K DubediAjay K Dubedi
Hi Akash,

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
This was selected as the best answer
Akash Choudhary 17Akash Choudhary 17
Hi Ajay , 
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
 
Ajay K DubediAjay K Dubedi
Hi Akash,

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
Meenal Sankhla 12Meenal Sankhla 12
Hi Ajay,

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.