Trigger Challenge #3: Write A Trigger whenever an account name is modified send an Email notification to the contact of an Account.

Write A Trigger whenever an account name is modified send an Email notification to the contact of an Account.

0
2786
Trigger Challenge #3 Account trigger with Email notification
Trigger Challenge #3 Account trigger with Email notification

Hello Salesforce Cody’s, Welcome back after a long time. Let’s do a quick Beginner/Intermediate level trigger.

trigger AccountTrigger on Account (after update) {
    List<Id> listOfAccountIds = new List<Id>();
    for(Account eachAccount : Trigger.New){
	//only name modified then send email to related contact
        if(eachAccount.Name != Trigger.oldmap.get(eachAccount.Id).Name){
            listOfAccountIds.add(eachAccount.Id);
        }
    }

    for(Contact c : [select Email from Contact where AccountId IN :listOfAccountIds]){
        EmailManager.sendMail(c.Email, 'subject', 'body');
    }
}

Let me know in the comments if any doubts?