0

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;
    }    
}
}
4
  • 1
    what is the schema for 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 a group by(category__c) in the soql - read the doc on how to do aggregate queries in apex
    – cropredy
    Feb 24, 2015 at 1:41
  • Thanks for your inputs. I did tried GROUP BY clause earlier but it gives an error and doesnt allow to save the code. Error: Field must be aggregated Feb 24, 2015 at 6:22
  • I tried adding aggregate to the query but it still gave the error. Could you please help me with a sample code for the same. Feb 24, 2015 at 7:11
  • Your best course of action is to practice the SOQL using Developer Console, SFDC Workbench, or Eclipse Force.com IDE - Try out the Group BY syntax (also look at salesforce.com/us/developer/docs/soql_sosl/index.htm under the section 'Group by')
    – cropredy
    Feb 24, 2015 at 17:27

1 Answer 1

1

Step 1: use Set to remove duplicate Values

Step 2: use Category__c in where clause so that You get all values

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>();
    Set<SelectOption> ProductCatalogList1 = new Set<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){
        ProductCatalogList1.add(new SelectOption(pc.Category__c,pc.Category__c));     
    }
   ProductCatalogList.addAll(ProductCatalogList1);
    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 Category__c =: 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;
    }    
}
}

I think this might be usefull for you if i am wrong then correct me.

1
  • Thanks a lot @Atul. Will try this code and let you know. Feb 27, 2015 at 6:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .