3

I really need your help because I'm completely stuck. I want to render a PDF from a Contract in Salesforce, and save it as a record's attachment.

I followed several tutorials and I created a PDF Visualforce page with the content of my contract + a standard controller extension to attach the PDF to the record, called from a secondary VF page that reference the first one and launch the action.

The PDF visualforce Page is working well, all the mergefields are here. But when I call the controller extension from the secondary one, the mergefields are not showing inside the PDF even if I declared them with the addFields method.

I'm going crazy because it worked before, and I stupidely overrided my code and now it's not working anymore :'O

My controller extension:

public class attachPDFtoContract {
    public Contract a {get;set;} //Contract object

    //method for date formatting
    public static String formatDate(Date d) {
    return d.year() + '-' + d.month() + '-' + d.day(); 
    }

    //constructor
    public attachPDFtoContract(ApexPages.StandardController standardPageController) {       
        standardPageController.addFields(new List<String>{'AMIKEO_model_type__c','CustomerSigned.Name', 'CustomerSigned.Phone', 'CustomerSigned.Email', 'BillingStreet', 'BillingCity', 'BillingPostalCode', 'BillingCountry', 'StartDate', 'loan_contract_end_date__c', 'datetime_meeting_1__c', 'datetime_meeting_2__c'});
        a = (Contract)standardPageController.getRecord();//instantiate the Contract object for the current record
    }

    //method called from the Visualforce's action attribute
    public PageReference attachPDF() {

        //generate and attach the PDF document
        PageReference pdfPage = Page.Convention_de_pret; //create a page reference to our pdfDemo Visualforce page, which was created from the post https://interactiveties.com/blog/2015/render-visualforce-pdf.php
        Blob pdfBlob; //create a blob for the PDF content
        if (!Test.isRunningTest()) { //if we are not in testing context
            pdfBlob = pdfPage.getContentasPDF(); //generate the pdf blob
        } else { //otherwise, we are in testing context and getContent() gets funky so create the blob manually
            pdfBlob = Blob.valueOf('Some Text');
        }
        Attachment attach = new Attachment(parentId = a.Id, Name = 'Convention_de_pret_'+ a.CustomerSigned.Name +'_'+ formatDate(a.StartDate) +'.pdf', body = pdfBlob);  //create the attachment object
        insert attach; //insert the attachment


        //redirect the user
        PageReference pageWhereWeWantToGo = new ApexPages.StandardController(a).view(); //we want to redirect the User back to the Account detail page
        pageWhereWeWantToGo.setRedirect(true); //indicate that the redirect should be performed on the client side
        return pageWhereWeWantToGo; //send the User on their way        
    }
}

The VF page I use to launch the action attachPDF() :

<apex:page standardController="Contract" extensions="attachPDFtoContract" action="{!attachPDF}>    
</apex:page>

And the PDF visualforce page with my content:

<apex:page standardController="Contract" extensions="attachPDFtoContract" renderAs="pdf" showHeader="false" sidebar="false" standardStylesheets="false" applyBodyTag="false" applyHtmlTag="false">
//mycontent with mergefields
</apex:page>

Is anyone could correct my code? Thank you in advance! Audrey

1 Answer 1

1

Not sure if you ever found an answer but I was experiencing the same issue and I was able to solve it by adding the following line to my controller extension.

pdfPage.getParameters().put('id',a.Id);

Add it right before this line "Blob pdfBlob;".

I hope that helps!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .