3

I have a Junction object for which I'm trying retrieve records in a subquery from one of it's parents and strangely the query returns child records when executing the query in SOQL Query Editor but it returns blank if executed via Apes. Has anyone come across this before? It's the same user running the query both times.

SOQL query executed in QUERY Editor (which returns Partner Relations)

Select Id, Name, (SELECT ID, Request__c FROM Partner_Relations__r) FROM Request__c

The equivalent executed in Apex

System.debug('Hello what values ' + [Select Id, Name, (SELECT ID, Request__c FROM Partner_Relations__r) FROM Request__c]);

The Apex query returns

Request__c:{Id=a1R1q000001BkHoEAK, Name=R-0003238, RecordTypeId=0125I000000pEmgQAE, CurrencyIsoCode=USD}, Request__c:{Id=a1R1q000001BkHuEAK, Name=R-0003240, RecordTypeId=0125I000000pEmhQAE, CurrencyIsoCode=USD})

I haven't even queried RecordTypeId or CurrencyISOCode :)

Does anyone know why the child records would not return in Apex despite returning in a general SOQL Editor?

The above Apex is just a simplified example as run in Execute Anonymous. The full query was returning blank child records in a function I had also. As the same user runs both I was a bit confused :)

Thanks in advance for any pointers on why this may be occurring.

1 Answer 1

4

System.debug is not printing out to log children's records. if you want to debug it, you need explicitly iterate children.

List<Request__c> requests = [
    Select Id, Name, (SELECT ID, Request__c FROM Partner_Relations__r)
    FROM Request__c
];

for(Request__c req :requests){
    for(Partner_Relation__c partnerRelation : req.Partner_Relations__r){
        System.debug('partnerRelation: ' + partnerRelation);
    }
}

Id always, RecordTypeId, if there is any record type on sobject and CurrencyISOCode, if you have multicurrency enabled, are always automatically added to any SOQL query.

2
  • +1 Also, JSON.serialize, etc can expose this additional data.
    – sfdcfox
    Nov 25, 2021 at 14:19
  • Thanks all. Appreciate your help as always.
    – RedQueries
    Nov 25, 2021 at 16:10

You must log in to answer this question.

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