If you want to use a template to send emails then you need to use setTargetObjectId along with setTemplateId. Custom object Id isn't accepted in setTargetObjectId by Salesforce. Only User, Contact, Lead, or Person objects are allowed for targetObjectId.
For workaround (maybe), please refer to the below link with a similar discussion which might help you further with the above issue.
You can send emails to custom object emails easily if you don't want to use a template.
public with sharing class SendMailCustomObjectController {
public void sendMassMail() {
List<Candidate__c> lstcon = [SELECT id, Name, Email__c FROM Candidate__c WHERE Email__c != NULL ];
String cemail;
for(Candidate__c cd:lstcon) {
cemail = cd.Email__c;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.toAddresses = new String[] { cemail };
mail.subject = 'Subject Test Message';
mail.plainTextBody = 'This is a test mail';
Messaging.SingleEmailMessage[] mailss = new List<Messaging.SingleEmailMessage> {mail};
Messaging.SendEmailResult[] results = Messaging.sendEmail(mailss);
}
}
}
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
OrgWideEmailAddress owa = [select id, DisplayName, Address from OrgWideEmailAddress limit 1];
EmailTemplate templateId = [Select id from EmailTemplate where name = 'Waitlisted opportunity now open'];
List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateID(templateId.Id);
mail.setWhatId(REcordId);
mail.setSaveAsActivity(false);
mail.setOrgWideEmailAddressId(owa.id);
allmsg.add(mail);
Messaging.sendEmail(allmsg,false);
Greetings to you!
If you want to use a template to send emails then you need to use setTargetObjectId along with setTemplateId. Custom object Id isn't accepted in setTargetObjectId by Salesforce. Only User, Contact, Lead, or Person objects are allowed for targetObjectId.
For workaround (maybe), please refer to the below link with a similar discussion which might help you further with the above issue.
https://salesforce.stackexchange.com/questions/61552/send-email-with-template-on-custom-records-email-id
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
Thanks and Regards,
Khan Anas
You can send emails to custom object emails easily if you don't want to use a template.
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
Thanks and Regards,
Khan Anas
Below query can fulfill your requirements. Hope this will work for you.
List<Messaging. MassEmailMessage > messagesToSend = new List<Messaging. MassEmailMessage>();
for (Id objectId : dataExtractRequestIds) {
Messaging. MassEmailMessage mail = new Messaging. MassEmailMessage();
mail.setTargetObjectIds(userIds);
mail.setTreatTargetObjectAsRecipient(true);
mail.setTemplateId(templateId);
mail.setwhatId(objectId);
mail.setSaveAsActivity(false);
messagesToSend.adD(mail);
}
Messaging.sendEmail(messagesToSend);
Please mark this as best answer if this solves your problem.
Thank you
Ajay Dubedi