Activity › Forums › Salesforce® Discussions › Grouping records in a report on Visualforce page
Tagged: Custom Report, Group By Clause, Reports, Salesforce Visualforce, Salesforce Visualforce Page
-
Grouping records in a report on Visualforce page
Posted by Karen on April 25, 2016 at 11:24 AMHi, using Visualforce page I have created a custom report. I want grouping this report using visualforce page. Please guide accordingly.
shariq replied 7 years, 7 months ago 4 Members · 3 Replies -
3 Replies
-
Hi karen,
Follow this BOB – link–http://bobbuzzard.blogspot.in/2011/06/grouping-records-and-children-in.html
That shows the records in grouping.
Hope this helps you.
- [adinserter block='9']
-
Hi,
Controller:
public class GroupingExampleController
{
private List<Account> allAccs {get; set;}
public List<GroupWrapper> groups {get; set;}
public String groupFieldName {get; set;}
public List<SelectOption> groupOptions {get; set;}public GroupingExampleController()
{
allAccs=[select id, Name, BillingStreet, BillingCity, BillingCountry, Type,
(select id, Name, Email, Phone from Contacts limit 5)
from Account
where Type != null
limit 10];
groupFieldName=’Type’;setupGrouping();
groupOptions=new List<SelectOption>();
groupOptions.add(new SelectOption(‘Name’, ‘Name’));
groupOptions.add(new SelectOption(‘BillingCity’, ‘BillingCity’));
groupOptions.add(new SelectOption(‘BillingCountry’, ‘BillingCountry’));
groupOptions.add(new SelectOption(‘Type’, ‘Type’));
}public PageReference regroup()
{
setupGrouping();
return null;
}private void setupGrouping()
{
Map<String, List<Account>> groupedMap=new Map<String, List<Account>>();
for (Account acc : allAccs)
{
String key=String.valueof(acc.get(groupFieldName));
if ( (null==key) || (0==key.length()) )
{
key=’Undefined’;
}
List<Account> groupedAccs=groupedMap.get(key);
if (null==groupedAccs)
{
groupedAccs=new List<Account>();
groupedMap.put(key, groupedAccs);
}groupedAccs.add(acc);
}groups=new List<GroupWrapper>();
for (String key : groupedMap.keySet())
{
GroupWrapper gr=new GroupWrapper();
groups.add(gr);
gr.accs=groupedMap.get(key);
gr.groupedVal=key;
}
}public class GroupWrapper
{
public List<Account> accs {get; set;}
public String groupedVal {get; set;}
public Integer count {get {return accs.size(); } set;}
}
}Visualforce page:-
<apex:page controller=”GroupingExampleController” tabstyle=”Account”>
<apex:form >
<apex:pageBlock >
Group By: <apex:selectList value=”{!groupFieldName}” size=”1″>
<apex:selectOptions value=”{!groupOptions}” />
</apex:selectList> <apex:commandButton value=”Go” action=”{!regroup}”/>
<table border=”0″>
<apex:repeat value=”{!Groups}” var=”group”>
<tr>
<td colspan=”3″><b>{!groupFieldName}:{!group.GroupedVal}</b> – {!group.count} records</td>
</tr>
<apex:repeat value=”{!group.accs}” var=”acc”>
<tr>
<td width=”30px”></td>
<td colspan=”2″><b>Account:</b>{!acc.Name}</td>
</tr>
<apex:repeat value=”{!acc.Contacts}” var=”cont”>
<tr>
<td width=”30px”></td>
<td width=”30px”></td>
<td><b>Contact:</b>{!cont.Name}</td>
</tr>
</apex:repeat>
</apex:repeat>
</apex:repeat>
</table>
</apex:pageBlock>
</apex:form>
</apex:page>Thanks
-
Hi,
I found this onlne –
<apex:page controller=”DisplaySectionsController” action=”{!load}” sidebar=”false”> <apex:sectionHeader title=”My Sample Display Page” subtitle=”Group by States” description=”This page shows how you can dynamically group results by field value.”/> <apex:repeat value=”{!states}” var=”state”> <apex:pageBlock title=”{!state}”> <apex:repeat value=”{!accounts}” var=”account”> <apex:outputPanel rendered=”{!IF(state=account.BillingState,true,false)}”> {!account.Name} – {!account.BillingState}<br/> </apex:outputPanel> </apex:repeat> </apex:pageBlock> </apex:repeat> </apex:page>
public with sharing class DisplaySectionsController { public List<Account> accounts {get;set;} public String[] states {get;set;} public void load() { // for demo purposes limit the states accounts = [Select ID, Name, BillingState From Account Where BillingState IN (‘CA’,’NY’,’FL’)]; // dynamically create set of unique states from query Set<String> stateSet = new Set<String>(); for (Account a : accounts) stateSet.add(a.BillingState); // convert the set into a string array states = new String[stateSet.size()]; Integer i = 0; for (String state : stateSet) { states[i] = state; i++; } } }
Hope this helps.
Log In to reply.