0

So I have a lightning component that will open a visualforce page when you click the pdf button. The visualforce page opens in pdf format but I can't call the Apex variable to get the Account Name, Type etc. For simplicity I've only posted the relevant code. How can I call the apex variable to display account fields on the vfp page?

enter image description here Aura:

<aura:attribute name="searchResult" type="List"/>
<aura:attribute name="searchString" type="String"/>     
<aura:attribute name="Message" type="boolean" default="false"/>     
<aura:attribute name="id" type="object"/>

<aura:iteration items="{!v.searchResult}" var="account">
    <tr>
        <td><div class="slds-truncate" title="{!account.Name}">{!account.Name}</div></td>
        <td><div class="slds-truncate" title="{!account.Type}">{!account.Type}</div></td>
        <td>    

      <!-- Use a Lightning Base Component To display an icon next to the label -->

          <lightning:button
            label="PDF"
            iconName="utility:open"
            iconPosition="left"
            variant="base"
            type="submit"
            onclick="{!c.downloadPDF}"
            value="{!account.Id}" 
             />
       </td>
   </tr>
</aura:iteration>

Controller:

 downloadPDF : function (component, event, helper)
    {
        //get acc id from pdf button
        var myId = event.getSource().get('v.value'); 
        //call apex controller
        var action = component.get("c.DisplayAccounts");
        //set variable equal to acc id
        action.setParams({'pdfSearch': myId});
        action.setCallback(this,function(response){
            var state = response.getState();
            if(state === "SUCCESS"){
                alert(myId);            
                window.open('/apex/AccountPDFViewer?id=' + myId);


            }
            else{
                console.log('unable to generate the pdf');
            }
        });
        $A.enqueueAction(action);

    },  

Apex:

public class AccountPDFController
{   
    public String PDFData {get;set;}
    public static Account acc{get;set;}

    public AccountPDFController()
    {
        PDFData='';
        acc = new Account();
    }
   @AuraEnabled  
    public static Account DisplayAccounts(String pdfSearch)
    {
        system.debug(pdfSearch);
        String secondSearch = pdfSearch;
        List < Account > pdfList = [SELECT Id, name, Type FROM Account Where Id=:secondSearch];
        system.debug('Apex Step 4');
        acc = pdfList[0];
        system.debug('Apex step 5');
        return acc;

    }
}

VFP:

<apex:page controller="AccountPDFController" renderas="PDF">
    <apex:outputText value="{!acc.Name}"/>
</apex:page>

1 Answer 1

1

You will need standardController in apex page:

<apex:page standardController="Account" extensions="AccountPDFController " >
    name - {!Account.Name}
</apex:page>

You dont need any declaration of extra variable like acc. Standard controller Account itself acts as accessor. You extension controller should constructor with standard controller parameter:

public class AccountPDFController {

    public AccountPDFController(ApexPages.StandardController controller){}

}
5
  • Thanks! Worked perfect, why does it not work the other way?
    – Tyler
    Aug 12, 2019 at 15:26
  • It will work if you query the account in constructor by getting the Id parameter from url. Much more complex than using standard controller. Aug 12, 2019 at 15:27
  • Oh okay, thanks for the help! I still have much to learn
    – Tyler
    Aug 12, 2019 at 15:32
  • Is there anyway I could pm you? I have another question about querying from constructor. I actually just ran into another project with that as a use case
    – Tyler
    Aug 12, 2019 at 19:33
  • not sure how pm works in SFSE. I am actually new to this platform. However I answered your question :) Aug 13, 2019 at 3:25

You must log in to answer this question.

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