I have a visualforce renderas=Pdf, I created a button to save them in notes and attachments related list, this work fine, but I can't get the value record information in my attachment.

If you can help, I appreciate.

public class VFController {

 // Constructor - this only really matters if the autoRun function doesn't work right
     public Opportunity o {get; set;}

    public VFController(ApexPages.StandardController stdController) {
        this.o = (Opportunity)stdController.getRecord();

    // Code we will invoke on page load.
    public PageReference autoRun() {

        String theId = ApexPages.currentPage().getParameters().get('id');

        if (theId == null) {
            // Display the Visualforce page's content if no Id is passed over
            return null;

       list<opportunity> oportunidade = [SELECT id, name, Numero_Sequencial__c FROM Opportunity WHERE id =:theId];

        for (Opportunity o: oportunidade) {
            // Do all the dirty work we need the code to do

       PageReference pdf = Page.Footer_Helitene;
             Attachment attach = new Attachment();
             Blob body;
              try {

        // returns the output of the page as a PDF
        body = pdf.getContentAsPDF();

    // need to pass unit test -- current bug    
    } catch (VisualforceException e) {
        body = Blob.valueOf('Some Text');

    attach.name = o.name+'  '+o.Numero_Sequencial__c+'.pdf' ;
     attach.Body = body;
     attach.ParentId = o.id;
     attach.IsPrivate = false;
     attach.ContentType = 'application/pdf';

     insert attach;


        // Redirect the user back to the original page
        PageReference pageRef = new PageReference('/' + theId);
        return pageRef;
  • Hi @lisyy can you expand on what you mean by "I can't get the value record information in my attachment."?
    – BarCotter
    Aug 11, 2014 at 11:18
  • 1
    For example in visualforce page <outputtext value={!opportunity.Amount}>, in button preview I can see the value of opportunity amount, but when I invoke the button, that value don't show up
    – lisyy
    Aug 11, 2014 at 11:25
  • @lisyy Have added an answer for same . Aug 11, 2014 at 12:19

1 Answer 1


This is because if your page thats been rendered as PDF if uses standard controller ,you need to set Id parameters for the same and also use setredirect(true)

Sample code is as follows

 PageReference pdf = Page.Footer_Helitene;
 pdf.getParameters().put('id',o.Id); /Set oportunity Id here 
 pdf.setRedirect(true);//Set redirect to true to be on safer side
 //Rest of your code

You must log in to answer this question.

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