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
prathap cprathap c 

In case of Master-Detail relationship, on Update of master record can we update the field of child record using workflow rule?

can any one explain about this .

In case of Master-Detail relationship, on Update of master record can we update the field of child record using workflow rule?
James LoghryJames Loghry
Not currently, no, but keep your eye out for Process Builder in Spring '15.  Another declarative tool you could use is simply updating your fields to be formula fields instead, reading data directly off the parent record.
MithunPMithunP
Hi Prathap,

It's not possible to update child records in a master detail relationship using workflow, but you can update using a Trigger. Create a trigger on Master object and update child records when ever DML happens.

Below is the sample Trigger code.
 
trigger ContactUpdate on Account (after update) {
    set<Id> acctIds = new set<Id>();
    map<Id, Account> mapAccount = new map<Id, Account>();
    list<Contact> listContact = new list<Contact>();
    
    for(Account acct : trigger.new) {
        acctIds.add(acct.Id);
        mapAccount.put(acct.Id, acct);
    }
    
    listContact = [SELECT MailingStreet, MailingCity, AccountId FROM Contact WHERE AccountId IN : acctIds];
    
    if(listContact.size() > 0) {
        for(Contact con : listContact) {
            con.MailingStreet = mapAccount.get(con.AccountId).BillingStreet;
            con.MailingCity = mapAccount.get(con.AccountId).BillingCity;
        }
        update listContact;
    }
}



Best Regards,
Mithun.
Naveen BoyaNaveen Boya
Hi Mithunp,

Could you please explain this code in detail? Please Mithunp
Sourajit MohantySourajit Mohanty
Hi Naveen,

Below is the explanation of Mithunp's code..

//Updating the Mailing Street,Mailing City fields of related Contacts to match with the Billing Street,Billing City       fields of parent Account everytime the parent Account record is updated

//trigger on Account object which fires every time Account object record gets updated 
trigger ContactUpdate on Account (after update)                     
{
          // Set to store unique Account IDs which fire the trigger
         set<Id> acctIds = new set<Id>();               

         //Map of IDs to Account for all the Account records which fire the trigger
         map<Id, Account> mapAccount = new map<Id, Account>();   

         list<Contact> listContact = new list<Contact>();

        //Loops through all Account records which fired the trigger...
        //  Best practice- Bulkification of code
         for(Account acct : trigger.new)                
         {
                 //stores the IDs of all Account records which fired the trigger in the set
                 acctIds.add(acct.Id); 
                      
                 // stored IDs and its assosciated Account records which fired the trigger in the map
                 mapAccount.put(acct.Id, acct);      
         }

        //Fetches the child Contact records of all those Account records which fired the 
        // trigger (getting the same from the set acctIds by matching for the AccountId)
        listContact = [SELECT MailingStreet, MailingCity, AccountId FROM Contact                                                               
                              WHERE AccountId IN : acctIds];                                                                                                                                                                                                                      
  
         //executes only when child Contact records are present for the Accounts
        if(listContact.size() > 0)                        
        {
              for(Contact con : listContact)          //looping through the list of related Contacts
              {
                      //setting the Mailing Street field of Contact to match with that of the parent 
                      // Account's Billing Street
                      con.MailingStreet = mapAccount.get(con.AccountId).BillingStreet;                                                                                                                                                    
                      
                     //setting the Mailing City field of Contact to match with that of the parent 
                    //Account's Billing City
                      con.MailingCity = mapAccount.get(con.AccountId).BillingCity;                                                                                                                                                                       
                }
             update listContact;                   //updates the new list of Contacts in the database
         }
}