Our app uses a lot of dynamic SOQL, and in a client org I'm currently getting an error which I can't explain when we're performing a large query against account, including subqueries for all related lists on the page layout.
I've boiled it down to this (ignore line breaks in the string, just added them for readability):
String q = 'SELECT OwnerId,
(SELECT Name FROM Conversations__pr LIMIT 5)
FROM Account WHERE Id = \'abc123467890abc\'';
SObject queryResult = Database.query(q);
System.Debug(queryResult.getSObjects('Conversations__pr'));
Conversation__c
is an object configured by the client that has a lookup to Contact, hence it should be available via a child relationship with the suffix __pr
from Account:
AND YET!
if I run the above code via Execute Anonymous in the dev console, I get this:
Line: 10, Column: 1 System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Account.Conversations__r
My code definitely does not use 'Conversations__r'
, so I suspect there's a platform bug at play here.
Further Findings
If I change queryResult
to be an instance of Account
rather than SObject
, then queryResult.Conversations__pr
returns the data expected, but no, I can't change the code to that because this does need to be dynamic. I can't find any references to this kind of issue with getSObjects
, so am I doing something dumb that I can't see or is this a bug?