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
Hope this will.
Thanks,
Gaurav
Skype: gaurav62990
Hi Sreekanth,
Please try the below code when a new student is created send him email alert with an email template.
Please make it best if it helps you.
Thanks
Akshay
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