Send Visualforce as an email attachment from Apex Trigger – Alternate design

Send Visualforce as an email attachment from Apex Trigger

Previously I had written same blog post and it was working fine before Winter 16. However, after Winter 16 critical update was released by Salesforce to consider getContent() method as a callout. If this update is enabled in your Salesforce instance then my previous blog post will not work.

In this blog post, we will go through alternate design, where we would still be able to send Visualforce page content as an Email attachment. Difference is, instead of Apex Trigger , Invocable Method and Process builder will help us getting there.

Again, consider below simple Visualforce page, which we want to send as an attachment.

Salesforce Winter 16 – Top features

You can watch recording of my live stream channel as well to know more on Winter 16 features. Powerpoint presentation used in livestream can be accessed from here.

Salesforce Winter 16
Salesforce Winter 16

1. Object Manager

Objects are objects, even if they are standard or custom objects. Salesforce simplified navigation to access Objects, we can access it now from “Object Manager” menu in setup.

Object Manager - Salesforce Winter 16
Object Manager

2. Lightning App builder to edit record page layout (pilot)

“Lightning App builder” should be enabled first to use this feature. We can use lightning components developed in Org or from AppExchange and change new lightning layout by using intuitive, drag and drop page layout editor.

Lightning App builder - Record page edit - Salesforce Winter 16
Lightning App builder – Record page edit

3. Rollup Summary field limit increased from 10 to 25

4. Restricted Picklist (Pilot)

Using Apex , Dataloader or any other API , it is possible to save any other value than allowed value in picklist field.  This future will enforce data integrity and result in error if other than allowed value selected in DML operation.

Restricted Picklist - Salesforce Winter 16
Restricted Picklist

Send Email with Generated PDF as attachment from Trigger – before Winter 16

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.

Update – 21 Oct 2015 (Winter 16)

After Winter 16 release, this solution will not work as getContent() method is treated as callout and if we try to call it from Async Apex or Rest API in this case, it will not return pdf content.

This solution will only work for you if you have not enabled critical update “PageReference getContent() and getContentAsPDF() Methods Treated as Callouts” in your Salesforce organization.

Note : If this solution is not working for you then try this.

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 we 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.