[SalesForce] Build a Pdf document with merge field from a record

I have tried to do this for few days, now I need your help. When in my org an user save a Lead a Pdf document have to be created automaticly. The Pdf have a template and the fields from the saved lead need to be put in that document in few blank spaces.
Now I'm using a VF page that render my lead page as a Pdf a custom controller and an helper class but It's not working because I'm not able to pass the fields between the saved Lead and the VF page.
Someone could give me a tip. Every Tips are very appreciated

Sorry I put the code below:
the vf page rendered as Pdf

    <apex:page standardController="Lead" extensions="dataController"  renderAs="pdf">   
<apex:image id="theImage" value="{!$Resource.stannah}"  style="margin-left:250px; margin-top:40px;"/> 
  <br/>
  <br/>  
  <h2>Gentile <apex:outputText value=" {!name}"/></h2>
  <br/>
  Ci risulta che lei lavora presso <apex:outputText value=" {!company}"/><br/>
  <br/> 
  Siamo riusciti a creare il file pdf con i suoi dati<br/>
  Grazie per aver richiesto informazioni per i nostri prodotti<br/>
  <br/>
<apex:image id="theImage2" value="{!$Resource.num_verde}"  style="margin-left:245px; margin-top:40px;"/>
  <br/>
  <br/>
  Cordiali saluti<br/>
  Lo staff di Stannah  
</apex:page>

this is the controller extension

global class dataController {
    global static String nn1;
    global static String cc2;

  public String name {get; set;}
  public String company {get; set;}

  public Lead ld{get;set;}  

     private ApexPages.StandardController stdController;

   public dataController(ApexPages.StandardController stdController) {
       this.stdController = stdController;
       this.ld = (Lead)stdController.getrecord();
       getMydata();
   }

   public void getMydata() {
       name=nn1;
       company=cc2;
       System.debug('7name: '+name);
       System.debug('8company: '+Company);
   }
}

This is the help class. I use this with a process builder when a lead is created process builder fired the invocable method that have to create the pdf page and put it in a document

public class PdfLead {

@InvocableMethod(label='PDF' description='creo dei pdf')   
public static list<String> pagePdf(){
    System.debug('passo 1');

    Lead l=[select id, firstName, company from lead order by CreatedDate desc limit 1];
      System.debug('1name: '+l.FirstName);
      System.debug('2company: '+l.Company);

     dataController.nn1 = l.FirstName;
     dataController.cc2 = l.Company;
     System.debug('3name: '+dataController.nn1);
     System.debug('4company: '+ dataController.cc2);
     Blob b;

     PageReference pdf = Page.PageOfLead;
    //pdf.getParameters().put('id', ld.Id);
    pdf.setRedirect(true);
    Blob body;

    try{
    // returns the output of the page as a PDF
    body = pdf.getContent();
    } catch (VisualforceException e) {
      body = Blob.valueOf('Some Text');
        System.debug(e);
        //10:59:06:171 USER_DEBUG [18]|DEBUG|System.VisualforceException: 
        //system.security.NoDataFoundException: Unable to retrieve object
    }

     /*generatePdf2 gp = new generatePdf2();
     b = gp.pagePdf2();   
     System.debug('5Dimensione: '+b.size());*/

     document d = new document(); 
        d.folderid='00lU00000018TxaIAE';
        d.Name = l.FirstName; 
        d.Body = b; 
        d.ContentType = 'application/pdf';
        d.Type = 'pdf';

    insert d;
    return null;
}

Now we are trying to pass only lead Name and Company to the Vf page rendered as Pdf, but it's not working. Sorry if i don't put the code before.

Best Answer

For your Visualforce, you don't even need an extension. You can merge in fields from the standard controller. Here is a simple demonstration:

<apex:page standardController="Lead">
    <div>
        Name: <apex:outputField value="{!Lead.Name}" />
    </div>
    <div>
        Company: <apex:outputField value="{!Lead.Company}" />
    </div>
</apex:page>

It might not solve the root issue but it will at least simplify your code. Don't use Apex unless you have to.

Related Topic