+ Start a Discussion
Money CareMoney Care 

How to Grouping in Vf page

Hi all

I have created a custom report using visual force page.but i want to grouping this report uisng vf page.can anyone help me on this job ?
<apex:page docType="html-5.0" standardController="MasterTest__c"  extensions="PartyCodeReport"  showHeader="false" sidebar="false">
<center><h1 style="text-align:center;color:blue;">  This is Party/masterCode Report! </h1></center>
<apex:pageMessages ></apex:pageMessages>
<style type="text/css" media="print">
@media print
{
#non-printable { display: none; }
#printable {
display: block;
width: 100%;
height: 100%;
}
}
</style>

<style type = "text/css">
        .colHeadr {text-align:center;}
        .colHeadrRight{
            text-align:right;
        }
        }
    </style>


       <apex:define name="body">
            
            <div style="width:800px;margin: 0px auto 10px auto;border: 1px solid black;">
    <apex:form id="tableId">
        <apex:pageBlock >
            
                    <b><apex:outputLabel value="Party/MasterCode Report Report On: "  style="font-weight: bold;color:Black;"/></b>
                     <apex:inputField value="{!account.Date__c}" style="font-weight: bold;color:red;" required="false"/>
                     
                     
                   <!--- Trial Balance Report On: <apex:input type="date" value="{!dat}"/>-->
                 
                 <apex:commandButton value="Display" action="{!displaingTable}" reRender="tableId" style="font-weight: bold;color:Black;" oncomplete="window.opener.location.refresh();" />
        <b><apex:commandLink id="printable"  onclick="JavaScript:window.print();" value="Print"/></b> <br/><br/><br/>
                
                 
               
                 
                 <apex:outputPanel rendered="{!If(account.Date__c== null,true,false)}">
                 <apex:outputLabel style="font-weight: bold;color:blue;font-size:15px;padding-left:1%" > Please enter date to fetch accurate results. </apex:outputLabel>
                 </apex:outputPanel>
                 
  </apex:pageBlock>



 <table border="1" cellpadding="1" cellspacing="0" style="width:100%;border:1px solid #000000;font-familyans-serif;font-size:20px">
                    <tr style="font-family:verdana;color:Black;font-size:12px;height=18px;width:100%" >
                    
                        <td  style="background-color: #DAFFFF" ><b><apex:outputText value="Master Code"/></b> </td>
                        <td  style="background-color: #DAFFFF" ><b><apex:outputText value="Master Name"/></b> </td>
                         <td  style="background-color: #DAFFFF" ><b><apex:outputText value="Group"/></b> </td>
                        <!---<td  style="background-color: #DAFFFF" ><b><apex:outputText value="Debit Bal."/></b></td>
                        <td  style="background-color: #DAFFFF" ><b><apex:outputText value="Credit Bal."/></b></td>
                        <td  style="background-color: #DAFFFF" ><b><apex:outputText value="Account Type"/></b></td>-->
                    </tr>
                    
                    
                    
                      <apex:repeat value="{!listAccount}" var="c" id="InvRepeat">
                        <tr border="1" style="font-family:verdana;color:Black;font-size:12px;width:100%" >
                            <!---<td ><apex:outputText value="{!c.Txn_Date__c}"/></td>--->
                            
                            
                           
                         
                            <td><apex:outputText value="{!c.Name}"/></td>
                            <td><apex:outputText value=" {!c.Master_Code__c}"/></td>
                            <td><apex:outputText value=" {!c.Group__c}"/></td>
                            
                            
                          
                            </tr>
                    </apex:repeat>
                    
                     
       
         
                      
                    
                    
                    </table>
                    </apex:form>
                      </div>
        </apex:define>


</apex:page>
public with sharing class PartyCodeReport {
    public MasterTest__c account{get;set;}
    public List<MasterTest__c> listAccount {get;set;}
   
   
    public Date dat {get;set;}
    
    public Map<Date,List<MasterTest__c>> mapOfDateWithEntry;

    public PartyCodeReport(ApexPages.StandardController controller){
    
        account = new MasterTest__c();
      
    }
     
   public void setValues(){
        listAccount  = new List<MasterTest__c>([SELECT id,Name,Group__c,Master_Code__c, Date__c
                        FROM MasterTest__c
                        order by Group__c ASC ]);
        mapOfDateWithEntry = new Map<Date,List<MasterTest__c>>();
        List<MasterTest__c> listOfEntryData;
        for(MasterTest__c entry : listAccount){
            if(entry.Date__c != null){
                if(mapOfDateWithEntry.containsKey(entry.Date__c)){
                    mapOfDateWithEntry.get(entry.Date__c).add(entry);
                }
                else{
                    listOfEntryData = new List<MasterTest__c>();
                    listOfEntryData.add(entry);
                    mapOfDateWithEntry.put(entry.Date__c,listOfEntryData);
                }
            }
        }
       
   }
   public void displaingTable(){
   
   
   try{
   
        setValues();
        
        
        
 
        if(account.Date__c!= null){
           
            system.debug('Map '+mapOfDateWithEntry);
            Set<Date> allDateSet = mapOfDateWithEntry.keySet();
            if(allDateSet.contains(account.Date__c)){
                listAccount = mapOfDateWithEntry.get(account.Date__c);
            }
            else {
                List<Date> sortedDateList = new List<Date>();
                sortedDateList.addAll(allDateSet);
                sortedDateList.sort();
                
                Boolean isAnyPastDate = false;
                Date requiredDate;
                for(Date recordDate : sortedDateList){
                    if(account.Date__c > recordDate){
                        requiredDate = recordDate;
                        isAnyPastDate = true;
                    }
                    else{
                        break;
                    }
                }
                if(isAnyPastDate){
                    listAccount = mapOfDateWithEntry.get(requiredDate);
                }
                else{
                    listAccount = new List<MasterTest__c>();
                }
                /*Date compareDate ;
                Integer count = 0;
                for(Date firstKeydate : allDateSet){
                    compareDate = firstKeydate;
                    break;
                }
                system.debug('Before Compare Date'+compareDate);
                for(Date keyDate : allDateSet){
                    if(keydate < account.DateUpdate__c && keydate > compareDate){
                        compareDate = keyDate;
                    }
                    if(account.DateUpdate__c < keyDate){
                        count ++ ;
                    }
                }
                system.debug('After Compare Date'+compareDate);
                Date requiredDate ;
                
                if(allDateSet.size() == count){
                    requiredDate = [Select DateUpdate__c from MasterCopy__c Order by DateUpdate__c Desc LIMIT 1][0].DateUpdate__c;
                }
                else {
                    requiredDate = compareDate;
                }
                listAccount = mapOfDateWithEntry.get(requiredDate);*/
            }
        }
     
    }
    
    catch(Exception e)
        {  
        //ApexPages.Message myMsg = new  ApexPages.Message(ApexPages.Severity.ERROR,'Sorry...... You have entered DUPLICATE MASTER CODE' );
           // ApexPages.addMessage(myMsg); 
         
           Apexpages.addMessage(new Apexpages.message(ApexPages.Severity.Error,' NO RECORD FOUND'));
       }
       } 
   
    
    
    
   
  
}
User-added image

 
Shashikant SharmaShashikant Sharma
Hi Money,

In order to do Grouping you have to create Map and populate it as per your need. Another way could be use of Aggreegate Query where you could use Group By for it.

Working with SOQL Aggregate Functions : 

Aggregate functions in SOQL, such as SUM() and MAX(), allow you to roll up and summarize your data in a query. For more information on aggregate functions, see ”Aggregate Functions” in the Salesforce SOQL and SOSL Reference Guide.
You can use aggregate functions without using a GROUP BY clause. For example, you could use the AVG() aggregate function to find the average Amount for all your opportunities.​


https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_agg_fns.htm

Thanks
Shashikant
Money CareMoney Care
Hi Shashikant

I need to grouping the group. As per my screenshot
group00
------------
0001       A  like this
 
Shashikant SharmaShashikant Sharma
Right now you are looping over listAccount in you VFP.

If you create a

MAP<String, List<MasterTest__c>> mapGroupToListOfAccount = new MAP<String, List<MasterTest__c>>();

And pupulate it with this query

MAP<String, List<MasterTest__c>> mapGroupToListOfAccount = new MAP<String, List<MasterTest__c>>();
listAccount  = new List<MasterTest__c>([SELECT id,Name,Group__c,Master_Code__c, Date__c
                        FROM MasterTest__c
                        order by Group__c ASC ]);

mapOfDateWithEntry = new Map<Date,List<MasterTest__c>>();
        


for(MasterTest__c entry : listAccount){
List<MasterTest__c> listOfMasterTest = new List<MasterTest__c>();
if( mapGroupToListOfAccount.contaisKey(entry.Group__c)) {
    listOfMasterTest = mapGroupToListOfAccount.get(entry.Group__c); 
}
listOfMasterTest.add( entry );
mapGroupToListOfAccount.put(entry.Group__c, listOfMasterTest ); 
}

By this you will have a MAP With Group as Key and Its data as values. Then you could loop over mapGroupToListOfAccount and fetch it items and show on VFP like grouped results. You could add styling to show heading for grouping.

Thanks
Shashikant
Money CareMoney Care
Hi shashikant

I tried the modified code ,but it si not happening
Shashikant SharmaShashikant Sharma
Please post your modified code for class and VFP.
Money CareMoney Care
Please check the modified code
 
<apex:page docType="html-5.0" standardController="MasterTest__c"  extensions="PartyCodeReport"  showHeader="false" sidebar="false">
<center><h1 style="text-align:center;color:blue;">  This is Party/masterCode Report! </h1></center>
<apex:pageMessages ></apex:pageMessages>
]
<style type="text/css" media="print">
@media print
{
#non-printable { display: none; }
#printable {
display: block;
width: 100%;
height: 100%;
}
}
</style>

<style type = "text/css">
        .colHeadr {text-align:center;}
        .colHeadrRight{
            text-align:right;
        }
        }
    </style>


       <apex:define name="body">
            
            <div style="width:800px;margin: 0px auto 10px auto;border: 1px solid black;">
    <apex:form id="tableId">
        <apex:pageBlock >
            
                    <b><apex:outputLabel value="Party/MasterCode Report Report On: "  style="font-weight: bold;color:Black;"/></b>
                     <apex:inputField value="{!account.Date__c}" style="font-weight: bold;color:red;" required="false"/>
                     
                     
                   <!--- Trial Balance Report On: <apex:input type="date" value="{!dat}"/>-->
                 
                 <apex:commandButton value="Display" action="{!displaingTable}" reRender="tableId" style="font-weight: bold;color:Black;" oncomplete="window.opener.location.refresh();" />
        <b><apex:commandLink id="printable"  onclick="JavaScript:window.print();" value="Print"/></b> <br/><br/><br/>
                
                 
               
                 
                 <apex:outputPanel rendered="{!If(account.Date__c== null,true,false)}">
                 <apex:outputLabel style="font-weight: bold;color:blue;font-size:15px;padding-left:1%" > Please enter date to fetch accurate results. </apex:outputLabel>
                 </apex:outputPanel>
                 
  </apex:pageBlock>



 <table border="1" cellpadding="1" cellspacing="0" style="width:100%;border:1px solid #000000;font-familyans-serif;font-size:20px" >
                    <tr style="font-family:verdana;color:Black;font-size:12px;height=18px;width:100%" >
                    
                        <td  style="background-color: #DAFFFF" ><b><apex:outputText value="Master Code"/></b> </td>
                        <td  style="background-color: #DAFFFF" ><b><apex:outputText value="Master Name"/></b> </td>
                         <td  style="background-color: #DAFFFF" ><b><apex:outputText value="Group"/></b> </td>
                        <!---<td  style="background-color: #DAFFFF" ><b><apex:outputText value="Debit Bal."/></b></td>
                        <td  style="background-color: #DAFFFF" ><b><apex:outputText value="Credit Bal."/></b></td>
                        <td  style="background-color: #DAFFFF" ><b><apex:outputText value="Account Type"/></b></td>-->
                    </tr>
                    
                    
                    
                      <apex:repeat value="{!listAccount}" var="c" id="InvRepeat">
                        <tr border="1" style="font-family:verdana;color:Black;font-size:12px;width:100%" >
                            <!---<td ><apex:outputText value="{!c.Txn_Date__c}"/></td>--->
                            
                            
                           
                         
                            <td><apex:outputText value="{!c.Name}"/></td>
                            <td><apex:outputText value=" {!c.Master_Code__c}"/></td>
                            <td><apex:outputText value=" {!c.Group__c}"/></td>
                            
                            
                          
                            </tr>
                    </apex:repeat>
                    
                     
       
         
                      
                    
                    
                    </table>
                    </apex:form>
                      </div>
        </apex:define>


</apex:page>
public with sharing class PartyCodeReport {
    public MasterTest__c account{get;set;}
    public List<MasterTest__c> listAccount {get;set;}
   
   
    public Date dat {get;set;}
    
List<MasterTest__c>>();
    public Map<Date,List<MasterTest__c>> mapOfDateWithEntry;



    public PartyCodeReport(ApexPages.StandardController controller){
    
        account = new MasterTest__c();
      
    }
     
   public void setValues(){
   
   
   
   

   
   MAP<String, List<MasterTest__c>> mapGroupToListOfAccount = new MAP<String, List<MasterTest__c>>();
listAccount  = new List<MasterTest__c>([SELECT id,Name,Group__c,Master_Code__c, Date__c
                        FROM MasterTest__c
                        order by Group__c ASC ]);

mapOfDateWithEntry = new Map<Date,List<MasterTest__c>>();
        


for(MasterTest__c entry : listAccount){
List<MasterTest__c> listOfMasterTest = new List<MasterTest__c>();
if( mapGroupToListOfAccount.contaisKey(entry.Group__c)) {
    listOfMasterTest = mapGroupToListOfAccount.get(entry.Group__c); 
}
listOfMasterTest.add( entry );
mapGroupToListOfAccount.put(entry.Group__c, listOfMasterTest ); 
}

   public void displaingTable(){
   
   
   try{
   
        setValues();
        
        
        
 
        if(account.Date__c!= null){
           
            system.debug('Map '+mapOfDateWithEntry);
            Set<Date> allDateSet = mapOfDateWithEntry.keySet();
            if(allDateSet.contains(account.Date__c)){
                listAccount = mapOfDateWithEntry.get(account.Date__c);
            }
            else {
                List<Date> sortedDateList = new List<Date>();
                sortedDateList.addAll(allDateSet);
                sortedDateList.sort();
                
                Boolean isAnyPastDate = false;
                Date requiredDate;
                for(Date recordDate : sortedDateList){
                    if(account.Date__c > recordDate){
                        requiredDate = recordDate;
                        isAnyPastDate = true;
                    }
                    else{
                        break;
                    }
                }
                if(isAnyPastDate){
                    listAccount = mapOfDateWithEntry.get(requiredDate);
                }
                else{
                    listAccount = new List<MasterTest__c>();
                }
                /*Date compareDate ;
                Integer count = 0;
                for(Date firstKeydate : allDateSet){
                    compareDate = firstKeydate;
                    break;
                }
                system.debug('Before Compare Date'+compareDate);
                for(Date keyDate : allDateSet){
                    if(keydate < account.DateUpdate__c && keydate > compareDate){
                        compareDate = keyDate;
                    }
                    if(account.DateUpdate__c < keyDate){
                        count ++ ;
                    }
                }
                system.debug('After Compare Date'+compareDate);
                Date requiredDate ;
                
                if(allDateSet.size() == count){
                    requiredDate = [Select DateUpdate__c from MasterCopy__c Order by DateUpdate__c Desc LIMIT 1][0].DateUpdate__c;
                }
                else {
                    requiredDate = compareDate;
                }
                listAccount = mapOfDateWithEntry.get(requiredDate);*/
            }
        }
     
    }
    
    catch(Exception e)
        {  
        //ApexPages.Message myMsg = new  ApexPages.Message(ApexPages.Severity.ERROR,'Sorry...... You have entered DUPLICATE MASTER CODE' );
           // ApexPages.addMessage(myMsg); 
         
           Apexpages.addMessage(new Apexpages.message(ApexPages.Severity.Error,' NO RECORD FOUND'));
       }
       } 
   
    
    
    
   
  
}


 
Shashikant SharmaShashikant Sharma
  1. Make mapGroupToListOfAccount a public property like you did for listAccount 
  2. Iterate over mapGroupToListOfAccount in VFP instead of listAccount
<apex:repeat value="{!listAccount}" var="c" id="InvRepeat">

So it would be something like
 
<apex:repeat value="{!mapGroupToListOfAccount}" var="groupValue" id="topRepeat">
<apex:outbputLabel value="{!groupValue}"
<apex:repeat value="{!mapGroupToListOfAccount[groupValue]}" var="c" id="InvRepeat">

    <apex:outputText value="{!c.Name}"/>
    <apex:outputText value=" {!c.Master_Code__c}"/>

</apex:repeat
</apex:repeat>