I am displaying a list of records as a drop down, which when selected displays all the related values of the selected record. Now this list has duplicate values since the related field is a picklist. My requirement is the list on VF page should display single record for duplicate values and when selected, it should display all the related results for the selected record. I tried using ORDER BY but it didn't showed the required results. Below is an example
> Field Category__c Product__c
> Electronics Computer
> Electronics Scanner
> Automobiles Car
> Automobiles Motorbike
> Clothing Dress material
In the above case, I want my page to display the list of Category__c with only one value for Electronics, Automobiles and Clothing and once selected, should display all the related products (like if I select Electronics, it should display Computer and Scanner). Currently its showing 2 Electronics and 2 Automobiles in the drop-down list and when selecting shows only one product and not both. Below is the code which I am working on.
Page
<apex:page controller="ProductCatalogController" sidebar="false" action="{!showrecord}">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection title="Business" rendered="{!flag}">
<apex:outputLabel value=""/>
<apex:selectlist value="{!selectedName}" size="1" id="values">
<apex:SelectOptions value="{!ProductCatalogList}"/>
<apex:actionSupport event="onchange" action="{!showrecord}" reRender="pc"/>
</apex:selectlist>
<apex:pageBlockTable value="{!wrapList}" var="v" id="pc" style="align :center;-moz-border-radius: 10px;-webkit-border-radius: 10px;border-radius: 10px;width: 225%;">
<apex:column value="{!v.prodcat.Category__c}" width="10%"/>
<apex:column value="{!v.prodcat.Products__c}" width="10%"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
<apex:pageBlockSection rendered="{!NOT(flag)}">
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
Controller
public class ProductCatalogController{
Public String selectedName {get;set;}
Public String newPCName{get;set;}
Public NTLWM_ProductCatalog__c objPC{get;set;}
Public List<Wrapper> wrapList{get;set;}
Public List<NTLWM_ProductCatalog__c> pcList{get;set;}
public Boolean flag{get;set;}
Public ProductCatalogController(){
flag = true;
objPC = new NTLWM_ProductCatalog__c();
selectedName = '------------All-----------';
showrecord();
}
public List<SelectOption> getProductCatalogList(){
List<SelectOption> ProductCatalogList = new List<SelectOption>();
ProductCatalogList.add(new SelectOption('------------All-----------','------------All-----------'));
List<NTLWM_ProductCatalog__c> prodList = [Select id, name, Category__c, from NTLWM_ProductCatalog__c];
for(NTLWM_ProductCatalog__c pc :prodList){
ProductCatalogList.add(new SelectOption(pc.Id,pc.Category__c));
}
return ProductCatalogList;
}
Public void showrecord(){
System.debug('=====selectedName=====' + selectedName);
wrapList = new List<Wrapper>();
if(selectedName == '------------All-----------'){
pcList = [select id, Name, Category__c, Products__c from NTLWM_ProductCatalog__c];
}
else{
pcList = [select id, Name, Category__c, Products__c from NTLWM_ProductCatalog__c where id =: selectedName];
}
for(NTLWM_ProductCatalog__c pc :pcList)
{
wrapList.add(new Wrapper(false,pc));
}
system.debug('inmethod'+ pcList);
}
Public Class Wrapper{
Public Boolean ischeck{get;set;}
Public NTLWM_ProductCatalog__c prodcat{get;set;}
Public Wrapper (Boolean ischeck, NTLWM_ProductCatalog__c prodcat){
This.ischeck=ischeck;
This.prodcat=prodcat;
}
}
}
NTLWM_ProductCatalog__c
? It looks like each row is a category and a product, so if you want only unique categories, you need to do agroup by(category__c)
in the soql - read the doc on how to do aggregate queries in apex