1

I have a SOQL query that looks like this:

SELECT Id, OwnerId, IsDeleted, AboutThirtyFieldsTotal__c, ...
(SELECT Id, IsDeleted, OverFiftyFields__c, ...
 FROM Borrowers__r )
FROM Loan__c WHERE LSC__c = '0051F00000ocJMdQAM'
ORDER BY CreatedDate LIMIT 200

The entire query is about 1,500 characters long.

When I run this query directly (by selecting it in VS Code and executing it with the command pallet) I get the correct result, with columns for Borrowers__r.FirstName__c with multiple rows for each of the related records.

If I run the exact same query from within Database.query, I don't get the related data, only the data from Loan__c with no data after the last field in the outer SELECT?

Why do I get related data when I directly execute the SOQL but not when it's executed by Apex?

3
  • 2
    Typically this is as a result of the context user's permissions. How are you confirming that you don't have any related child records when it is run in apex?
    – Mark Pond
    Commented Sep 7, 2021 at 22:38
  • I have a temporary Apex file that I'm using which I have this line: System.debug(LoanController.getLoans('0051F00000ocJMdQAM', 'lsc')); I execute this by highlighting it and using "Execute Anonymous Apex" in the command palette. It is this line that doesn't have the related records. However, within getLoans I System.debug(query);, and when I highlight that query and use the command palette to "Execute SOQL," I do get the related records.
    – Chuck
    Commented Sep 7, 2021 at 22:48
  • I don't know exactly which user I'm acting as when I execute anonymous Apex, but I did check that the user I think I'm running as has "Read, Create, Edit, Delete, View All, Modify All" for both objects.
    – Chuck
    Commented Sep 7, 2021 at 22:52

1 Answer 1

6

When you debug an SObject record, the output is not necessarily going to show you everything in memory, as it coerces the complex data type into a String to render.

What you observe when you system.debug(someRecord) in this case is incomplete. If you were to system.debug(someRecord.Parent__r.Field__c, you would see these fields are in fact populated.

Alternatively, you can system.debug(JSON.serialize(someRecord)) and see a more complete representation.

2
  • ty, that did the trick. I actually confirmed I'm getting the related data by printing out LoanController.getLoans('0051F00000ocJMdQAM', 'lsc')[0].
    – Chuck
    Commented Sep 7, 2021 at 23:06
  • Thank you for the system.debug(JSON.serialize()) trick. That'll come in handy. Commented Sep 8, 2021 at 11:52

You must log in to answer this question.

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