I can query something like select myCustomField, Owner.Name from myCustomObject, and get the owner's name in the results, which works as expected in a soql query.

However, when I execute this in Apex, it returns a List<myCustomObject>, which does not contain a property for Owner.Name, so the owner value is lost.

I did notice that it does return the OwnerID. Is there any way to return the actual result of this query in Apex, including the Owner information?

  • You should be able to access owner name by List[0].Owner.Name, it doesn't returns lookup object's field by default.
    – Raul
    Apr 6 '17 at 4:56
  • @DougOler, is that my ans solves your issue then please accept so that other can find this useful Apr 6 '17 at 15:58

SOQL query returns owner information and you can retrieve owner information from this list like this way:

List<Dealer__c> lstDealer = [SELECT Dealer_Number__c, Name, OwnerId, Owner.Name, owner.type FROM Dealer__c];

    System.debug('owner name=' + lstDealer[0].Owner.Name );
    System.debug('owner type=' + lstDealer[0].Owner.Type);
    System.debug('ownerId=' + lstDealer[0].Owner.Id);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.