function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mng0827mng0827 

Trigger to update child record of all account contacts

Hi,

Is it possible to update a child record of all contacts when one of the contact child record is updated? We have a custom object associated to contacts and would like the same record to reflect on all contacts within the same account. For example, Account A has 2 contacts, Contact 1 and Contact 2. Each contact has a child record, namely Child Record 1.a and Child Record 2.a. When Child Record 1.a is updated, Child Record 2.a should also update. Both child records should have the same info.

Is this possible? How do I do this?

Thanks!
AshlekhAshlekh
Hi,

You can do this by write a trigger on  Child Record object.
Arpit Patel 8Arpit Patel 8
Yes. It is doable.


When the Child Record is updated, you can get the related Parent Contact record from the Child Record 1.a (via relationship).

Once you get the Parent Contact, then you can all other Child record for the given parent, and update them.

Just some processing in the loop using list and maps. It should be very simple.

You can try to write the code for this so that I can further review. 

Hope this helps.
Reinke, ChrisReinke, Chris
Could you create a field on the Account record and then use a formula field to pull the data from that field to the sub-child records? You would update the field on Account and then all of the sub-children would be updated without using any code.

Chris.
mng0827mng0827
Thanks guys. I tried writing the trigger but get the following error:

Invalid field Account for SObject PFC_Dependent__c at line 23 column 46

Here's my code:

trigger updatePFCobjects on PFC_Dependent__c (before update) {
    Map<Id, PFC_Dependent__c> updateddependent = new Map<Id,PFC_Dependent__c>();
    for (integer i = 0; i<Trigger.new.size();i++){
        if((Trigger.old[i].Age__c != Trigger.new[i].Age__c)
        ||(Trigger.old[i].Contact__c != Trigger.new[i].Contact__c)
        ||(Trigger.old[i].First_Name__c != Trigger.new[i].First_Name__c)
        ||(Trigger.old[i].Has_Special_Needs__c != Trigger.new[i].Has_Special_Needs__c)
        ||(Trigger.old[i].Highest_Level_of_Education__c != Trigger.new[i].Highest_Level_of_Education__c)
        ||(Trigger.old[i].Last_Name__c != Trigger.new[i].Last_Name__c)) {
        updateddependent.put(Trigger.old[i].id,
                            Trigger.new[i]);
        }
   }
   List<Id> ContactIds = new List <Id>();
       for (integer i=0; i <Trigger.new.size(); i++) {
           {
               ContactIds.add(Trigger.new[i].Contact__c);
           }
   }
   List<Id> AccountIds = new List <Id>();
       for (integer i=0; i <Trigger.new.size(); i++) {
           {
               AccountIds.add(Trigger.new[i].Account);
           }
   List<Contact> parentObjList = new List<Contact>();  
       for (Contact c : [SELECT id, accountId  
                     FROM contact 
                     WHERE Id  
                     in :updateddependent.keySet()]) { 
       PFC_Dependent__c pfcd = Trigger.new[0];  
           pfcd.Age__c = pfcd.Age__c;
           pfcd.Contact__c = pfcd.Contact__c;
           pfcd.First_Name__c = pfcd.First_Name__c;
           pfcd.Has_Special_Needs__c = pfcd.Has_Special_Needs__c;
           pfcd.Highest_Level_of_Education__c = pfcd.Highest_Level_of_Education__c;
           pfcd.Last_Name__c = pfcd.Last_Name__c;
   updateddependent.add(c);
}  
   update updateddependent;     
}
}