1

Please, It is possible to write a SOQL aggregate statement using fflib_SObjectSelector? For example, I have this SOQL request: SELECT DX_Support__r.Name support, SUM(total__c) total FROM DX_Turnover__c GROUP BY DX_Support__r.Name LIMIT 10.

I use FFLIB framework in my project.

Thanks in advance

4
  • Given that it is an SObject selector, I'd say not. Take a look at trailhead for some discussion over selectors (where it mentions aggregate query solutions).
    – Phil W
    Commented Nov 10, 2020 at 10:27
  • Thanks @PhilW for this link. Unfortunately, I hadn't seen nothing about the SOQL aggregate Statement. I just need to transform this SOQL request above using FFLib (Selector layer).
    – FOCOMI
    Commented Nov 10, 2020 at 14:02
  • What I'm saying is that you might need to write your own selector that doesn't implement the fflib_ISObjectSelector interface/fflib_SObjectSelector class.
    – Phil W
    Commented Nov 10, 2020 at 14:05
  • Ah Okay! Thank you
    – FOCOMI
    Commented Nov 10, 2020 at 14:07

1 Answer 1

0

The answer to this depends on whether you want to mock the response using ApexMocks or some other dependency injection scheme

No mocking of response required

Within your selector class

public virtual AggregateResult[] selectTurnoverGroupedByName(..) {
 
    return [SELECT DX_Support__r.Name support, SUM(total__c) total 
             FROM DX_Turnover__c GROUP BY DX_Support__r.Name LIMIT 10
}

That is, the selector class includes a normal Aggregate Query SOQL without using any of the fflib_QueryFactory methods as those don't support aggregate queries.

Mocking of Response required

If using ApexMocks or other dependency injection, Apex doesn't permit mocking AggregateResult, even if you use the JSON serialize/deserialize approach. So, your selector method is going to have to return a Proxy object that models AggregateResult as a Map<String,Object>. There's a Q&A on approaches to do this in this answer

You must log in to answer this question.

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