[SalesForce] GROUP BY sentence in ExecuteSOQL within a Flow builder

I realized that I can use ExecuteSOQL to run a SOQL query within a flow. It works perfect. But when I use a SUM() and Group By sentence, the flow display Invalid interview state after the screen 01, so I'm not able to see screen 02Flow with ExecuteSOQL action. The error is displayed after screen 01

*This only applies when Group By is in the query, otherwise, it works fine.

Any Ideas how to fix this?

Action Get Records looks like this:
Action

**Edit, var_query contains something like this:
"SELECT Date__c, SUM(Hours__c) FROM Revenue_Forecast__c Where Opportunity__r.IsClosed = true and (Opportunity__c='xxxx' or Project__c='yyyyy') group by Date__c"

Best Answer

ExecuteSOQL is an open source contribution to the Lightning Flow Community that is a LWC + invocable Apex class

The invocable Apex class at its heart takes the incoming SOQL string and does

List<SObject> queryResult = Database.query(soqlQuery);

and passes back the sobjects to the Flow.

If soqlQuery is an aggregate query, then the return is a list of AggregateResult objects.

AggregateResult extends SObject and the ExecuteSOQL component allows you to choose AggregateResult as the value for Object for SObjects input field in the UI. Do not use Revenue Forecast as the SOQL doesn't return Revenue Forecast

If you run in debug, you can see the field names in the AggregateResult sobject collection. Unless you use aliases in your SOQL, the SUM(Hours__c) will have field name of expr0.

You can read more about processing AggregateResult objects in the Apex doc

Related Topic