Search Here

If the Account billing address is updated then update related contacts mailing address. [Using Map] trigger in salesforce

 Here is a Salesforce trigger implementation that updates related Contacts' MailingAddress whenever the BillingAddress of an Account is updated, using a Map to handle data efficiently.

trigger UpdateContactsOnBillingAddressChange on Account (after update) {

    // Map to store updated Account IDs and their Billing Address

    Map<Id, Account> accountsWithUpdatedAddress = new Map<Id, Account>();

    // Identify Accounts where Billing Address has changed

    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) {

            accountsWithUpdatedAddress.put(acc.Id, acc);

        }

    }

    // Exit if no Billing Address has been updated

    if (accountsWithUpdatedAddress.isEmpty()) {

        return;

    }

    // Query related Contacts for these Accounts

    List<Contact> contactsToUpdate = [

        SELECT Id, MailingStreet, MailingCity, MailingState, MailingPostalCode, MailingCountry, AccountId

        FROM Contact

        WHERE AccountId IN :accountsWithUpdatedAddress.keySet()

    ];

    // Update the Mailing Address on Contacts

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

    for (Contact con : contactsToUpdate) {

        Account acc = accountsWithUpdatedAddress.get(con.AccountId);

        // Update Mailing Address fields

        con.MailingStreet = acc.BillingStreet;

        con.MailingCity = acc.BillingCity;

        con.MailingState = acc.BillingState;

        con.MailingPostalCode = acc.BillingPostalCode;

        con.MailingCountry = acc.BillingCountry;

        contactsForUpdate.add(con);

    }

    // Perform the update operation

    if (!contactsForUpdate.isEmpty()) {

        update contactsForUpdate;

    }

}

Key Features of the Trigger:

  1. Efficient Map Usage:

    • A Map<Id, Account> stores Accounts with updated BillingAddress for quick reference during Contact updates.
  2. Field Change Detection:

    • Compares BillingStreet, BillingCity, BillingState, BillingPostalCode, and BillingCountry fields between Trigger.new and Trigger.old.
  3. Bulk Handling:

    • Processes multiple Accounts and their Contacts in bulk to ensure scalability.
  4. Exit Early:

    • If no relevant Accounts are identified, the trigger exits to avoid unnecessary operations.
  5. SOQL Optimization:

    • Queries only the necessary Contacts related to Accounts with updated Billing Addresses.
@isTest
public class UpdateContactsOnBillingAddressChangeTest {
    @isTest
    static void testUpdateContactsOnBillingAddressChange() {
        // 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 that 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.