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
AswiniAswini 

How to write a trigger to update each contact's phone using Account's phone if contact's Mailing country is equal to Account's Billing Country.

How to write a  trigger to update each contact's phone using Account's phone if contact's Mailing country is equal to Account's Billing Country.
Best Answer chosen by Aswini
CharuDuttCharuDutt
Hii Ashwini
Try Below Code
trigger updateContactAddress on Account (after update) {
  
	Map<Id,String>AccountMap = new Map<Id,String>();
	List<Contact> lstCon = new List<Contact>();
		for(Account Acc : Trigger.New){
			if(Acc.Phone != Trigger.oldMap.get(Acc.Id).Phone){
				AccountMap.Put(Acc.id,Acc.Phone);
		}
	}
		
		list<Account> lstAccount = [Select Id,Phone,BillingCountry,(Select Id,Phone,MailingCountry from Contacts)from Account Where Id in : AccountMap.keySet()];
		for(Account Acc : lstAccount){
		for(Contact con : Acc.Contacts){
		if(Acc.BillingCountry == con.MailingCountry){
				con.phone = AccountMap.get(Acc.Id);
				lstCon.add(con);
				}
			}
		}
		if(lstCon.size()>0){
		update lstCon;
		}
}
Please Mark It As Best Answer If It Helps
Thank You!

All Answers

CharuDuttCharuDutt
Hii Ashwni
Try Below Trigger
trigger updateContactAddress on Account (after update) {
	Map<Id,String>AccountMap = new Map<Id,String>AccountMap();
	list<Contact> LstCon = new list<Contact>();
		for(Account Acc : Trigger.New){
			if(Acc.Phone != Trigger.oldMap.get(Acc.Id).Phone){
				AccounrMap.Put(Acc.id,Acc.Phone);
		}
	}

		list<Account> lstAccount = [Select,Id,Phone,BillingCountry,(Select Id,Phone,MailingCountry from Contacts) 
									from Account Where Id IN : AccountMap.keySet()];
		for(Account Acc : lstAccount){
		for(Contact Con : Acc.Contacts){
		if(Acc.BillingCountry == Con.MaillingCountry){
				con.phone = Account.get(Acc.Id);
				LstCon.add(Con.Id);
				}
			}
		}
		if(LstCon.size()>0){
		update LstCon;
		}
}
Please Mark It As Best Answer If It Helps
Thank You!
AswiniAswini
Hi Charu,
I got a small error in the above code could you plz help me to solve that.User-added image
CharuDuttCharuDutt
Hii Ashwini 
Try Below Code Error Has Been Removed
trigger updateContactAddress on Account (after update) {
  
	Map<Id,String>AccountMap = new Map<Id,String>();
	
		for(Account Acc : Trigger.New){
			if(Acc.Phone != Trigger.oldMap.get(Acc.Id).Phone){
				AccountMap.Put(Acc.id,Acc.Phone);
		}
	}
		
		list<Account> lstAccount = [Select Id,Phone,BillingCountry,(Select Id,Phone,MailingCountry from Contacts)from Account Where Id in : AccountMap.keySet()];
		for(Account Acc : lstAccount){
		for(Contact con : Acc.Contacts){
		if(Acc.BillingCountry == con.MailingCountry){
				con.phone = AccountMap.get(Acc.Id);
				
				}
			}
		}
		if(lstAccount.size()>0){
		update lstAccount;
		}
}
Please Mark It As Best Answer If It Helps
Thank You!
CharuDuttCharuDutt
Hii Ashwini
Try Below Code
trigger updateContactAddress on Account (after update) {
  
	Map<Id,String>AccountMap = new Map<Id,String>();
	List<Contact> lstCon = new List<Contact>();
		for(Account Acc : Trigger.New){
			if(Acc.Phone != Trigger.oldMap.get(Acc.Id).Phone){
				AccountMap.Put(Acc.id,Acc.Phone);
		}
	}
		
		list<Account> lstAccount = [Select Id,Phone,BillingCountry,(Select Id,Phone,MailingCountry from Contacts)from Account Where Id in : AccountMap.keySet()];
		for(Account Acc : lstAccount){
		for(Contact con : Acc.Contacts){
		if(Acc.BillingCountry == con.MailingCountry){
				con.phone = AccountMap.get(Acc.Id);
				lstCon.add(con);
				}
			}
		}
		if(lstCon.size()>0){
		update lstCon;
		}
}
Please Mark It As Best Answer If It Helps
Thank You!
This was selected as the best answer
mukesh guptamukesh gupta
Hi Ashwini,

Please use below code:-
 
trigger updateContactPhone on Account (after update) {
  
	Map<Id,String>accMap= new Map<Id,String>();
	List<Contact> lstCon = new List<Contact>();
		for(Account acc : Trigger.New){
			if(acc.Phone != Trigger.oldMap.get(acc.Id).Phone){
				accMap.Put(acc.id,acc.Phone);
		}
	}
		
		list<Account> lstAccount = [Select Id,Phone,BillingCountry,(Select Id,Phone,MailingCountry from Contacts)from Account Where Id in : accMap.keySet()];
		for(Account acc: lstAccount){
		for(Contact con : acc.Contacts){
		if(acc.BillingCountry == con.MailingCountry){
				con.phone = accMap.get(ccc.Id);
				lstCon.add(con);
				}
			}
		}
		if(lstCon.size()>0){
		update lstCon;
		}
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
SFDC12SFDC12
Hi Ashwini,

Trybelow trigger,

trigger updatephonebasedonaddress1 on Account (after update) {
List<contact>conlist=new List<contact>();
set<Id>accid=new set<id>();
    for(Account a:trigger.new){
        accid.add(a.id);
    }
    List<Account>acclist=[select id,name,phone,BillingCountry,(select id,Phone,LastName,MailingCountry from contacts)from Account where id=:accid];
    for(Account a:acclist){
        for(contact c:a.contacts){
            if(c.MailingCountry==a.BillingCountry){
                c.phone=a.phone;
                conlist.add(c);
            }
        }
        update conlist;
    }
}