function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Alex Li 26Alex Li 26 

Reference field from inner SOQL

sObject customObj = [SELECT field1, field2, (SELECT field3, field4 FROM customDetailsObject__r) FROM customObj WHERE Id = :currentCustomObj t.Id]; 

// Referencing field3 => error : A non foreign key field cannot be referenced in a path expression
System.debug(customObj .customDetailsObject__r.field3);

// Get customDetailsObject.Id and parent Id (customObj.Id)
System.debug(customObj .customDetailsObject__r);
I'm trying to get field of child custom object from custom parent object but I'm sure what I'm doing wrong when trying accessing it. I have used the "Child Relationship Name".

The singular and plural name are the same (no 's' added or whatsoever) so I don't know if that's the issue. 

 
Steven NsubugaSteven Nsubuga
sObject customObj = [SELECT field1, field2, (SELECT field3, field4 FROM customDetailsObject__r) FROM customObj WHERE Id = :currentCustomObj t.Id]; 

// Referencing field3 => error : A non foreign key field cannot be referenced in a path expression
System.debug(customObj.customDetailsObject__r[0].field3);

// Get customDetailsObject.Id and parent Id (customObj.Id)
System.debug(customObj.customDetailsObject__r[0]);

 
Nihar ANihar A
Hi Alex,
Usually Sub query result is returned as a list of records. So if you only have one child try using 
System.debug(customObj .customDetailsObject__r[0].field3);