+ Start a Discussion
Ryan GreeneRyan Greene 

VF Page not pulling info properly

I have a VF Page (code below) on a Contact which renders as a PDF and is inserted in another record when a Lead is converted. If I pull the page independently it works filling the correct info. When it is paired with the Apex (code below) it fails with a fatal error and the break occurs on the line in the Apex "pdfBody = pdfPage.getContentAsPDF();". Should I be calling the page in a different way, or am I completely doing this wrong?
Thank you
VF Page:
<apex:page standardController="Contact" applyBodyTag="false" renderAs="pdf">
<!--CSS Styling Removed-->
                 <!-- Breaks when trying to pull Contact First Name. If I do not pull the first name Apex and VF Page run perfectly -->
                I, {!Contact.FirstName} acknowledge blah blah blah
public class EchoSignRG{
    public ApexPages.StandardController Controller;
    public EchoSignRG(ApexPages.StandardController Controller){
        this.Controller = Controller;}
    public static void SendPDF(List<ID> conid){
        Lead L = [SELECT Id,Name,Street,City,State,PostalCode,ConvertedContactId,ConvertedOpportunityId,LeadSource,ConvertedAccountId FROM Lead WHERE Id =: conid];
        Contact con1 = [SELECT Id,AccountId,email,Firstname,lastname FROM Contact WHERE Id =: L.ConvertedContactId];
            pageReference pdfPage = Page.ContactPDF;
            blob pdfBody;
                pdfBody = blob.valueOf('Unit.Test');
                pdfBody = pdfPage.getContentAsPDF();}
//Fatal Error LINE ABOVE
            attachment pdfFile = new attachment();
            pdfFile.isPrivate = false;
            pdfFile.body      = pdfBody;
            pdfFile.parentId  = agreementRec1.id;
            pdfFile.Name      = agreementRec1.Name+'.pdf';
            insert pdfFile;
Alain CabonAlain Cabon

Did you use a sample of code written before 2015? (like this one 2014: https://developer.salesforce.com/page/Visualforce_Quote2PDF )

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

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.

http://www.jitendrazaa.com/blog/tag/getcontentaspdf/ (http://www.jitendrazaa.com/blog/tag/getcontentaspdf/*)

2) InvocableMethod Annotation: Use the InvocableMethod annotation to identify methods that can be run as invocable actions.
Invocable methods are called with the REST API and used to invoke a single Apex method. Invocable methods have dynamic input and output values and support describe calls.