+ Start a Discussion
SFDC_2706SFDC_2706 

Need to group repeated records in VF Page..!!

Hi All,

I have a VF page which shows all the products and its related prices and other fields as well. But in the VF page, products are coming many times(duplication is not an issue) and i need to group all the products with similar name like we do in the Reports standard functionality. Suppose there are 10 records for the same product "Study", it should only show one study value with other records being grouped.

Below is the VF Page :
<apex:page tabStyle="Apttus_Proposal__Proposal__c"  standardController="Apttus_Proposal__Proposal__c"
                                                   extensions="DiscountReportExtension" sidebar="false">
  
    <apex:form >
     <apex:pageBlock title="Discount Sheet" id="pg1">
       <apex:pageBlockButtons location="Top">          
            <apex:actionFunction name="exportToExcel" action="{!exportToExcel}" rerender="refresh" status="myStatus" onComplete="window.close();"/>
             <input type="button" value="Export To Excel" onclick="exportToExcel();" />
       </apex:pageBlockButtons>
     </apex:pageBlock>
     
    <apex:pageBlock >
    <apex:pageBlockTable value="{!PLIlist}" var="pli">      
        <apex:column headerValue="Client Service Name" value="{!pli.APTPS_Client_Service_Name__c}"/>
        <apex:column headerValue="Product name" value="{!pli.Apttus_Config2__ProductId__c}"/>
        <apex:column headerValue="Option" value="{!pli.Apttus_Config2__OptionId__r.name}"/>        
        <apex:column headerValue="Product code" value="{!pli.Apttus_Config2__ProductId__r.ProductCode}"/>
        <apex:column headerValue="Region" value="{!pli.Apttus_Config2__ChargeType__c}"/>
        <apex:column headerValue="List Price" value="{!pli.Apttus_Config2__ListPrice__c}"/>
        <apex:column headerValue="Unit Net Price" value="{!pli.APTS_Unit_Net_Price__c}"/>
        <apex:column headerValue="Standard Price" value="{!pli.Adj_Amount_From_Price_List_Item__c}"/>
        <apex:column headerValue="Standard Price - List Price" value="{!pli.Diff_b_w_LP_Adj_Amt__c}"/>
        <apex:column headerValue="Standard Price - Unit Net Price" value="{!pli.Diff_b_w_UNP_Adj_Amt__c}"/>
        <apex:column headerValue="(Standard Price - List Price)%" value="{!pli.Diff_b_w_LP_Adj_Amt_in__c}"/>
        <apex:column headerValue="(Standard Price - Unit Net Price)%" value="{!pli.Diff_b_w_UNP_Adj_Amt_in__c}"/>      
     </apex:pageBlockTable> 
     </apex:pageBlock>
    
    </apex:form>    
</apex:page>

Below is the Apex class :
public class DiscountReportExtension {
    public Apttus_Proposal__Proposal__c prop {get;set;}
    public Static Transient List<Apttus_Config2__LineItem__c> PLIlist{get; set;}
    public Id propId;
    
    
    Public DiscountReportExtension(ApexPages.StandardController stdController){               
        this.prop = (Apttus_Proposal__Proposal__c)stdController.getRecord();
        propId = prop.Id;    
               
        PLIlist = new List<Apttus_Config2__LineItem__c>([select id,name,APTPS_Client_Service_Name__c,Apttus_Config2__OptionId__r.name,Apttus_Config2__AttributeValueId__r.APTS_Specimen_Type__c,             Apttus_Config2__ProductId__r.ProductCode,Apttus_Config2__ProductId__r.APTS_Lab_Name__c,Apttus_Config2__ProductId__r.APTS_Methodology__c,Apttus_Config2__ProductId__r.APTS_Analyser_Type__c,
                  Apttus_Config2__AttributeValueId__r.APTS_Transport_Temperature__c,Apttus_Config2__ProductId__r.APTS_Stability_Temperature__c, Apttus_Config2__ProductId__r.APTS_Stability__c,
                  Apttus_Config2__AttributeValueId__r.APTS_Collection_Container__c,Apttus_Config2__ProductId__r.APTS_Optimum_Sample_Volume__c, Apttus_Config2__ProductId__r.APTS_Minimum_Sample_Volume__c,                Apttus_Config2__ProductId__r.APTS_Lab_Code__c,Apttus_Config2__ProductId__r.APTS_Lab_Turnaround_Time__c,APTS_Unit_Net_Price__c,Apttus_Config2__ListPrice__c,Adj_Amount_From_Price_List_Item__c,                Apttus_Config2__ChargeType__c,Diff_b_w_LP_Adj_Amt__c,Diff_b_w_LP_Adj_Amt_in__c,Diff_b_w_UNP_Adj_Amt_in__c,Diff_b_w_UNP_Adj_Amt__c,Apttus_Config2__ProductId__c from 
Apttus_Config2__LineItem__c where Apttus_Config2__ConfigurationId__r.Apttus_Config2__Status__c = 'Finalized' AND Apttus_Config2__Quantity__c != 0 AND Apttus_Config2__Quantity__c != null AND  
Apttus_Config2__LineItem__c.Apttus_Config2__ConfigurationId__r.Apttus_QPConfig__Proposald__c =: propId ORDER BY Apttus_Config2__ProductId__c]);     
        
      }
    
    public PageReference exportToExcel() 
    {
        PageReference pg = new PageReference('/apex/ExportDiscountReport?propId='+propId);
        pg.setRedirect(true);
        return pg;
    }
 }

I tried it using Aggregate query but couldn't succeed. Any help would be greately appreciated. Thanks in advance. 
Best Answer chosen by SFDC_2706
Amritesh SahuAmritesh Sahu
Hi,

You need to group it map, Product Name as Key and List of records as Value.
I have modified the code to match your requirement.
VF:
<apex:page tabStyle="Apttus_Proposal__Proposal__c"  standardController="Apttus_Proposal__Proposal__c"  extensions="DiscountReportExtension" sidebar="false">
  
    <apex:form >
     <apex:pageBlock title="Discount Sheet" id="pg1">
       <apex:pageBlockButtons location="Top">          
            <apex:actionFunction name="exportToExcel" action="{!exportToExcel}" rerender="refresh" status="myStatus" onComplete="window.close();"/>
             <input type="button" value="Export To Excel" onclick="exportToExcel();" />
       </apex:pageBlockButtons>
     </apex:pageBlock>
     
    <apex:pageBlock >
    <!--apex:pageBlockTable value="{!PLIlist}" var="pli">      
        <apex:column headerValue="Client Service Name" value="{!pli.APTPS_Client_Service_Name__c}"/>
        <apex:column headerValue="Product name" value="{!pli.Apttus_Config2__ProductId__c}"/>
        <apex:column headerValue="Option" value="{!pli.Apttus_Config2__OptionId__r.name}"/>        
        <apex:column headerValue="Product code" value="{!pli.Apttus_Config2__ProductId__r.ProductCode}"/>
        <apex:column headerValue="Region" value="{!pli.Apttus_Config2__ChargeType__c}"/>
        <apex:column headerValue="List Price" value="{!pli.Apttus_Config2__ListPrice__c}"/>
        <apex:column headerValue="Unit Net Price" value="{!pli.APTS_Unit_Net_Price__c}"/>
        <apex:column headerValue="Standard Price" value="{!pli.Adj_Amount_From_Price_List_Item__c}"/>
        <apex:column headerValue="Standard Price - List Price" value="{!pli.Diff_b_w_LP_Adj_Amt__c}"/>
        <apex:column headerValue="Standard Price - Unit Net Price" value="{!pli.Diff_b_w_UNP_Adj_Amt__c}"/>
        <apex:column headerValue="(Standard Price - List Price)%" value="{!pli.Diff_b_w_LP_Adj_Amt_in__c}"/>
        <apex:column headerValue="(Standard Price - Unit Net Price)%" value="{!pli.Diff_b_w_UNP_Adj_Amt_in__c}"/>      
     </apex:pageBlockTable-->      
	 
	 <apex:repeat value="{!mapProduct2List}" var="prodName">
		<apex:pageBlockSection title="{!prodName}" columns="1">
			<apex:pageBlockTable value="{!mapProduct2List[prodName]}" var="pli">      
				<apex:column headerValue="Client Service Name" value="{!pli.APTPS_Client_Service_Name__c}"/>
				<apex:column headerValue="Product name" value="{!pli.Apttus_Config2__ProductId__c}"/>
				<apex:column headerValue="Option" value="{!pli.Apttus_Config2__OptionId__r.name}"/>        
				<apex:column headerValue="Product code" value="{!pli.Apttus_Config2__ProductId__r.ProductCode}"/>
				<apex:column headerValue="Region" value="{!pli.Apttus_Config2__ChargeType__c}"/>
				<apex:column headerValue="List Price" value="{!pli.Apttus_Config2__ListPrice__c}"/>
				<apex:column headerValue="Unit Net Price" value="{!pli.APTS_Unit_Net_Price__c}"/>
				<apex:column headerValue="Standard Price" value="{!pli.Adj_Amount_From_Price_List_Item__c}"/>
				<apex:column headerValue="Standard Price - List Price" value="{!pli.Diff_b_w_LP_Adj_Amt__c}"/>
				<apex:column headerValue="Standard Price - Unit Net Price" value="{!pli.Diff_b_w_UNP_Adj_Amt__c}"/>
				<apex:column headerValue="(Standard Price - List Price)%" value="{!pli.Diff_b_w_LP_Adj_Amt_in__c}"/>
				<apex:column headerValue="(Standard Price - Unit Net Price)%" value="{!pli.Diff_b_w_UNP_Adj_Amt_in__c}"/>      
			</apex:pageBlockTable> 
		</apex:pageBlockSection>
	 </apex:repeat>
    </apex:pageBlock>
    </apex:form>    
</apex:page>

Controller:
public class DiscountReportExtension {
    public Apttus_Proposal__Proposal__c prop {get;set;}
    //public Static Transient List<Apttus_Config2__LineItem__c> PLIlist{get; set;}	
    public Id propId;
	
	public Static Transient Map<String,List<Apttus_Config2__LineItem__c>> mapProduct2List{get; set;} //group same product name in map
    
    
    Public DiscountReportExtension(ApexPages.StandardController stdController){               
        this.prop = (Apttus_Proposal__Proposal__c)stdController.getRecord();
        propId = prop.Id;    
               
        List<Apttus_Config2__LineItem__c> PLIlist = new List<Apttus_Config2__LineItem__c>([select id,name,APTPS_Client_Service_Name__c,Apttus_Config2__OptionId__r.name,Apttus_Config2__AttributeValueId__r.APTS_Specimen_Type__c,             Apttus_Config2__ProductId__r.ProductCode,Apttus_Config2__ProductId__r.APTS_Lab_Name__c,Apttus_Config2__ProductId__r.APTS_Methodology__c,Apttus_Config2__ProductId__r.APTS_Analyser_Type__c,
                  Apttus_Config2__AttributeValueId__r.APTS_Transport_Temperature__c,Apttus_Config2__ProductId__r.APTS_Stability_Temperature__c, Apttus_Config2__ProductId__r.APTS_Stability__c,
                  Apttus_Config2__AttributeValueId__r.APTS_Collection_Container__c,Apttus_Config2__ProductId__r.APTS_Optimum_Sample_Volume__c, Apttus_Config2__ProductId__r.APTS_Minimum_Sample_Volume__c,                Apttus_Config2__ProductId__r.APTS_Lab_Code__c,Apttus_Config2__ProductId__r.APTS_Lab_Turnaround_Time__c,APTS_Unit_Net_Price__c,Apttus_Config2__ListPrice__c,Adj_Amount_From_Price_List_Item__c,                Apttus_Config2__ChargeType__c,Diff_b_w_LP_Adj_Amt__c,Diff_b_w_LP_Adj_Amt_in__c,Diff_b_w_UNP_Adj_Amt_in__c,Diff_b_w_UNP_Adj_Amt__c,Apttus_Config2__ProductId__c from 
Apttus_Config2__LineItem__c where Apttus_Config2__ConfigurationId__r.Apttus_Config2__Status__c = 'Finalized' AND Apttus_Config2__Quantity__c != 0 AND Apttus_Config2__Quantity__c != null AND  
Apttus_Config2__LineItem__c.Apttus_Config2__ConfigurationId__r.Apttus_QPConfig__Proposald__c =: propId ORDER BY Apttus_Config2__ProductId__c]);     
        
		for(Apttus_Config2__LineItem__c objRecord : PLIlist)
		{
			if(!mapProduct2List.containsKey(objRecord.PRODUCTNAMEFIELDAPI))
				mapProduct2List.put(objRecord.PRODUCTNAMEFIELDAPI, new List<Apttus_Config2__LineItem__c>);
			
			mapProduct2List.get(objRecord.PRODUCTNAMEFIELDAPI).add(objRecord);
		}
		
      }
	  	  
    
    public PageReference exportToExcel() 
    {
        PageReference pg = new PageReference('/apex/ExportDiscountReport?propId='+propId);
        pg.setRedirect(true);
        return pg;
    }
 }

Please change the PRODUCTNAMEFIELDAPI in controller to the Product Field with whom you want to group.

Hope this helps !!

Regards,
Amritesh

All Answers

Amritesh SahuAmritesh Sahu
Hi,

You need to group it map, Product Name as Key and List of records as Value.
I have modified the code to match your requirement.
VF:
<apex:page tabStyle="Apttus_Proposal__Proposal__c"  standardController="Apttus_Proposal__Proposal__c"  extensions="DiscountReportExtension" sidebar="false">
  
    <apex:form >
     <apex:pageBlock title="Discount Sheet" id="pg1">
       <apex:pageBlockButtons location="Top">          
            <apex:actionFunction name="exportToExcel" action="{!exportToExcel}" rerender="refresh" status="myStatus" onComplete="window.close();"/>
             <input type="button" value="Export To Excel" onclick="exportToExcel();" />
       </apex:pageBlockButtons>
     </apex:pageBlock>
     
    <apex:pageBlock >
    <!--apex:pageBlockTable value="{!PLIlist}" var="pli">      
        <apex:column headerValue="Client Service Name" value="{!pli.APTPS_Client_Service_Name__c}"/>
        <apex:column headerValue="Product name" value="{!pli.Apttus_Config2__ProductId__c}"/>
        <apex:column headerValue="Option" value="{!pli.Apttus_Config2__OptionId__r.name}"/>        
        <apex:column headerValue="Product code" value="{!pli.Apttus_Config2__ProductId__r.ProductCode}"/>
        <apex:column headerValue="Region" value="{!pli.Apttus_Config2__ChargeType__c}"/>
        <apex:column headerValue="List Price" value="{!pli.Apttus_Config2__ListPrice__c}"/>
        <apex:column headerValue="Unit Net Price" value="{!pli.APTS_Unit_Net_Price__c}"/>
        <apex:column headerValue="Standard Price" value="{!pli.Adj_Amount_From_Price_List_Item__c}"/>
        <apex:column headerValue="Standard Price - List Price" value="{!pli.Diff_b_w_LP_Adj_Amt__c}"/>
        <apex:column headerValue="Standard Price - Unit Net Price" value="{!pli.Diff_b_w_UNP_Adj_Amt__c}"/>
        <apex:column headerValue="(Standard Price - List Price)%" value="{!pli.Diff_b_w_LP_Adj_Amt_in__c}"/>
        <apex:column headerValue="(Standard Price - Unit Net Price)%" value="{!pli.Diff_b_w_UNP_Adj_Amt_in__c}"/>      
     </apex:pageBlockTable-->      
	 
	 <apex:repeat value="{!mapProduct2List}" var="prodName">
		<apex:pageBlockSection title="{!prodName}" columns="1">
			<apex:pageBlockTable value="{!mapProduct2List[prodName]}" var="pli">      
				<apex:column headerValue="Client Service Name" value="{!pli.APTPS_Client_Service_Name__c}"/>
				<apex:column headerValue="Product name" value="{!pli.Apttus_Config2__ProductId__c}"/>
				<apex:column headerValue="Option" value="{!pli.Apttus_Config2__OptionId__r.name}"/>        
				<apex:column headerValue="Product code" value="{!pli.Apttus_Config2__ProductId__r.ProductCode}"/>
				<apex:column headerValue="Region" value="{!pli.Apttus_Config2__ChargeType__c}"/>
				<apex:column headerValue="List Price" value="{!pli.Apttus_Config2__ListPrice__c}"/>
				<apex:column headerValue="Unit Net Price" value="{!pli.APTS_Unit_Net_Price__c}"/>
				<apex:column headerValue="Standard Price" value="{!pli.Adj_Amount_From_Price_List_Item__c}"/>
				<apex:column headerValue="Standard Price - List Price" value="{!pli.Diff_b_w_LP_Adj_Amt__c}"/>
				<apex:column headerValue="Standard Price - Unit Net Price" value="{!pli.Diff_b_w_UNP_Adj_Amt__c}"/>
				<apex:column headerValue="(Standard Price - List Price)%" value="{!pli.Diff_b_w_LP_Adj_Amt_in__c}"/>
				<apex:column headerValue="(Standard Price - Unit Net Price)%" value="{!pli.Diff_b_w_UNP_Adj_Amt_in__c}"/>      
			</apex:pageBlockTable> 
		</apex:pageBlockSection>
	 </apex:repeat>
    </apex:pageBlock>
    </apex:form>    
</apex:page>

Controller:
public class DiscountReportExtension {
    public Apttus_Proposal__Proposal__c prop {get;set;}
    //public Static Transient List<Apttus_Config2__LineItem__c> PLIlist{get; set;}	
    public Id propId;
	
	public Static Transient Map<String,List<Apttus_Config2__LineItem__c>> mapProduct2List{get; set;} //group same product name in map
    
    
    Public DiscountReportExtension(ApexPages.StandardController stdController){               
        this.prop = (Apttus_Proposal__Proposal__c)stdController.getRecord();
        propId = prop.Id;    
               
        List<Apttus_Config2__LineItem__c> PLIlist = new List<Apttus_Config2__LineItem__c>([select id,name,APTPS_Client_Service_Name__c,Apttus_Config2__OptionId__r.name,Apttus_Config2__AttributeValueId__r.APTS_Specimen_Type__c,             Apttus_Config2__ProductId__r.ProductCode,Apttus_Config2__ProductId__r.APTS_Lab_Name__c,Apttus_Config2__ProductId__r.APTS_Methodology__c,Apttus_Config2__ProductId__r.APTS_Analyser_Type__c,
                  Apttus_Config2__AttributeValueId__r.APTS_Transport_Temperature__c,Apttus_Config2__ProductId__r.APTS_Stability_Temperature__c, Apttus_Config2__ProductId__r.APTS_Stability__c,
                  Apttus_Config2__AttributeValueId__r.APTS_Collection_Container__c,Apttus_Config2__ProductId__r.APTS_Optimum_Sample_Volume__c, Apttus_Config2__ProductId__r.APTS_Minimum_Sample_Volume__c,                Apttus_Config2__ProductId__r.APTS_Lab_Code__c,Apttus_Config2__ProductId__r.APTS_Lab_Turnaround_Time__c,APTS_Unit_Net_Price__c,Apttus_Config2__ListPrice__c,Adj_Amount_From_Price_List_Item__c,                Apttus_Config2__ChargeType__c,Diff_b_w_LP_Adj_Amt__c,Diff_b_w_LP_Adj_Amt_in__c,Diff_b_w_UNP_Adj_Amt_in__c,Diff_b_w_UNP_Adj_Amt__c,Apttus_Config2__ProductId__c from 
Apttus_Config2__LineItem__c where Apttus_Config2__ConfigurationId__r.Apttus_Config2__Status__c = 'Finalized' AND Apttus_Config2__Quantity__c != 0 AND Apttus_Config2__Quantity__c != null AND  
Apttus_Config2__LineItem__c.Apttus_Config2__ConfigurationId__r.Apttus_QPConfig__Proposald__c =: propId ORDER BY Apttus_Config2__ProductId__c]);     
        
		for(Apttus_Config2__LineItem__c objRecord : PLIlist)
		{
			if(!mapProduct2List.containsKey(objRecord.PRODUCTNAMEFIELDAPI))
				mapProduct2List.put(objRecord.PRODUCTNAMEFIELDAPI, new List<Apttus_Config2__LineItem__c>);
			
			mapProduct2List.get(objRecord.PRODUCTNAMEFIELDAPI).add(objRecord);
		}
		
      }
	  	  
    
    public PageReference exportToExcel() 
    {
        PageReference pg = new PageReference('/apex/ExportDiscountReport?propId='+propId);
        pg.setRedirect(true);
        return pg;
    }
 }

Please change the PRODUCTNAMEFIELDAPI in controller to the Product Field with whom you want to group.

Hope this helps !!

Regards,
Amritesh
This was selected as the best answer
Amritesh SahuAmritesh Sahu
Please add below line
mapProduct2List = new Map<String,List<Apttus_Config2__LineItem__c>>();
before the line
for(Apttus_Config2__LineItem__c objRecord : PLIlist)
SFDC_2706SFDC_2706
Thanks amritesh for your kind support and swift response.