Salesforce Reports provide a variety of formats and options to enable us to build reports from our data. However, sometimes we cannot achieve exactly what we need either in terms of the way the data is presented, or having a requirement to report on data that is not related in a way that can be consolidated in a report. That is when we may consider a Visualforce report. A Visualforce report is Visualforce page, that allows us to enter selection criteria and options, and use APEX to retrieve the data and present it back to us in a Visualforce page. Although this requires development skills to create and maintain, it is an option worth investigating if the standard reporting tools do not satisfy your requirements.
The following example explains how a Visualforce report can be built to report on Opportunities and the related Opportunity team. It has been kept relatively simple, and yes the same data could be extracted from a standard report, but just not presented this way, with the Opportunity Team data in the same row as each Opportunity. It also demonstrates how filters can be applied to the data.
Step 1: Create the page and controller
We need to start by creating a Visualforce page and controller. This needs to be a custom controller as this page doesn’t relate to any specific record.
We need two list of opportunities, one will be our master list that we query in the constructor and this is the list we base our filter from. The second list is the filtered opportunities, these are the ones we want displayed on our page. When the page loads, we want all the opportunities to be displayed so in the constructor we initialize this list as a copy of the list of all opportunities.[crayon-6114a90106c3a323283391/]
Our Visualforce page has a table that displays the list of Opportunities in the display Opportunities list. We put all the fields we want displayed as columns in apex:column tags within a page block table.[crayon-6114a90106c3f873677273/]
Step 2: Create the filter
To create the filter we create a dummy opportunity object that we pass from the controller to the page. We use this to capture the input values and take advantage of the Salesforce standard way of displaying fields. Doing this means we only have to specify the field we want, and Salesforce manages the type of control including any picklist values. As we are using this object to pass data back to the controller the fields need to be wrapped in apex:form tags.[crayon-6114a90106c40305946458/]
Now for the filter. We want to display only those opportunities that match the filter Opportunity dummy record that we create. To do this we loop through our master record of opportunities and compare them to each field we want from the dummy filter Opportunity. We want to check first of all that the field is not null (or an empty string) this means that nothing has been selected as the filter. If they match we add it to the list of display opportunities.[crayon-6114a90106c44578914847/]
On the Visualforce page we add action support to the filter fields, this tells the controller that we want the filter method to be called when the field changes, and then we want to rerender the table of our Opportunities.[crayon-6114a90106c45306545154/]
Step 3: Getting the related OpportunityTeamMember records
In the controller we need to query all the opportunity team member records related to the opportunities we have and create a map from Opportunity Id to a list of Opportunity Team Members.[crayon-6114a90106c46668172234/]
Then in the Visualforce page we can put another page block table within one of the columns. This page block table iterates over the list Opportunity Team Members that we get from the map using the key that is the Opportunity Id. Within this table we can add whatever columns we choose.[crayon-6114a90106c47051172405/]
Putting it all together:[crayon-6114a90106c48996520685/]
Here are the results:
In this example, the filters are used to filter on Customer Type, to only show New Customers and then to filter on where the Lead Source is Web.
In this example, no filters are used, and the entire set of Opportunities along with the Opportunity Team Members are displayed.
What Certification are you studying for now?
Focus on Force currently provides practice exams and study guides for ten certifications