You need to sign in to do that
Don't have an account?
carmilyn.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.
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.
Since you are using email template to send email you need to specify TargetObjectID,
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
Hope this helps.