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
carmilyn.c martincarmilyn.c martin 

send an email via Trigger

Hello, 

This is my Trigger and I am receiving an error when I am editing a Record:

trigger Eisai_Send_Email_Notification_End_Of_Fiscal_Year on PTO_esi__c(after update){
    List<String> sendToRep_List = new List<String>();
    List<String> sendToDM_List = new List<String>();
    String emailTemplateIdRep = [SELECT Id FROM EmailTemplate WHERE DeveloperName = 'Email_Template_PTO_Year_End_Notification_Rep'].Id;
    String emailTemplateIdDM = [SELECT Id FROM EmailTemplate WHERE DeveloperName = 'Email_Template_PTO_Year_End_Notification_DM'].Id;
    
    for(PTO_esi__c tpto: [SELECT Owner_Email__c,ID FROM PTO_esi__c WHERE Type_of_User__c = 'REP'])
        {
        sendToRep_List.add(tpto.Owner_Email__c);
        }
    for(PTO_esi__c tpto: [SELECT Owner_Email__c,ID FROM PTO_esi__c WHERE Type_of_User__c = 'DM'])
        {
        sendToDM_List.add(tpto.Owner_Email__c);
        }
    
    for(PTO_esi__c pto: trigger.new){
        
        if(pto.Active__c && pto.Current_user_Level__c == 'REP')
            {
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setToAddresses(sendToRep_List);
            mail.setTemplateId(emailTemplateIdRep);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{ mail });
            }
            
        if(pto.Active__c && pto.Current_user_Level__c == 'DM')
            {
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setToAddresses(sendToDM_List);
            mail.setTemplateId(emailTemplateIdDM);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{ mail });
            }
    }

}




ERROR Message: 
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger Eisai_Send_Email_Notification_End_Of_Fiscal_Year caused an unexpected exception, contact your administrator: Eisai_Send_Email_Notification_End_Of_Fiscal_Year: execution of AfterUpdate caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Missing targetObjectId with template: []: Trigger.Eisai_Send_Email_Notification_End_Of_Fiscal_Year: line 31, column 1

I am editing a record with value on this field (Current_user_Level__c ) = DM
and setting Active checkbox to true. 

Thank you. 
Prateek Singh SengarPrateek Singh Sengar
Hi,
Since you are using email template to send email you need to specify TargetObjectID,
setTargetObjectId(targetObjectId)

Required if using a template, optional otherwise. The ID of the contact, lead, or user to which the email will be sent. The ID you specify sets the context and ensures that merge fields in the template contain the correct data.
Signature

Now from your code it looks like you want to send the email to recipients from the custom field. Now to achieve this you will have to do update your code as follows
 
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(sendToRep_List);
mail.setTemplateId(emailTemplateIdRep);

//if using mail template its mandatory to populate TargetObjectID
mail.setTargetObjectId(USERRECORDID);
//by setting the below parameter the email will be send to Toaddess and not targetobjectID
mail.setTreatTargetObjectAsRecipient(false);

Messaging.sendEmail(new Messaging.SingleEmailMessage[]{ mail });

Hope this helps.


 
carmilyn.c martincarmilyn.c martin
i need to make sure that all updated records owner are the ones to receive an email, i am not sure if I did it correctly