function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anju Alexander 8Anju 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

 
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);
  }

 
Himanshu ParasharHimanshu Parashar
Hi Anju,

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.
Anju Alexander 8Anju Alexander 8
Hi Himanshu, Thanks for the reply. But if i am going with your second option to create a new email id field and create a workflow...i won't get the emailid from Recurring donations to send the mail. When creating email alert, i won't get the email id in Recipient Type
Himanshu ParasharHimanshu Parashar
Hi Anju,

As per my understanding Workflow fire after trigger execution so you should have value in that field.

Thanks,
Himanshu
Himanshu ParasharHimanshu Parashar
Hi Anju,

There is one more solution which I found though it is tricky
 
// Pick a dummy Contact
 Contact c = [select id, Email from Contact where email <> null limit 1];

 // Construct the list of emails we want to send
 List<Messaging.SingleEmailMessage> lstMsgs = new List<Messaging.SingleEmailMessage>();

 Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
 msg.setTemplateId( [select id from EmailTemplate where DeveloperName='My_Email_Template'].id );
 msg.setWhatId( [select id from Account limit 1].id );
 msg.setTargetObjectId(c.id);
 msg.setToAddresses(new List<String>{'random_address@opfocus.com'});

 lstMsgs.add(msg);

 // Send the emails in a transaction, then roll it back which will not actually send an email since transection rolleback.
 Savepoint sp = Database.setSavepoint();
 Messaging.sendEmail(lstMsgs);
 Database.rollback(sp);

 // For each SingleEmailMessage that was just populated by the sendEmail() method, copy its
 // contents to a new SingleEmailMessage. Then send those new messages.
 List<Messaging.SingleEmailMessage> lstMsgsToSend = new List<Messaging.SingleEmailMessage>();
 for (Messaging.SingleEmailMessage email : lstMsgs) {
 Messaging.SingleEmailMessage emailToSend = new Messaging.SingleEmailMessage();
 emailToSend.setToAddresses(email.getToAddresses());
 emailToSend.setPlainTextBody(email.getPlainTextBody());
 emailToSend.setHTMLBody(email.getHTMLBody());
 emailToSend.setSubject(email.getSubject());
 lstMsgsToSend.add(emailToSend);
 }
 Messaging.sendEmail(lstMsgsToSend); //Now Actually send email.



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.

 
Anju Alexander 8Anju Alexander 8
Hi Himanshu, When i use workflow, I am not using trigger. Thanks
Himanshu ParasharHimanshu Parashar
Hi Anju,

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.
Anju Alexander 8Anju Alexander 8
Hi Himanshu, My problem is that since I am using email template i need to give targetObjectId..If I am not using template and setting the email body in trigger itself , I don't have to set targetObjectId and the email gets sent to the email id in accounts record. Is there any way to send this email ?
Himanshu ParasharHimanshu Parashar
Hi Anju,

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 ?
Himanshu ParasharHimanshu Parashar
sorry If I am being so stupid to understand your problem.
Anju Alexander 8Anju Alexander 8
Hi Himanshu, Sorry...actually I didn't see your code. Thanks for the help
Himanshu ParasharHimanshu Parashar
ok no problem.

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.