Update Contacts’ Mailing Address via SOQL

Keeping addresses in sync across related Salesforce records is more than just good data practice—it’s a way to avoid communication errors, streamline operations, and maintain a single source of truth. In many organizations, Contacts share the same mailing address as their parent Account’s billing address, especially in B2B settings.

In this blog, we demonstrate a smart and scalable solution using Parent-to-Child SOQL queries in Apex. This trigger automatically updates the Mailing Address fields on all related Contacts whenever the Billing Address of an Account changes. It runs in the after update context and uses a single query per parent to efficiently fetch and update child records.

This automation ensures your records stay clean, synchronized, and reliable—with zero manual effort from the user.

🧠 Why Address Synchronization Matters


When an Account’s Billing Address is updated but the related Contacts retain the older address, it leads to:

  • Communication sent to outdated locations

  • Confusion among teams relying on Contact-level data

  • Integration errors with external tools

  • Inaccurate reports and exports

By automatically syncing Billing changes from Account to Contact Mailing Address, this trigger:

  • Keeps data accurate and up to date

  • Removes the burden of manual updates

  • Improves the reliability of reports and campaigns

  • Ensures consistency across teams and tools

It’s a simple enhancement that solves a very real problem in every CRM.

🔍 What This Blog Covers


  • How to use Parent-to-Child SOQL to query related Contacts in a single query

  • How to detect Billing Address field changes using Trigger.oldMap

  • How to update multiple fields on child records in one go

  • How to write bulk-safe, clean Apex logic for real-time automation

  • The advantages of using subqueries over nested loops

  • How to wrap logic in a handler class for clarity and testability

This is a textbook example of efficient, scalable field-level synchronization using Apex triggers and Salesforce relationship queries.

🎯 Real-World Use Cases for This Trigger


  • Sales and support teams who contact clients using Mailing Address data from Contact records

  • Marketing teams generating campaign lists and exports based on Contact-level address fields

  • Ops and logistics teams using Contact records in print/delivery processes

  • Admin teams seeking to eliminate inconsistencies and improve data quality

  • Data integrations where synced address fields are a critical dependency

This solution ensures that every change made to the Account’s Billing Address is reflected in real time across all associated Contacts—no extra clicks, no outdated data.

🎥 Learn It Visually – YouTube Playlist Included


Prefer to see this in action? The full implementation of this trigger is available in the Salesforce Makes Sense YouTube playlist. Watch how:

  • Parent-to-Child SOQL works in Apex

  • The trigger handler keeps logic clean

  • Real-time updates are pushed to child records seamlessly

The playlist is tailored for Salesforce developers, admins, and learners who want hands-on experience with real-world Apex solutions.

Solution:

trigger AccountTrigger on Account (after update) {
         if(Trigger.isUpdate){ if(Trigger.isAfter){
                                   AccountTriggerHandler.updateRelatedConts(Trigger.New, Trigger.oldMap);
                         }
          }
}
public class AccountTriggerHandler {

          public static void updateRelatedContactMailWithoutMap(List<Account> accList,Map<Id,Account>
oldMap){
                List conList=new List(); Set idSet= new Set(); for(Account
                 acc:accList){ if((!acc.BillingCity.equals(oldMap.get(acc.Id).BillingCity) ||
                 !acc.BillingCountry.equals(oldMap.get(acc.Id).BillingCountry) ||
                 !acc.BillingPostalCode.equals(oldMap.get(acc.Id).BillingPostalCode)
|| !acc.BillingState.equals(oldMap.get(acc.Id).BillingState) ||
!acc.BillingStreet.equals(oldMap.get(acc.Id).BillingStreet) ) && oldMap!=null){ idSet.add(acc.Id);
                             }
                    }
                     for(Account acc:[SELECT Id, BillingCountry, BillingCity, BillingState,
        BillingPostalCode, BillingStreet, (SELECT Id FROM Contacts) FROM
         Account WHERE Id IN:idSet]) {
                                        if(acc.Contacts!=null){ for(Contact cont:acc.Contacts){
                                        cont.MailingCountry=acc.BillingCountry; cont.MailingCity= acc.BillingCity;
                                        cont.MailingState=acc.BillingState;
                                        cont.MailingPostalCode=acc.BillingPostalCode;
                                        cont.MailingStreet=acc.BillingStreet;
                                        conList.add(cont);
                                        }
                              }
                    }
                     if(!conList.isEmpty()){ update conList;
                    }
         }
}

Want to Apply As Content Writer?

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart

Let's get you started!

Interested in writing Salesforce Content?

Fill in this form and we will get in touch with you :)