function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sreekanth cheerasreekanth cheera 

When ever new student is created send him email alert with email template using apex?

GauravGargGauravGarg
Hi Sreekanth,
  • you can use Message.singleEmailMessage() class to complete your requirement. 
  • For template, either you build template within Apex class or create a custom email template for Student object and use in Apex. 
  • Write method to call Message.singleEmailMessage() on afterInsert Trigger call. 
  • https://www.salesforcetutorial.com/sending-emails-singleemailmessage/
  • Please ensure your email deliverability is ON. Setup -> Deliverability --> All email

Hope this will. 

Thanks,

Gaurav
Skype: gaurav62990

Akshay_DhimanAkshay_Dhiman

Hi Sreekanth,

Please try the below code when a new student is created send him email alert with an email template.

 

//Apex Class
public class SendMailWithStudent {
    
    public static void isSendEmail(List<Student__c> stuList)
    {
        if(stuList.size()>0)
        {
         
              String htmlBody = '';
            List<Messaging.SingleEmailMessage> emailList = new List<Messaging.SingleEmailMessage>();
            for(Student__c std : stuList)
            {
                if(std.Email__c !=NULL){
                    Messaging.SingleEmailMessage body = new Messaging.SingleEmailMessage();
                    body.setSubject('Alert  '+std.Name);
                    List<String> strList =new List<String>();
                    strList.add(std.Email__c);
                    body.setToAddresses(strList);
                    body.setSubject('Details of Contacts Inserted is ');
                // please put your email address
                    body.setReplyTo('yourEmail@gmail.com'); 
                    htmlBody ='<table border ="1" style="border-collapse: collapse"> <Caption> Details of all associated contacts</Caption><tr><th>Name</th></tr>';
                    htmlBody+='<tr><td>'+ std.Name +'</td><td>' ;
                    body.setHtmlBody(htmlBody);
                    emailList.add(body);
                }
                Messaging.sendEmail(emailList);
            }
        }
    }
}
//Trigger Class
trigger SendEmail on Student__c (before insert) {
    if(trigger.isInsert){
        SendMailWithStudent.isSendEmail(trigger.new);
    }

}
 

Please make it best if it helps you.

Thanks
Akshay

 

sreekanth cheerasreekanth cheera
public class NewStudent {

    public static list<Student__c> sendemail(list<Student__c> std){
        EmailTemplate et=[select id from EmailTemplate where name=:'NewStudent'];
        list<Messaging.SingleEmailMessage> email=new list<Messaging.SingleEmailMessage>();
        for(Student__c st:std){
            if(st.email__c!=null && st.Course__c!=null){
                Messaging.SingleEmailMessage msg=new Messaging.SingleEmailMessage();
               msg.setTargetObjectId(st.id);
              
                msg.setTemplateId(et.id);
                msg.setSaveAsActivity(false);
                email.add(msg);
            }
        }
        messaging.sendEmail(email);
        return std;
    }
      
    }

And Iam getting this Error

SendMail: execution of AfterInsert caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_TYPE_FOR_OPERATION, Only User, Contact, Lead, or Person objects are allowed for targetObjectId: a0F7F000004QANv.: [targetObjectId, a0F7F000004QANvUAO] Class.NewStudent.sendemail: line 18, column 1 Trigger.SendMail: line 2, column 1