I have a page where I'm displaying List of records of an object (Product) on selecting each record from a dropdown list (default value -----------------Select----------). My requirement is the moment I open the page (with default option of ----------Select---------- on dropdown list), I need to display all the records on my page. Currently once I open the page, it shows no records. but once I select any value from dropdown list and then click ----------Select-----------, it shows all the records. I need the same functionality when Im opening the page. In addition to this Im also trying to add 'GROUP BY' clause to my query on Category__c field which is of Text type, but its not allowing me to group it. Gives an error which says "Field must be grouped or aggregated: Id ". I tried adding ORDER BY ID but it again gave me an error "Ordered field must be grouped or aggregated: Id". Please help. Below is my code:


public class MyController{    
Public String selectedName {get;set;}
Public String newPCName{get;set;}
Public Product2 objPC{get;set;}    
Public List<Wrapper> wrapList{get;set;}
Public List<Product2> pcList{get;set;}
public Boolean flag{get;set;}

Public MyController(){
    flag =  true;
  objPC = new Product2(); 

public List<SelectOption> getProductList(){
    List<SelectOption> ProductList = new List<SelectOption>();
    ProductList.add(new SelectOption('------------Select-----------','------------Select-----------')); 
    List<Product2> prodList = [Select id, name, Category__c, FieldA__c from Product2];
    for(Product2 pc :prodList){
        ProductList.add(new SelectOption(pc.Id,pc.Name));     
   return ProducttList;    

Public void showrecord(){
    System.debug('=====selectedName=====' + selectedName);
    wrapList = new List<Wrapper>();
    if(selectedName == '------------Select-----------'){
    pcList = [select id, Name, Category__c, FieldA__c, Description__c  from Product2];

   pcList = [select id, Name, Category__c, FieldA__c, Description__c,   from Product2 where id =: selectedName];
    for(Product2 pc :pcList)
        wrapList.add(new Wrapper(false,pc));
    system.debug('inmethod'+ pcList);
Public Class Wrapper{
    Public Boolean ischeck{get;set;}
    Public Product2 prodcat{get;set;}
    Public Wrapper (Boolean ischeck, Product2 prodcat){



<apex:page controller="MyController" sidebar="false" action="{!showrecord}">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockSection title="Product" rendered="{!flag}">
            <apex:outputLabel value=""/>
            <apex:selectlist value="{!selectedName}" size="1" id="values"> 
                <apex:SelectOptions value="{!ProductList}"/>  
                <apex:actionSupport event="onchange" action="{!showrecord}" reRender="pc"/>          
            <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.FieldA__c}" width="10%"/>                    
                <apex:column value="{!v.prodcat.Description__c}" width="60%"/>  

        <apex:pageBlockSection rendered="{!NOT(flag)}">


  • n.b (not specifically on topic but given your handle of 'Salesforce Learner' you might want to think about some best practices in variable names/method names. 1) flag - should be something like isReady or something that gives meaning to the rendered= decision; 2) showRecord() should be something like showRecords(); 3) Wrapper should be something like ProductWrapper. You get the idea.
    – cropredy
    Dec 3, 2014 at 5:08
  • Thanks for your valuable inputs. Will keep this in mind. The actual names are actually different at the time of creating them in org. Dec 3, 2014 at 5:27

1 Answer 1



selectedName = '------------Select-----------';

at the end of your constructor will display the records when the page opens.

The problem you are seeing when trying to add GROUP BY is that you must apply an aggregate function to every other field returned by the query. That is because when multiple records have the same value of the field you are grouping by, you need to nominate which of the multiple values from the non-grouped fields to show. The names of the data values returned also change. See Working with SOQL Aggregate Functions too.

  • I added showrecord(); i my constructor but it didnt showed all the records as I open my page Dec 2, 2014 at 17:49
  • @SalesforceLearner Sorry about that. See changed answer.
    – Keith C
    Dec 2, 2014 at 17:51
  • Thanks alot @Keith C. It worked for showing all records :). As suggested, I added AVG to all the other fields in the query, but it gave an error which says " field Description__c does not support aggregate operator AVG". Please suggest. Dec 3, 2014 at 5:00
  • @SalesforceLearner MIN will work on strings and numbers. But picking an arbitrary value (be it AVG or MIN) like that may make the results fairly meaningless. You will also need to change the surrounding logic including using an AggregateResult and change how the page references the data.
    – Keith C
    Dec 3, 2014 at 8:37

You must log in to answer this question.

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