Getting started with Query.apex
Fields Selection Continued
It's also possible to include fields from a parent. The easiest way would be passing the parent name to the 'selectAllFields' method:
List accounts =
new Query('Account').
selectAllFields('Owner').
run();
This would select the Id field in Account object, as weel as all the user accessible fields in the Owner reference, which is a User object.
The statement is equivalent to:
List accounts =
[ SELECT Id, Owner.Id, Owner.Name, Owner.CreatedById ... FROM Account ];
Another way would be simply passing the parent field along with the relationship to the 'selectFields' method:
List accounts =
new Query('Account').
selectFields('Owner.Name, Owner.CreatedById').
run();
equivalent to:
List accounts =
[ SELECT Id, Owner.Name, Owner.CreatedById FROM Account ];
Meanwhile, multiple layer parent relationship is supported:
List accounts =
new Query('Contact').
selectFields('Account.Owner.Name').
run();