+ Start a Discussion
salesforce1#salesforce1# 

How to Generating Reports from apex code?

Hi

 

I am new to cloud programming, i want to know how to genarate a reports from apex code, explain with an small example.

 

Regards 

jhansisridhar_2011jhansisridhar_2011

Hi Salesforce 1#,

 

Here is the example to create reports through apex,You need one controller class and two Visualforce pages.

 

 

Your controller:

 public class MyApexClass{

public Opportunity[] GetOpportunityList() {

Opportunity[] opps = [SELECT

name,

account.name,

amount,

owner.name

FROM

Opportunity

LIMIT 10

];

return opps;

}

 

public pageReference generateReport() {

return Page.secondPage;

}

}

 

Your fist page:(the button is in this page.)

 

<apex:page controller="MyClass">

<apex:form>

<apex:commandButton value="Generate Report" action="{!generateReport}"/>

</apex:form>

</apex:page>

 

 

 Your second page:(this is the report.Name this page as "secondPage")

 

<apex:page renderAs="pdf" Controller="MyClass">

<apex:datatable value="{!opportunityList}" var="opp" columnsWidth="25%,25%,25%,25%">

<apex:column value="{!opp.name}" headerValue="Name"/>

<apex:column value="{!opp.amount}" headerValue="Name"/>

<apex:column value="{!opp.account.name}" headerValue="Account Name"/>

<apex:column value="{!opp.owner.name}" headerValue="Owners Name"/>

</apex:datatable>

</apex:page>

 

Hope this post helps U,

 

 

Mark it as solution if it helps U out.

 

Regards,

Jhansi.

salesforce1#salesforce1#

Hi Jhansi

 

In second page i got an eroor  Unknown property 'String.name'.

 

can u rectify that error.

 

 

Regards