Search Here

If the Account phone is updated then populate the phone number on alL related Contacts (Home Phone field). [Using Parent-Child SOQL] trigger in salesforce

 Here's how you can write a trigger in Salesforce to update all related Contacts' HomePhone field whenever the Phone field on an Account is updated. The solution uses Parent-Child SOQL and adheres to best practices.

trigger UpdateContactsOnAccountPhoneChange on Account (after update) {

    // Set to hold account IDs where the Phone has been updated

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

        // Collect the IDs of Accounts where Phone has been updated

    for (Account acc : Trigger.new) {

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

        if (acc.Phone != oldAcc.Phone) {

            updatedAccountIds.add(acc.Id);

        }

    }

    // If no relevant accounts, exit early

    if (updatedAccountIds.isEmpty()) {

        return;

    }

    // Query Contacts related to updated Accounts

    List<Contact> contactsToUpdate = [

        SELECT Id, HomePhone

        FROM Contact

        WHERE AccountId IN :updatedAccountIds

    ];

    // Prepare the Contacts for update

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

    for (Contact con : contactsToUpdate) {

        Account updatedAcc = Trigger.newMap.get(con.AccountId);

        con.HomePhone = updatedAcc.Phone; // Set HomePhone to Account Phone

        contactsForUpdate.add(con);

    }

    // Perform the update on related Contacts

    if (!contactsForUpdate.isEmpty()) {

        update contactsForUpdate;

    }

}

Key Points of the Implementation:

  1. Trigger Context:

    • The trigger runs on the Account object in the after update context.
    • The after context is used because we are updating child records (Contact) based on the changes in the parent record (Account).
  2. Change Detection:

    • Compare Trigger.new and Trigger.old to detect changes in the Phone field of Account.
  3. Parent-Child SOQL:

    • Query Contact records where the AccountId matches the IDs of Accounts with updated Phone numbers.
  4. Bulkified Code:

    • The code handles bulk updates efficiently by processing multiple Accounts and their related Contacts in batches.
  5. Avoid Recursive Triggers:

    • Ensure there is no logic in the Contact trigger that could lead to recursion when these updates are performed.
@isTest
public class UpdateContactsOnAccountPhoneChangeTest {
    @isTest
    static void testUpdateContactsOnAccountPhoneChange() {
        // Create an Account and related Contacts
        Account acc = new Account(Name = 'Test Account', Phone = '1234567890');
        insert acc;

        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 Phone
        acc.Phone = '9876543210';
        update acc;

        // Verify that the Contacts' HomePhone fields have been updated
        List<Contact> updatedContacts = [SELECT HomePhone FROM Contact WHERE AccountId = :acc.Id];
        for (Contact con : updatedContacts) {
            System.assertEquals('9876543210', con.HomePhone);
        }
    }
}




Tags

Post a Comment

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