You need to sign in to do that
Don't have an account?
Anju Alexander 8
Sending Email using Email address from Account
Hi All,
I am using Account(Person Account) object and one custom object Recurring Donations. Recurring Donations have a lookup relationship to Account. Now I am sending an email whenever a donation record is newly created through trigger and I am getting the email address from Account Object In the email I am using html template from email templates.( I am using html template since i am using images inside the email ). Now if I am using template I need to give targetObjectId. So I gave targetObjectId as accountId. When I give targetObjectId as accountId it is giving error as -You can give only lead,contact and user as targetObjectId. I am not using Contact Object. So guys please help me to find a solution for sending email using Account's email...I can only use email from Account object Below....Please Help
Thanks,
Anju Alexander
I am using Account(Person Account) object and one custom object Recurring Donations. Recurring Donations have a lookup relationship to Account. Now I am sending an email whenever a donation record is newly created through trigger and I am getting the email address from Account Object In the email I am using html template from email templates.( I am using html template since i am using images inside the email ). Now if I am using template I need to give targetObjectId. So I gave targetObjectId as accountId. When I give targetObjectId as accountId it is giving error as -You can give only lead,contact and user as targetObjectId. I am not using Contact Object. So guys please help me to find a solution for sending email using Account's email...I can only use email from Account object Below....Please Help
Thanks,
Anju Alexander
trigger sendMail on npe03__Recurring_Donation__c (before insert) { Set<id> Ids = new Set<id>(); for(npe03__Recurring_Donation__c donation : trigger.new) { if(donation.npe03__Organization__c!=null) //npe03__Organization__c is the accountId in recurring donations { Ids.add(donation.npe03__Organization__c); System.debug('****'+donation.npe03__Organization__c); } /*catch (NullPointerException e) { donation.addError(e); }*/ } // Step 0: Create a master list to hold the emails we'll send List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>(); List<Account> accnt = new List<Account>(); for(Account acc : [SELECT id,name,PersonEmail,FirstName from Account where id IN:Ids]) { accnt.add(acc); } for(Account acnt : accnt) { if (acnt.PersonEmail != null && acnt.FirstName != null) { // Step 1: Create a new Email Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); // Step 2: Set list of people who should get the email List<String> sendTo = new List<String>(); sendTo.add(acnt.PersonEmail ); mail.setToAddresses(sendTo); mail.setTargetObjectId(acnt.Id); for(OrgWideEmailAddress owa: [SELECT ID,DisplayName,Address FROM OrgWideEmailAddress]) { mail.setOrgWideEmailAddressId(owa.id); } EmailTemplate templateId = [Select id from EmailTemplate where DeveloperName= 'Recurring_Donation']; mail.setTemplateID(templateId.id); mails.add(mail); } } Messaging.sendEmail(mails); }
1. As you said that you are not using Contact object you can create Contact with Account email address on the fly and pass contact id in targetObjectId field. I know this is ugly way to do that but it will be easy to implement for you.
2. Create a new email field on you npe03__Recurring_Donation__c object and fill Account email id to that field and then use workflow to send an email.
Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant
P.S. If my answer helps you to solve your problem please mark it as best answer.
As per my understanding Workflow fire after trigger execution so you should have value in that field.
Thanks,
Himanshu
There is one more solution which I found though it is tricky
Let me know if that helps you.
Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant
P.S. If my answer helps you to solve your problem please mark it as best answer.
Last option is only when you are not using workflow and completely handling using trigger. Makes sense ?
Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
I understood your concern. If you will check my code line 10 is setting Targetobjectid with contact but we are not actually sending email to that contact id. we are only getting compiled html body from email template which you will use to send email at line 25
makes sense ?
Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help others to find best answer.