function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
David89David89 

How to create a pdf?

Hi all,

I need to create a pdf (with the lead information) when a lead is created, this pdf is saved as document.

I wrote an apex class with an invocable method which fire in a process builder after 
a lead creation, and a visualforce page to insert the lead information.

This system doesn't work for the lead that fired the process builder, instead it works 
if I insert an id of existing lead.

Which is the problem?

Apex class:
public class PdfLead {
    
@InvocableMethod(label='PDF' description='creo dei pdf')   
public static list<Document> pagePdf(){
    
    Lead l = [select id, name from lead order by CreatedDate desc limit 1];
    
    PageReference pdf = Page.PageOfLead;
    pdf.getParameters().put('id', l.Id);
    
    Blob body;
    
    try{
    // returns the output of the page as a PDF
    body = pdf.getContent();
    } catch (VisualforceException e) {
      body = Blob.valueOf('Some Text');
    }
      
 
    List<Document> doc = new List<Document>();
    Document d = new Document(); 
        d.folderid='00lU00000018TxaIAE';
        d.Name = l.Name; 
        d.Body = body; 
        d.ContentType = 'application/pdf';
        d.Type = 'pdf';
    doc.add(d);    
    insert doc;
    return doc;

}

}
Visualforce page:
<apex:page StandardController="Lead" renderAs="pdf">  
  <h2>Gentile {! Lead.Name }</h2>
  <br/>
  Ci risulta che lei lavora presso {!Lead.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/>
</apex:page>

Thanks!!
 
Tom JansenTom Jansen
Hello David,

have you found the solution for this problem? Because I'm having the same issue..
Tom JansenTom Jansen
The solution is to use a time-based action for this problem. I solved this somewhere in December 2016. If you cheat a little bit, you can set a date that is 1 hour and 5 minutes later than the created date. In the process builder you call the invocable action one hour BEFORE the custom date. This way, 5 minutes after the creation of the Lead, your invocable apex will launch. When I did it this way, it solved the problem.
Mustapha Elmadi 14Mustapha Elmadi 14
Hello !

I did have the same problm about only one field that i want to show in my pdf. so i tried to implement the trick but with no succes.
The solution that i found is to pass the field directly in my url and then use the code below in your visualforce :)
<apex:outputText value="{!$CurrentPage.parameters.parameterName}" />