Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SendVF_Email_InvocableMethod {
- @InvocableMethod(label = 'Send an email from apex class'description= 'sends an email')
- public static void sendEmailWithAttachment(List<id> listofInvoice) {
- for (Id Invoiceid : listofInvoice) {
- PageReference pref = page.selected_invoice_pdf;
- pref.getParameters().put('id', (Id) Invoiceid);
- pref.setRedirect(true);
- Attachment attachment = new Attachment();
- Blob b = pref.getContentAsPDF();
- attachment.Body = b;
- attachment.Name = Datetime.now().format('yyyy-MM-dd HH:mm') + ' ' + 'Invoice' + '.pdf';
- attachment.IsPrivate = false;
- attachment.ParentId = Invoiceid;
- insert attachment;
- Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
- Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
- attach.setBody(pref.getContentAsPDF());
- semail.setSubject('Invoice Issued');
- String[] emailIds = new String[]{};
- for (Invoice__c i : [
- SELECT Billing_Contact_Email__c
- FROM Invoice__c]){
- emailIds.add(i.Billing_Contact_Email__c);
- }
- String s = String.join(emailIds, ', ');
- String[] ccAddresses = new String[]{'someaddress@email.com'};
- for (Invoice__c i2 : [
- SELECT Billing_Contact_Email_2__c
- FROM Invoice__c]){
- ccAddresses.add(i2.Billing_Contact_Email_2__c);
- }
- String s2 = String.join(ccAddresses, ', ');
- semail.setToAddresses(emailIds);
- semail.setCCAddresses(ccAddresses);
- semail.setBccSender(true);
- semail.setReplyTo('billing@email.com');
- semail.setSenderDisplayName('billing@email.com');
- semail.setHTMLBody('<p>Hi' + Invoice__c.Billing_Contact_Name__c +',</p><p>Attached please find your invoice from Company for your review. If you have any questions please contact billing@email.com<br /> <br />Thank you for your business!<br /> <br />Sincerely,<br />analytic.li Billing Team<br /> <br />P: (XXX) XXX-XXXX<br />E: billing@email.com</p>');
- semail.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
- Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});
- }
- }
- }
Add Comment
Please, Sign In to add comment