Trigger : Find all Account related Contacts when an Account is Updated.

Scenario: Write trigger syntax to find all Account related Contacts when an Account is Updated.

AccountTrigger
trigger AccountTrigger on Account (after update) {
    AccountTriggerHandler handler = new AccountTriggerHandler();
    if(trigger.isUpdate && trigger.isafter) { 
        handler.findAccountRelatedContacts(trigger.new);
    }
}
AccountTriggerHandler
public class AccountTriggerHandler {
    // Scenario 1: Write trigger syntax to find all Account related Contacts when an Account is Updated.
    public void findAccountRelatedContacts(List<Account> lstAccounts) { 
    	// Note This will work for bulk record as we are using in operator not =:lstAccounts
    	List<Contact> contacts = [SELECT Id From Contact Where AccountId in: lstAccounts];
        System.debug('Account Related Contacts '+ contacts);
    }
}

Leave a Reply