beyond-the-cloud-dev / soql-lib

The SOQL Lib provides functional constructs for SOQL queries in Apex.
https://soql.beyondthecloud.dev/
MIT License
61 stars 9 forks source link

groupBy() does not currently support grouping by related object fields #112

Closed jessewheeler closed 4 months ago

jessewheeler commented 5 months ago

Hi! Big fan of SOQL Lib. We recently ran into a need to be able to use a GROUP BY clause with related object fields, which doesn't appear to be supported yet.

Instead of having to write:

SOQL.of(OpportunityLineItem.SObjectType)
        .count(OpportunityLineItem.Name, 'count')
        .toString() + ' GROUP BY OpportunityLineItem.Opportunity.AccountId';

We'd like to be able to follow the "relationshipName" + SObjectField pattern:

SOQL.of(OpportunityLineItem.SObjectType)
        .count(OpportunityLineItem.Name, 'count')
        .groupBy('OpportunityLineItem.Opportunity.Account', Account.Id);

I chose arbitrary SObjects for this example.

pgajek2 commented 5 months ago

Great @jessewheeler, your SOQL Lib code looks perfect! 🤩 I added a small comment about the Unit Test, please take a look when you have time. I also changed the destination branch from main to release/v3.2.3. I need to add groupBy related method to the SOQL Lib documentation before we merge it :D

jessewheeler commented 5 months ago

Sounds good, thanks @pgajek2!