You need to sign in to do that
Don't have an account?
kailash chandra 25
how can i send email through custom object?
I want to send email through custom object
List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
EmailTemplate emailSickEmpTemplate = [Select Id from EmailTemplate where developername = 'Email_with_list_of_sick_employees'];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateID(emailSickEmpTemplate.Id);
mail.setTargetObjectId(Userinfo.getUserId());
mail.setSaveAsActivity(false);
mail.setOrgWideEmailAddressId(owa.id);
allmsg.add(mail);
Messaging.sendEmail(allmsg,false);
This is the exception:
|SendEmail failed. First exception on row 0; first error: INVALID_TYPE_FOR_OPERATION, Only User, Contact, Lead, or Person objects are allowed for targetObjectId: a020v000002n3sr.: [targetObjectId, a020v000002n3srAAA]
List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
EmailTemplate emailSickEmpTemplate = [Select Id from EmailTemplate where developername = 'Email_with_list_of_sick_employees'];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateID(emailSickEmpTemplate.Id);
mail.setTargetObjectId(Userinfo.getUserId());
mail.setSaveAsActivity(false);
mail.setOrgWideEmailAddressId(owa.id);
allmsg.add(mail);
Messaging.sendEmail(allmsg,false);
This is the exception:
|SendEmail failed. First exception on row 0; first error: INVALID_TYPE_FOR_OPERATION, Only User, Contact, Lead, or Person objects are allowed for targetObjectId: a020v000002n3sr.: [targetObjectId, a020v000002n3srAAA]
TargetObjectId parameter is used to set the recipient of the email. You cannot set that value to a Custom Object Id, If you are trying to send an email to a User selected on one of the lookups on the Custom Object Record, then query for the Id and assign that to TargetObjectId.
All Answers
TargetObjectId parameter is used to set the recipient of the email. You cannot set that value to a Custom Object Id, If you are trying to send an email to a User selected on one of the lookups on the Custom Object Record, then query for the Id and assign that to TargetObjectId.
I explored this topic in my own org, finally we cant send email through custom object Because salesforce doesn't allow mail.setTargetObjectId(custom_object_record_id)
above solution will be send mail to owner of email template while i need to send email to the custom object.