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
Jay WeerappuligeJay Weerappulige 

Aggregate values of a formula filed group by lookup filed

I am quite new to Apex. I have a object with formula number filed I want to fet SUM of that filed group by Lookup filed. It doesn't work with SOQL query. I need to write a class and return tha data and display on a community lightnning page if the login user is the contact person of the record. I have develped the community page. The problem i have to writing the Apex controller. 
Sagar Wahal 1Sagar Wahal 1
Hi Jay,

You can use GROUP BY on a lookup up field. For Example, the following query will work:

When your GROUP BY field is a standard lookup field:
Select COUNT(CaseNumber) from Case GROUP BY Contact.Id
OR:
Select COUNT(CaseNumber) from Case GROUP BY ContactId

With your GROUP BY field is a custom field:
 
Select COUNT(CaseNumber) from Case GROUP BY Product__c

Can you please post the SOQL query you are using.

Regards,
Sagar Wahal
Jay WeerappuligeJay Weerappulige
I have already tried this but failed. I think need to write a aggregate class. I am working with custom objects.
Regards,
Jay Weera