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
chikkuchikku 

How send email dynamic based on Contact email id

  1. I have created a Vf page and apex to send PDF attachments in email.
  2. I have object Apple__c with Lookup Contact
  3. When I create a new Apple record and Particular contact with Email, then email should send to the contact which I have selected.
 
Now I have developed static, I need in dynamic.
public class sendAnEmail
{
    
    @InvocableMethod(label='Test' description='sends an email')
    public static void sendEmailWithAttachment(List<id> listofQuoteHeader)
    {
     List<Contact> Con = [SELECT Id, Email FROM Contact ];
           for(Id QuoteHeaderid :listofQuoteHeader)
           {
               PageReference pref= page.PDFGEN;
               pref.getParameters().put('id',(Id)QuoteHeaderid);
               pref.setRedirect(true);
               
               Attachment attachment = new Attachment();      
               Blob b=pref.getContentAsPDF();
               attachment.Body = b;
               attachment.Name = Datetime.now().format('yyyy-MM-dd HH:mm') + ' ' + 'Quote' + '.pdf';
               attachment.IsPrivate = false;
               attachment.ParentId = QuoteHeaderid;
               insert attachment;
               
               Messaging.SingleEmailMessage semail= new 
                Messaging.SingleEmailMessage();
               Messaging.EmailFileAttachment attach= new 
                   Messaging.EmailFileAttachment();
               attach.setBody(pref.getContentAsPDF());
               semail.setSubject('Quote Issued');
              String[] emailIds= new String[]{'abc@gmail.com'}; // I need this part has  
                 dynamic based on contact email id i choosed
               semail.setToAddresses(emailIds);
               
               semail.setPlainTextBody('Please find the attached quote details');
               semail.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
               Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});
           }
    }
 
}


 
ShivankurShivankur (Salesforce Developers) 
Hi Chikku,

This is been already addressed on following questions posted from your end:
https://trailblazers.salesforce.com/answers?id=9064S000000DrztQAC
https://developer.salesforce.com/forums/?id=9062I000000DIpp

Please mark as Best Answer so that it can help others in the future.

Thanks.