Friday, 9 October 2015

update the Field values from parent to child Trigger

Where Account name, phone is update  related contact Lastname and phone also update.

trigger ContactUpdate on Account (after update)
 {
   set<Id> acctIds = new set<Id>();
    map<Id, Account> mapAccount = new map<Id, Account>();
    list<Contact> listContact = new list<Contact>();
    for(Account acct : trigger.new) 
    {
        acctIds.add(acct.Id);
        mapAccount.put(acct.Id, acct);
    }
    listContact = [SELECT LastName ,Phone , AccountId FROM Contact WHERE AccountId IN : acctIds];
    
   if(listContact.size() > 0) 
   {
        for(Contact con : listContact)
        {
            con.LastName= mapAccount.get(con.AccountId).Name;
            con.phone= mapAccount.get(con.AccountId).phone;
        }
        update listContact;
    }

}

No comments:

Post a Comment