+ Start a Discussion
Prasanth RPrasanth R 

if phone number update in account,,it should pop up contact phone number using map

i'm beginner in trigger.i tried below code but stucked in midway..kindly someone help

trigger practiceacc on Account (after update){
    List<contact> ctlt = new List<contact>();
    set<Id> st = new set<Id>();
    for(Account acc:Trigger.new){
        st.add(acc.id);       
    }
Map<Id,Account> mapacc = new new Map<Id,Account>([Select Id,Name,Phone from contact where AccountId in:st]);
    for(Account obj1:Trigger.new){
      if(mapacc.get(obj1.Accountid).obj1=Phone)  
    }     
    update ctlt;
}
CharuDuttCharuDutt
HIi Prasanth
Try Below Trigger
trigger practiceacc on Account (after update){
    set<Id> st = new set<Id>();
    Map<String, String> MapAcc= new Map<String, String>();
    for(Account acc:Trigger.new){
        if(acc.phone != Trigger.oldMap.get(acc.id).phone){
        st.add(acc.id);
        mapAcc.put(acc.Id,Acc.phone);
        }       
    }
   list<contact> lstcon =[select id,phone,AccountId from contact Where AccountId IN : st ];
   for(contact con : lstcon ){
   if(MapAcc.containsKey(Con.AccountId )){
     con.phone = MapAcc.get(Con.AccountId);
    }
   }
update lstcon ;

}
Please Mar It As Best Answer If It Helps
Thank You!
Prasanth RPrasanth R
@char dutt..i'm not able to understand this could you explain
AbhinavAbhinav (Salesforce Developers) 
Explaing above @Charu's  answer.
 
if(acc.phone != Trigger.oldMap.get(acc.id).phone)

 with this it checking whether phone no value is changed or not .
Map<String, String> MapAcc= new Map<String, String>();

This map will hold Key as Accound Id and Value as Account Phone

list<contact> lstcon =[select id,phone,AccountId from contact Where AccountId IN : st ] 

This quey will give you all related contact assosciated with account on which update are being done.

if(MapAcc.containsKey(Con.AccountId ).----> now after iterating contact list with this condition we are checking whether that Contact Id is present in map which is created.

if yes then updating the value of contact phone

MapAcc.get(Con.AccountId)---->. in this line our key is Con.AccountId and with get function we are getting the value which was stored earlier in map i.e phone.

Hope able to explain.

Thanks!