How send email dynamic based on Contact email id - Answers - Salesforce Trailblazer Community
Trailblazer Community
Ask Search:
CibiSaran SivaprakasamCibiSaran Sivaprakasam 

How send email dynamic based on Contact email id

I need to dynamic Email with the contact which chooses.
 
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
               semail.setToAddresses(emailIds);
               
               semail.setPlainTextBody('Please find the attached quote details');
               semail.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
               Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});
           }
    }
 
}

 
Best Answer chosen by CibiSaran Sivaprakasam
Kirti MishraKirti Mishra
Hi,

You will need to query the contact email.
Try the below code, change the object api name. I have taken 'Quote' as an example here.
 
public class sendAnEmail
{
    
    @InvocableMethod(label='Test' description='sends an email')
    public static void sendEmailWithAttachment(List<id> listofQuoteHeader)
    {

        Map<Id, Quote> quotesMap =  new Map<Id, Quote>([SELECT Contact.Email FROM Quote WHERE Id IN :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
            String[] emailIds= new String[]{quotesMap.get(QuoteHeaderid)?.Contact.Email};
               semail.setToAddresses(emailIds);
               
               semail.setPlainTextBody('Please find the attached quote details');
               semail.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
               Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});
           }
    }
 
}