+ Start a Discussion
Arundhati DebArundhati Deb 

write a trigger to update one account depending on another account

We have to a update child account depending on parent account. We have the same record type for both the accounts. When a checkbox called "entity" is checked, and a look up field called parent account is filled, that account is considered a child account. Whenever a new address is added or edited on the parent account, the same should flow on child account in the same field of address.
We have to write a trigger for the same
Rameshwar gaurRameshwar gaur
if (Trigger.isUpdate)
        {
        List<Account> Allacc = [SELECT ID,ParentId,BillingAddress,BillingCity,BillingCountry,
                                                          BillingPostalCode,BillingState,BillingStreet,ShippingAddress,
                                                          ShippingCity,ShippingCountry,ShippingPostalCode,ShippingState,
                                                          ShippingStreet FROM Account];
        Map<Id,Account> MapAccount = New Map<ID,Account>();
       	for(Account acc : Allacc)
        {
            MapAccount.put(acc.ParentId,acc);
        }
        List<Account> AccountUpdate = new List<Account>();
        for(Account Acc : Trigger.New)
        {
            if(MapAccount.get(acc.id).entity_c == true){
            if(acc.ShippingAddress != Trigger.OldMap.get(acc.id).ShippingAddress || acc.BillingAddress != Trigger.OldMap.get(acc.id).BillingAddress)
            {
                   MapAccount.get(acc.id).BillingCity = acc.BillingCity;
                   MapAccount.get(acc.id).BillingCountry = acc.BillingCountry;
                   MapAccount.get(acc.id).BillingPostalCode = acc.BillingPostalCode;
                   MapAccount.get(acc.id).BillingState = acc.BillingState;
                   MapAccount.get(acc.id).BillingStreet = acc.BillingStreet;
                   MapAccount.get(acc.id).ShippingCity = acc.ShippingCity;
                   MapAccount.get(acc.id).ShippingCountry = acc.ShippingCountry;
                   MapAccount.get(acc.id).ShippingPostalCode = acc.ShippingPostalCode;
                   MapAccount.get(acc.id).ShippingState = acc.ShippingState;
                   MapAccount.get(acc.id).ShippingStreet = acc.ShippingStreet;
                   AccountUpdate.add(MapAccount.get(acc.id)); 
                
            }
         }
        } 
        Update AccountUpdate;
        }

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
If you have any more queries or those code not work please feel free to ask i will try my best to help you.