0

I am getting an error in the below code..Its a very simple one actually but i am missing someething I am just trying to show 10 record in a table using apex:repeat

VF page

<apex:page controller="queryaccountrecords" >

<apex:form >
<apex:pageBlockTable>
<apex:pageBlock title="Account Records">
<apex:outputPanel id="ContactDetail" >
<apex:repeat value="{!accts }" var="dbItem">
<apex:outputText value="{!dbItem.Name}" /><br/>
</apex:repeat>
</apex:outputPanel>
</apex:pageBlock>
</apex:pageBlockTable>
</apex:form>

</apex:page>

controller:

public class queryaccountrecords {



    public queryaccountrecords()
     {
     Account[] accts = [SELECT Name FROM Account LIMIT 10];

    }


}
1
  • and what else i need to do if i want to show this data in a pdf on click of a button? Commented Jun 3, 2015 at 10:31

2 Answers 2

1

Here is updated code :

public class MyController {
public List<Account> accts{get;set;}
    public MyController (){ 
        accts = [SELECT Name FROM Account LIMIT 10];
    }
  public PageReference deliverAsPDF() {
     PageReference pdf =  Page.generatePdf;
            pdf.getParameters().put('p','p');
               pdf.setRedirect(true);
                       return pdf;
  }
  public String chooseRender {
     get { 
           return (ApexPages.currentPage().getParameters().get('p') != null) ? 'pdf' : null; 
         }
  }
}

And vf page is :

<apex:page controller="MyController" renderAs="{!if($CurrentPage.parameters.p == null, null, 'pdf')}"> 
    <apex:form >
        <apex:pageBlock title="Account Records">
            <apex:pageBlockTable value="{!accts}" var="acc">
                <apex:column value="{!acc.Name}"/> 
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
    <apex:outputLink rendered="{!$CurrentPage.parameters.p == null}" value="/apex/generatePdf?p=true" target="_blank">GENERATE PDF</apex:outputLink>
</apex:page>

Thanks

Aryan

10
  • and what else i need to do if i want to show this data in a pdf on click of a button? Commented Jun 3, 2015 at 10:31
  • 2
    u can use renderAs="pdf" attribute in <apex:page> and apply some css for pdf design. Commented Jun 3, 2015 at 10:34
  • but i want to show pdf on click of a button..if i add render as = pdf..the same page will be shown as a pdf Commented Jun 3, 2015 at 10:36
  • Means you have a Requirement to Download the Visualforce Page as PDF When click on CommandButton ?? Commented Jun 3, 2015 at 10:38
  • Yes....@aryan kapoor Commented Jun 3, 2015 at 10:38
1

Your accts variable should be visible to VF page. Try below code

public class queryaccountrecords {
    public Account[] accts{get;set;}
    public queryaccountrecords()
     {
        accts= [SELECT Name FROM Account LIMIT 10];
    }
}
2
  • Thanks for ur help..I tried ur code..I am getting an error - Error: Unknown property 'queryaccountrecords.accts @highfive Commented Jun 3, 2015 at 10:25
  • edited.use public access modifier
    – highfive
    Commented Jun 3, 2015 at 10:26

You must log in to answer this question.

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