+ Start a Discussion
Dream_weaverDream_weaver 

Update addresses of all Contacts associated with an Account whenever Account’s address changes..

I tried the following way but only one contact is getting updated.....

 
   if(trigger.isUpdate)
   {
      list<id> ids=new list<id>();
      for(account ac:trigger.new)
      {
        ids.add(ac.id);
      }
      list<contact> con=new list<contact>();
      list<contact> updtcont=new list<contact>();
      map<id,contact> map1=new map<id,contact>();
      con=[select accountid,MailingStreet,MailingCity,MailingState,MailingCountry from contact where accountid in:ids ];
      
      for(contact cont:con)
      {
         map1.put(cont.accountid,cont);
      }
     for(account ac:trigger.new)
      {
             if(map1.containsKey(ac.id))
       {
                 map1.get(ac.id). MailingStreet=ac.BillingStreet;
                 updtcont.add(map1.get(ac.id));
       }
      }
      try
      {
        if(!updtcont.isEmpty())
       {
       
        update updtcont;
       
        }
      }
     
      catch(Exception e)
   {
      Error_Reporting__c err= new Error_Reporting__c();
      err.Reason__c=e.getMessage();
      err.Record_Id__c=updtcont.get(0).id;
       insert(err);
   
   }

Best Answer chosen by Admin (Salesforce Developers) 
ManjunathManjunath

Hi,

 

You are using the following statement in your code which will give you only on contact.

con=[select accountid,MailingStreet,MailingCity,MailingState,M

ailingCountry from contact where accountid in:ids ];

 

Above should be done for all the id not just single id present in trigger.new list.

 

Regards

Manjunath

 

 

All Answers

ManjunathManjunath

Hi,

 

You are using the following statement in your code which will give you only on contact.

con=[select accountid,MailingStreet,MailingCity,MailingState,M

ailingCountry from contact where accountid in:ids ];

 

Above should be done for all the id not just single id present in trigger.new list.

 

Regards

Manjunath

 

 

This was selected as the best answer
Dream_weaverDream_weaver

Thanks..I reworked on the code...got the final correct one..

 

 

if(trigger.isUpdate)
   {
      integer count=0;
      list<id> ids=new list<id>();
       map<id,account> map1=new map<id,account>();
      for(account ac:trigger.new)
      {
        if((ac.BillingStreet != Trigger.oldMap.get(ac.Id).BillingStreet)||
           (ac.BillingCity != Trigger.oldMap.get(ac.Id).BillingCity))        
        {
        ids.add(ac.id);
        map1.put(ac.id,ac);
        }
      }
      list<contact> con=new list<contact>();
      list<contact> updtcont=new list<contact>();
     
      con=[select name,accountid,MailingStreet,MailingCity,MailingState,MailingCountry from contact where accountid in:ids ];
      
      for(contact cont:con)
      {
         if(map1.containsKey(cont.accountid))
        {
          cont.MailingStreet=map1.get(cont.accountid).BillingStreet;
          updtcont.add(cont);
        }
      }
     try
      {
        if(!updtcont.isEmpty())
        {
            update updtcont;
        }
      }
     
      catch(Exception e)
   {
      Error_Reporting__c err= new Error_Reporting__c();
      err.Reason__c=e.getMessage();
      err.Record_Id__c=updtcont.get(0).id;
       insert(err);
   
   }
      
      
   }