+ Start a Discussion
MasieMasie 

Update a child object based on update on parent object

Hi

May someone please help, I have an object called the rebate that has a lookup relationship to the account object. I want when a field is updated on the account object a field should also be updated on the rebate object but I am having a problem with this trigger.

trigger updateStartDate on Rebates__c (after insert, after update,before update){

Set<Id> accountIds = new Set<Id>();
   
    for(Rebates__c reb :trigger.new){
        accountIds.add(reb.Company__c);
}

    Map <ID,Account> updateAccounts = new Map<ID,Account>([Select id, Date_Installed__c from Account where id IN: AccountIds]);{
                                                 
for(Rebates__c reb :trigger.new){
accountIds.add(reb.Company__c);

       if(reb.From_Install_Date__c == true && updateAccounts.get(reb.Company__c).Date_Installed__c != null){

    if(reb.Product__c == '1t200000024Dtm')

{
                reb.Start_Date__c = reb.First_Day_of_the_Month__c;
   
    accountIds.update(reb);

}

else
{

   reb.Start_Date__c = reb.Install_Date__c;
    accountIds.update(reb);

       }
           try
              
           {
               update accountIds;
           }
           catch (system.DmlException e)
           {
               system.debug (e);
           }

   }
}
}
}
Best Answer chosen by Masie
ch ranjithch ranjith
trigger rt on Account (after insert,after update)
{
set<id> set1=new set<id>();
  list<contact> conlistnew=new list<contact>();
  for(account acc:trigger.new)
  {
    set1.add(acc.id);
  } 
  list<contact> conlist=[select id,name,accountid from contact where accountid in:set1];
 
  for(account tempacc:trigger.new)
  {
  for(contact tempcon:conlist)
  {
   if(tempacc.id==tempcon.accountid)
   {
   tempcon.update_from_account__c=tempacc.contact_update_fiels__c;
     conlistnew.add(tempcon);
   }
  }
  }
  update conlistnew;
}

All Answers

Ankit AroraAnkit Arora
Correct me if am wrong, you have a account lookup on rebate and now whenever account field is updated then you want to update rebate field?

If this is a scenario, then why not write a trigger on account. And when updated, you can identify that this account is associated with which rebate using SOQL and then you can update the rebate too.
MasieMasie
Hi Ankit

I will try that- thanks.
ch ranjithch ranjith
trigger rt on Account (after insert,after update)
{
set<id> set1=new set<id>();
  list<contact> conlistnew=new list<contact>();
  for(account acc:trigger.new)
  {
    set1.add(acc.id);
  } 
  list<contact> conlist=[select id,name,accountid from contact where accountid in:set1];
 
  for(account tempacc:trigger.new)
  {
  for(contact tempcon:conlist)
  {
   if(tempacc.id==tempcon.accountid)
   {
   tempcon.update_from_account__c=tempacc.contact_update_fiels__c;
     conlistnew.add(tempcon);
   }
  }
  }
  update conlistnew;
}
This was selected as the best answer
MasieMasie
Thanks Ch Ranjith

Your trigger helped me model mine. You are a life saver
Harish NallaHarish Nalla
trigger TestAccountTrigger on Account (before update) {

  Map<id, Account> accountMap = new Map<Id,account>
  ([select id,name,Phone,(select id,Phone from contacts) from account where id in :trigger.newmap.keyset()]);
   list<contact> conupdatelist = new list<contact>();
    for (account acc: trigger.new){
         list<contact> conlist = accountMap.get(acc.id).contacts;
         for(contact con : conlist )
         {
            con.phone = acc.phone;
            conupdatelist.add(con );
         }
    }
    update conupdatelist;
 }