Guest User

Untitled

a guest
Jul 25th, 2018
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. public class SendVF_Email_InvocableMethod {
  2.  
  3. @InvocableMethod(label = 'Send an email from apex class'description= 'sends an email')
  4. public static void sendEmailWithAttachment(List<id> listofInvoice) {
  5. for (Id Invoiceid : listofInvoice) {
  6.  
  7. PageReference pref = page.selected_invoice_pdf;
  8. pref.getParameters().put('id', (Id) Invoiceid);
  9. pref.setRedirect(true);
  10.  
  11. Attachment attachment = new Attachment();
  12. Blob b = pref.getContentAsPDF();
  13. attachment.Body = b;
  14. attachment.Name = Datetime.now().format('yyyy-MM-dd HH:mm') + ' ' + 'Invoice' + '.pdf';
  15. attachment.IsPrivate = false;
  16. attachment.ParentId = Invoiceid;
  17. insert attachment;
  18.  
  19. Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
  20. Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
  21. attach.setBody(pref.getContentAsPDF());
  22. semail.setSubject('Invoice Issued');
  23. String[] emailIds = new String[]{};
  24. for (Invoice__c i : [
  25. SELECT Billing_Contact_Email__c
  26. FROM Invoice__c]){
  27. emailIds.add(i.Billing_Contact_Email__c);
  28. }
  29. String s = String.join(emailIds, ', ');
  30.  
  31.  
  32. String[] ccAddresses = new String[]{'someaddress@email.com'};
  33. for (Invoice__c i2 : [
  34. SELECT Billing_Contact_Email_2__c
  35. FROM Invoice__c]){
  36. ccAddresses.add(i2.Billing_Contact_Email_2__c);
  37. }
  38. String s2 = String.join(ccAddresses, ', ');
  39.  
  40. semail.setToAddresses(emailIds);
  41. semail.setCCAddresses(ccAddresses);
  42. semail.setBccSender(true);
  43. semail.setReplyTo('billing@email.com');
  44. semail.setSenderDisplayName('billing@email.com');
  45. 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>');
  46. semail.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
  47. Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});
  48. }
  49. }
  50.  
  51. }
Add Comment
Please, Sign In to add comment