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
rpsfdcrpsfdc 

SOQL GROUP BY error - <Number> field cannot be grouped by in a query call

Hi,

 

When I execute this query, I get an error '<number field> cannot be grouped by in a query call'. My query is:

 

Select SUM(Quantity) SumQuant, CustomAccountId__c, ProductCategory__c, BillingMonth__c from MyCustomObject__c where BillingYear__c = 2010 GROUP BY CustomAccountId__c, ProductCategory__c, BillingMonth__c

 

Here, BillingMonth__c and BillingYear__c are NUMBER fields (CustomAccountId__c and ProductCategory__c are reference fields).

 

Is there a documentation on which fields can and cannot be used in the GROUP BY clause? E.g.. Can I use Formula fields as one of the parameters for Group By? Should all the fields in Group by clause be indexed (or external Ids) in the Force.com Database? Can't number fields be used to group by?

 

Any help is highly appreciated.

 

Thanks,

Ruchir

rpsfdcrpsfdc

Can anyone please help here?

shruthishruthi

Formula Fields cannot be used with the group by clause.

Use the following code in the system log to identify if the field can be grouped or not.

 

Schema.DescribeFieldResult example = CustomObject__c.CustomField__c.getDescribe();
system.debug('Can CustomField__c be groupable?'+example.groupable);

 

The above code returns a boolean - True indicates that the field can be grouped and False indicates that it cannot be grouped.