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
K_TK_T 

How to get a child sobject's parent's field?

I am trying to retrieve a child sobject's parent's field, but I'm getting an invalid field error. This is the code snippet:

The account Id would be the Id of an account with contacts, of course. 
List<Sobject> sobjectList = [SELECT Id, (SELECT Owner.Name, ownerId FROM Contacts) FROM Account where Id = '001E000000nG22yIAC' ];

List<Sobject> contList = sobjectList.get(0).getSobjects('contacts');
System.debug(loggingLevel.ERROR, 'cont list: ' + contList);
for (Sobject cont : contList) {

    String name = (String)cont.get('Owner.Name');
    System.debug(loggingLevel.ERROR, ' name: ' + name);
}

Is this a known limitation of Salesforce? 

Best Answer chosen by K_T
AshwaniAshwani
It is possible. Try with this:


String name = (String)cont.get('Owner').get('Name');




All Answers

AshwaniAshwani
It is possible. Try with this:


String name = (String)cont.get('Owner').get('Name');




This was selected as the best answer
K_TK_T
it is actually this: 

String name = (String)cont.getSobject('Owner').get('Name');

Thanks!