Tuesday, August 1, 2017

Auto populate a child object field from parent object field when you update the field


Requirement : Change the All child Contact records Field (Title) when we update the Account (industry) Field.



trigger updatechaildrecords on Account (After update) {
    Set<Id> wanttoupdateIDs = Trigger.oldmap.Keyset();
    
    List<Account> Acc = [SELECT Id, Name,industry, (select ID, AccountID,Title from Contacts) FROM Account WHERE Id in :wanttoupdateIDs];
    system.debug('********'+Acc);
    List<Contact> childrenToUpdate = new List<Contact>();
    for(Account AA: Acc){    
      for(Contact Con: AA.Contacts) 
     {      
       con.Title = AA.industry;
                       
       childrenToUpdate.add(Con);
     } 
    }
     update childrenToUpdate;
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.