There may be scenario in Salesforce that you need to send a Visualforce page rendered as PDF as a part of Email Attachment. This will be very easy if you want to perform this using Controller or Extension class, we just have to call
getContentAsPDF() method of
PageReference class and use returned blob data as a attachment in Email.
However, If we are talking about achieving same in Trigger then it would be problem. Trigger does not support getContent() method of PageReference class. If you are thinking to use getContent() in future call then again you are not lucky, because @future methods does not support it. Also
Apex job doesn’t support this method.
Now, I hope you understood that in which situation we are
So, In this article, I am going to explain how to resolve this issue. Not exactly resolve but workaround for above problem.
Solution is very simple, We will expose apex method as a REST API. Code for sending email will be written in APEX based REST API and our Trigger will call this method asynchronously using @future annotation.
Sample Visualforce Page, page Name – “PDF_Demo”
1 | < apex:page renderAs = "PDF" > |
3 | < h1 >Congratulations</ h1 > |
Lets assume that above Visualforce page needs to be send as a part of attachment.
Creating APEX based REST API with POST Method :
2 | * Created By : Jitendra Zaa |
3 | * Description : Apex based REST API which exposes POST method to send Email |
5 | @RestResource (urlMapping= '/sendPDFEmail/*' ) |
6 | Global class GETPDFContent{ |
8 | global static void sendEmail(String EmailIdCSV, String Subject, String body) { |
10 | List<String> EmailIds = EmailIdCSV.split( ',' ); |
12 | PageReference ref = Page.PDF_DEMO; |
13 | Blob b = ref.getContentAsPDF(); |
15 | Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); |
17 | Messaging.EmailFileAttachment efa1 = new Messaging.EmailFileAttachment(); |
18 | efa1.setFileName( 'attachment_WORK.pdf' ); |
22 | email.setSubject( Subject +String.valueOf(DateTime.now())); |
23 | email.setToAddresses( EmailIds ); |
24 | email.setPlainTextBody(Body); |
25 | email.setFileAttachments( new Messaging.EmailFileAttachment[] {efa1}); |
26 | Messaging.SendEmailResult [] r = Messaging.sendEmail( new Messaging.SingleEmailMessage[] {email}); |
In above code, we have created APEX based REST API with POST method. We are simply getting content of Visualforce rendered as PDF with method “getContentAsPDF()” and setting up this in object of “Messaging.EmailFileAttachment”.
REST API created using Apex class can be accessed from URL “https://<YOUR_SALESFORCE_INSTANCE>/services/apexrest/<RESTURL>”
In below part of code, we will be consuming REST API recently created
2 | * Created By : Jitendra Zaa |
3 | * Description : This class exposes @future method to send VF page rendered as PDF as attachment in Email |
5 | public class SendVFAsAttachment{ |
8 | public static void sendVF(String EmailIdCSV, String Subject,String body,String userSessionId) |
12 | HttpRequest req = new HttpRequest(); |
13 | req.setEndpoint( addr ); |
14 | req.setMethod( 'POST' ); |
15 | req.setHeader( 'Authorization' , 'OAuth ' + userSessionId); |
16 | req.setHeader( 'Content-Type' , 'application/json' ); |
18 | Map<String,String> postBody = new Map<String,String>(); |
19 | postBody.put( 'EmailIdCSV' ,EmailIdCSV); |
20 | postBody.put( 'Subject' ,Subject); |
21 | postBody.put( 'body' ,body); |
22 | String reqBody = JSON.serialize(postBody); |
25 | Http http = new Http(); |
26 | HttpResponse response = http.send(req); |
Note : Please make sure you have added URL of your salesforce in “Remote Site Settings“, else you will not be able to make REST API call.
Question : How do we set Body for POST method in Apex based REST API or How do we supply argument in POST method ?
Answer : As you can see in above code sample, we have to supply JSON as a Request Body and set Header ContentType as “application/json“.
1 | req.setHeader( 'Content-Type' , 'application/json' ); |
3 | Map<String,String> postBody = new Map<String,String>(); |
4 | postBody.put( 'EmailIdCSV' ,EmailIdCSV); |
5 | postBody.put( 'Subject' ,Subject); |
6 | postBody.put( 'body' ,body); |
7 | String reqBody = JSON.serialize(postBody); |
Writing Trigger:
2 | * Created By : Jitendra Zaa |
3 | * Description : This Trigger calls @future method to send VF page rendered as PDF as attachment in Email |
4 | * Note : Make sure that this Trigger does not call Future methods more than 10 times. |
6 | trigger SampleTrigger on Lead (before insert) { |
7 | SendVFAsAttachment.sendVF( 'emailaddress1@email.com,emailaddress2@email.com' , 'Sample Test from Trigger' , |
8 | 'Sample Email Body' ,UserInfo.getSessionId()); |
From above sample code, we are calling static method asynchronously, which in turn calling REST API to send Email with Visualforce as attachment.
No comments:
Post a Comment