Trigger to Update Contacts on Account Update

trigger AccountTrigger on Account (before update) {
   Map updateAccMap = new Map();
    List updatedContacts = new List();
   for(Integer i=0; i< trigger.new.size(); i++) {
       if(trigger.old[i].ShippingState != trigger.new[i].ShippingState) {
           updateAccMap.put(Trigger.old[i].id, Trigger.new[i]);
       }
   }
   //for(Contact c : [Select Id, AccountId, Account_Updated__c From Contact where AccountId IN :updateAccMap.keySet()]) {
   for(Contact c : [Select Id, AccountId, Account_Updated__c From Contact where AccountId IN :trigger.old]) {
       Account parentAcc = updateAccMap.get(c.AccountId);
       c.Account_Updated__c = true;
          updatedContacts.add(c);
   }
   if(!updatedContacts.isEmpty())
	update updatedContacts;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s