1

I create a PDF with button at the account record. That works good, but now I want to pass information from the account record into my visual force. I tried it, see below, but the (AName) account name does not appear at the pdf.

APEX CLASS

public class attachPDFToAccount2 {

private Id accId {get; set;}
Public List<Account> AccList {get; set;}
public String AName {get; set;}

public attachPDFToAccount2() {
    accId = ApexPages.currentPage().getParameters().get('id');
    AccList = [SELECT Name FROM Account WHERE Unternehmens_Id_Long__c = :accId]; 
    FOR (Account Acc : AccList) {
        AName = Acc.Name;
    }
}

public final Account a;

public attachPDFToAccount2(ApexPages.StandardController standardPageController) {
    a = (Account)standardPageController.getRecord(); 
}

public PageReference attachPDF() {
    
    PageReference pdfPage2 = Page.pdfpage2; 
    Blob pdfBlob; 
    if (!Test.isRunningTest()) { 
        pdfBlob = pdfPage2.getContent(); 
    } else { 
        pdfBlob = Blob.valueOf('...');
    }
    Attachment attach = new Attachment(parentId = a.Id, Name = 'pdffile.pdf', body = pdfBlob); 
    insert attach;
    
    PageReference pageWhereWeWantToGo = new ApexPages.StandardController(a).view();
    pageWhereWeWantToGo.setRedirect(true); 
    return pageWhereWeWantToGo; 
}

}

ACTION PAGE

<apex:page action="{!attachPDF}" extensions="attachPDFToAccount2" standardController="Account">
    <apex:pageMessages ></apex:pageMessages>
    <apex:detail inlineEdit="true" relatedList="true"></apex:detail>
</apex:page>

RENDER PAGE

<apex:page renderAs="pdf" controller="attachPDFToAccount2">
<apex:stylesheet value="{!$Resource.pdfStyle}"/>
<center>
    <h1>Visualforce PDF Sample - {!AName}</h1>
</center>
<table align="center" style="border: 1px solid #6699CC;">
    <tr>
        <td style="background-color: #6699CC; color: #FFFFFF; font-size: 200%; padding: 10px;">NOTES</td>
        <td>
            <ul>
                <li>TEST</li>
            </ul>
        </td>
    </tr>
</table>
</apex:page>
2
PageReference pdfPage2 = Page.pdfpage2; 
...
pdfBlob = pdfPage2.getContent();

This causes a new, separate instance of the controller to be loaded; it doesn't know about the variables in the original context. You need to give it the correct parameter:

PageReference pdfPage2 = Page.pdfpage2; 
pdfPage2.getParameters().put('id', accId);
Blob pdfBlob; 
if (!Test.isRunningTest()) { 
    pdfBlob = pdfPage2.getContent(); 
} else { 
    pdfBlob = Blob.valueOf('...');
}
1
  • Can you give me some more tipps, I changed the code but I got no results, the accountname does not appear
    – peXeq
    Aug 2 '21 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.