Search Here

If the Account billing address is updated then update related contacts mailing address. [Using Parent-Child SOQL] trigger in salesforce

 Here's how you can write a Salesforce trigger to update related Contacts' MailingAddress fields when the BillingAddress of an Account is updated. This implementation uses Parent-Child SOQL.

trigger UpdateContactsOnAccountBillingAddressChange on Account (after update) {

    // Collect Account IDs where the Billing Address has changed

    Set<Id> updatedAccountIds = new Set<Id>();

    for (Account acc : Trigger.new) {

        Account oldAcc = Trigger.oldMap.get(acc.Id);

        if (acc.BillingStreet != oldAcc.BillingStreet ||

            acc.BillingCity != oldAcc.BillingCity ||

            acc.BillingState != oldAcc.BillingState ||

            acc.BillingPostalCode != oldAcc.BillingPostalCode ||

            acc.BillingCountry != oldAcc.BillingCountry) {

            updatedAccountIds.add(acc.Id);

        }

    }

    // Exit if no Billing Address changes detected

    if (updatedAccountIds.isEmpty()) {

        return;

    }

    // Query Accounts with their related Contacts

    List<Account> accountsWithContacts = [

        SELECT Id, BillingStreet, BillingCity, BillingState, BillingPostalCode, BillingCountry,

               (SELECT Id, MailingStreet, MailingCity, MailingState, MailingPostalCode, MailingCountry FROM Contacts)

        FROM Account

        WHERE Id IN :updatedAccountIds

    ];

    // Prepare Contacts for update

    List<Contact> contactsToUpdate = new List<Contact>();

    for (Account acc : accountsWithContacts) {

        for (Contact con : acc.Contacts) {

            // Update Mailing Address on Contact

            con.MailingStreet = acc.BillingStreet;

            con.MailingCity = acc.BillingCity;

            con.MailingState = acc.BillingState;

            con.MailingPostalCode = acc.BillingPostalCode;

            con.MailingCountry = acc.BillingCountry;

            contactsToUpdate.add(con);

        }

    }

    // Update Contacts in bulk

    if (!contactsToUpdate.isEmpty()) {

        update contactsToUpdate;

    }

}

Key Features:

  1. Parent-Child SOQL:

    • Queries Account records with their related Contact records using a subquery.
  2. Change Detection:

    • Compares the old and new BillingAddress fields of Account records to detect changes.
  3. Bulk Handling:

    • Efficiently handles multiple Accounts and their related Contacts in a single transaction.
  4. Exit Early:

    • Avoids unnecessary operations when no relevant changes are detected.
  5. Minimal SOQL/DML:

    • Uses one SOQL query to fetch all necessary data and performs a single DML update operation.
@isTest
public class UpdateContactsOnAccountBillingAddressChangeTest {
    @isTest
    static void testBillingAddressUpdate() {
        // Create an Account with initial Billing Address
        Account acc = new Account(
            Name = 'Test Account',
            BillingStreet = '123 Main St',
            BillingCity = 'San Francisco',
            BillingState = 'CA',
            BillingPostalCode = '94105',
            BillingCountry = 'USA'
        );
        insert acc;

        // Create related Contacts
        Contact con1 = new Contact(FirstName = 'John', LastName = 'Doe', AccountId = acc.Id);
        Contact con2 = new Contact(FirstName = 'Jane', LastName = 'Smith', AccountId = acc.Id);
        insert new List<Contact>{ con1, con2 };

        // Update the Account Billing Address
        acc.BillingStreet = '456 Elm St';
        acc.BillingCity = 'Los Angeles';
        acc.BillingState = 'CA';
        acc.BillingPostalCode = '90001';
        acc.BillingCountry = 'USA';
        update acc;

        // Verify Contacts' Mailing Addresses have been updated
        List<Contact> updatedContacts = [SELECT MailingStreet, MailingCity, MailingState, MailingPostalCode, MailingCountry
                                          FROM Contact WHERE AccountId = :acc.Id];
        for (Contact con : updatedContacts) {
            System.assertEquals('456 Elm St', con.MailingStreet);
            System.assertEquals('Los Angeles', con.MailingCity);
            System.assertEquals('CA', con.MailingState);
            System.assertEquals('90001', con.MailingPostalCode);
            System.assertEquals('USA', con.MailingCountry);
        }
    }
}



Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.