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];

    }


}
  • and what else i need to do if i want to show this data in a pdf on click of a button? – Ankita Singh Jun 3 '15 at 10:31
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

| improve this answer | |
  • and what else i need to do if i want to show this data in a pdf on click of a button? – Ankita Singh Jun 3 '15 at 10:31
  • 2
    u can use renderAs="pdf" attribute in <apex:page> and apply some css for pdf design. – Arjun Khatri Jun 3 '15 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 – Ankita Singh Jun 3 '15 at 10:36
  • Means you have a Requirement to Download the Visualforce Page as PDF When click on CommandButton ?? – Arjun Khatri Jun 3 '15 at 10:38
  • Yes....@aryan kapoor – Ankita Singh Jun 3 '15 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];
    }
}
| improve this answer | |
  • Thanks for ur help..I tried ur code..I am getting an error - Error: Unknown property 'queryaccountrecords.accts @highfive – Ankita Singh Jun 3 '15 at 10:25
  • edited.use public access modifier – highfive Jun 3 '15 at 10:26

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.