 Hello everyone. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this module, I'm going to demonstrate how you can send email notification through Apex Trigger. Let's understand the scenario that I'm going to implement. So upon creation of contact record, a welcome email notification should be triggered to the email ID populated on contact record through Apex Trigger. So this is the use case that I'm going to implement with the help of Apex Trigger. So jumping to developer console. Here you can see I have created a contact trigger on contact object and it will run on after insert context. So from here, I'm going to call the method that will be defined in a contact trigger handler. So I'm creating Apex class contact trigger handler. And here I'm going to define a method. So I need to call this method in the trigger. So here I'm writing contact. Trigger handler dot then this method and from here I'm going to pass trigger dot new. So whenever contact record will be inserted. So those newly created records will be available in this trigger dot new context variable. Now here if you want to apply these conditions like trigger dot is insert and trigger dot is after. So you can apply it like this. And here I need to store that trigger dot new into a list of context. So I'm creating con list. So I'm saving this contact trigger handler so that I can save the trigger. So this way through trigger you will be having all the newly created records in con list. Now here I need to iterate on this con list. So before that, basically, I need to create list of messaging dot email type. As you see, like this trigger I'm implementing for bulk insert handling. So all the records that we will be inserting will be available in this contact list. So there can be multiple contacts. So if you want to send email notifications so you need to store them into this list. So one by one, we will be creating one instance of this email, like for that we will be using messaging dot single email message. So one by one, we will be creating that record and that record will be added into this email list later on. Now I'm going to iterate loop on con list. So one by one contacts will be available into this con variable. So now here I'm applying if condition if con dot email is not equals to none. So it means if contact record is having any email populated then only this functionality should work. So here I'm writing messaging dot single email message then email equals to email message equals to new and I'm copying this and paste it. So this way instance of the single email message is created. Now we need to set the parameters. So I'm just creating list of string. It will store to address. So here I'm providing con dot email. So that particular contacts email address will be stored into this to address. Now, email message dot set to address and here I need to pass this parameter. So basically in this two address we can provide multiple addresses. That's why we need to pass the list of string here. So two addresses set now I'm creating string will subject. So here I'm writing welcome then plus con dot first name. Right. So this way you can concatenate value. So this will be a plain text and this is the merge field. So both will be combined. So con dot first name will be combined with this welcome and it will be available into email subject. Now after this email message dot set subject and here I need to pass this parameter. So this way subject is also set and I'm setting display name. So I'm populating welcome notification in this this name variable then email. Message dot set sender display name. Okay, so here if you want to set any other name so you can do that as well. Let's say here I'm writing my name and this display name will be passed into this parameter. So this way we set the address we set the subject we set the sender display name. Now it's time to create the body of the email. So I'm creating one string variable named as content. And here I'm writing high then concatenating con dot first name. So after space contact first name will be displayed and then plus and in single quotes I'm writing comma and then BR. So BR is for line break it is HTML tab so that you can use. So, whenever this text will be rendered so in first line high and then first name will be displayed and after comma cursor will be on new line, then welcome to Salesforce ecosystem, and I'm just pasting it twice. So here I'm not concatenating any field so that's why everything is available in one time in single quotes then plus. So here I provided this text begin your Salesforce admin and developer journey with Sanjay Gupta. So this message will be displayed and in next line I'm writing happy learning. So this way this text is created and here I'm putting a semicolon so that this complete statement will end. So I wrote this in separate line that's why it is looking like so you can write it in single line as well. So remember like if you are using text as well as fields so you need to write it like this you need to use plus and these plus we are using because we are writing it in different lines. If you write it in it in single line so you can just copy and paste everything in one single quotes. So this way this content is created now in email masses dot set HTML body so I can pass this content variable here. Right. So now what I need to do I need to add this email masses instance to email list. So email list dot email masses so it is added. So this way this loop is completed now. And it is ending here. So after completion of this loop. I just need to in like use this email list so that I can send the messages. Remember I am writing message in dots and will and inside bracket I am passing email list. So whatever instances are available in this email list so to those instances emails will be triggered automatically. Right. So this way this code is completely implemented now it's time to test it. So we just need to create one contact record and we need to populate the email idea and we need to test whether email is triggered or not. So I'm jumping to the UI. And I'm going to create new contact record. So basically provided the data and it is showing like some duplicate rule is there so it is checking for that but I'm just clicking on save. So it is created. Now it's time to check the mail so I'm just logging in with my Gmail account. So let me jump to this one. You can see so basically now I'm logged in with Sanjay Gupta Tech School. So here this message is available and here you can see high test one. Welcome to Salesforce ecosystem begin your Salesforce admin and developer journey with Sanjay Gupta happy learning. Thank you. Everything is available in proper format. So with the help of this code I hope you, you are able to understand how we can use email service to send email notifications through trigger. One thing I want to show you like if you want to test it for bulk. So how you can do that. So you can simply write this code. So if you want to test it for bulk email notification. So what you can do you can create a contact list, then I just iterated this loop twice, and in first name last name I provided these values. I'm just renaming these so testing one email emailing one. So if I is one so this email ID will set if I is two or other than one this email ID will set. So I just added contacts into contact list and I'm going to insert two contacts. So when two contacts will be inserted so two emails will be triggered one will go to the email address and another will go to this email address. So here you can see there is no unread email here also. Now I'm going to execute this code so that we can see like both the email addresses are receiving emails or not. So I'm just clicking on execute. Okay it is saying duplicate detected. Okay, so I just need to change it. Okay, basically with these emails, various accounts are already available. So that's why it is showing this error. So I'm just removing these email IDs from here. Now moving here and clicking on execute. So this time it executed successfully. So if I come here so here I'm logged in as tech dot Sanjay Gupta. So you can see this test testing test to a mail is received here. If I go here here I'm logged in as Sanjay Gupta dot tech school. So here you can see testing test one is triggered. So this way, both the emails are triggering successfully. So this is the scenario which I demonstrated and I hope you understood how we can send email notifications through apex trigger and Salesforce.