function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
kavya mareedukavya mareedu 

Hello People!

While writing a SOQL Query if we have to group by a picklist value, how should I define it. 
Khan AnasKhan Anas (Salesforce Developers) 
Hi Kavya,

Greetings to you!

You can do it like this:
SELECT Picklist__c value, COUNT(Id) recordCount
FROM Object__c
GROUP BY Picklist__c

Please refer to the below link for more information:

https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_groupby.htm

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Ajay K DubediAjay K Dubedi
Hi Kavya,

You have two option to do this.
1- You can go with the Query-

SELECT StageName label ,StageName value
FROM Opportunity
GROUP BY StageName

2- You can go with schema class-
   Try the bellow Code and put your Custom object according to your need.  

public class MyOwnTestController {
    public static List<String> getPickListValuesIntoList(){
        List<String> pickListValuesList= new List<String>();
        Schema.DescribeFieldResult fieldResult = Opportunity.StageName.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        for( Schema.PicklistEntry pickListVal : ple){
            pickListValuesList.add(pickListVal.getLabel());
        }     
        System.debug('hello'+pickListValuesList);
        return pickListValuesList;
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi