0

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"

8
  • does the doc state that aggregate queries are supported?
    – cropredy
    Commented Jun 30, 2020 at 13:52
  • You're right! It seems, aggregate queries are not supported yet :( Thank you!!
    – jgoge
    Commented Jul 1, 2020 at 14:57
  • what does your invocable apex class look like? It can certainly do aggregate queries; the issue is more on how it returns results to the Flow; see for example: unofficialsf.com/salesforce-lightning-flow-using-advanced-soql
    – cropredy
    Commented Jul 1, 2020 at 15:59
  • I don't have apex class. I'm running this flow from a button in a Page Layout.
    – jgoge
    Commented Jul 1, 2020 at 21:31
  • sorry - the Action icon confused me as I thought it was invocable Apex; use edit to expose the Action element and how it is configured
    – cropredy
    Commented Jul 1, 2020 at 22:06

1 Answer 1

1

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

3
  • Great! Thank you @cropredy ! Yes, I'm able to see the the result of SUM(Hours__c) in expr0 when debugging. However, when I click next on "Screen 01", then it display the message Invalid interview state, with no debug messages.
    – jgoge
    Commented Jul 5, 2020 at 4:58
  • 1
    Yes. This is where things might go haywire in Flow because Flow likes to deal with sobjectTypes where static describes can be done to get field names. AggregateResult is in effect a dynamic describe. Is there a reason you can't write your own apex invocable to return the sum in a custom apex type?
    – cropredy
    Commented Jul 5, 2020 at 5:42
  • Yes, the reason is that I don't know how to do it, haha... Ok, that makes sense! I'll figure it out. Thank you so much!
    – jgoge
    Commented Jul 28, 2020 at 13:12

You must log in to answer this question.

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