In a master/detail relationship, the following query returns the contact Id. How to get the contact name instead? (contact__c is a master/detail field).

Select contact__c
FROM BOG_Membership__c 
WHERE BOG_Membership_Status__c = 'Active' 
AND BOG_Membership_Type_New__c <> '' 

If it helps, if I do an execute anonymous query using contact__r.Name instead of contact__c I get the names returned. If I use the very same query in apex, I get an invalid field contact__r.Name (case doesn't seem to matter).

  • Are you sure your execute anonymous query is running against the same org as your Apex query? – Keith C Mar 20 '16 at 11:42
  • Can you provide more context on the apex code? It would be helpful to see if you are running as another user and how you are using the soql query in your apex. – zainogj Mar 20 '16 at 13:28
  • what version is your class? (Reason I'm asking is I don't recall when the OOB concatenation of Contact first name + lastname into Contact.name was introduced) – cropredy Mar 20 '16 at 15:04
  • Wait, it works in execute anonymous but not in apex? Are you sure it is the query that fails, and not some post-processing step? – Adrian Larson Mar 20 '16 at 16:45
  • Is this still an open question, or did you find a resolution? – Adrian Larson May 8 '16 at 16:34

Contact has a read-only Name field out of the box, which concatenates FirstName and LastName. So your query would look like this:

SELECT Contact__r.Name
FROM BOG_Membership__c 
WHERE BOG_Membership_Status__c = 'Active' 
AND BOG_Membership_Type_New__c <> ''

In fact, you can chain selects through multiple lookup fields, e.g. to select the name of the Account the Contact belongs to:



You say the Contact Name is returned when you run the SOQL, but when you run the Apex you get invalid field Contact__r.Name. I guess you mean that you have stored the SOQL query results in a BOG_Membership__c variable, and you now want to read the related Contact's name from it. If not, please share the exact code which is causing the Invalid field error.

If this is the case, then bear in mind the SOQL returns BOG_Membership__c records, and BOG_Membership__c really doesn't have a field called Contact__r.Name. To get to the related record, use SObject.getSObject(Schema.SObjectField) method:

//Assuming there is at least one membership record on the DB...
BOG_Membership__c m = [SELECT Id, Contact__c, Contact__r.Name FROM BOG_Membership__c LIMIT 1];
Schema.SObjectField contactField = BOG_Membership__c.Contact__c;
Contact c = (Contact)m.getSObject(contactField);

//For my org, this logs out Name: Some Guy
System.debug('Name: ' + c.Name);
  • I keep getting Invalid field Contact__r.Account.Name for BOG_Membership__c and Invalid field Contact__r.Name for BOG_Membership__c errors. What am I missing? – Apex N-u-b Mar 20 '16 at 9:09

If you are experiencing this issue when you try to get the value from your records (post-processing), you may be running into a similar issue as Unable to retieve dynamic sObject field value. You can implement the class I describe there and just do:

for (BOG_Membership__c membership : [/*query*/])
    String contactName = CrossObject.get(membership, 'Contact__r.Name');

You can also just hardcode the path:

String contactName = membership.Contact__r.Name;

If you are experiencing this issue when you perform your query, you are almost certainly experiencing an issue with Field Level Security. I was able to reproduce the behavior you describe by creating a custom object MyObject__c with a Contact lookup Contact__c, and unchecking Visible for all profiles.

To fix the problem, I clicked into the lookup field and hit the Set Field-Level Security button. Make sure Visible is checked for your profile (and anyone else who should have access). The reason the Apex does not blow up is, well, it was designed that way. The Apex Developer Guide states that:

Apex code runs in system context. In system context, Apex code has access to all objects and fields— object permissions, field-level security, sharing rules aren’t applied for the current user. This is to ensure that code won’t fail to run because of hidden fields or objects for a user. The only exceptions to this rule are Apex code that is executed with the executeAnonymous call and Chatter in Apex. executeAnonymous always executes using the full permissions of the current user. For more information on executeAnonymous, see Anonymous Blocks.


Use following query.

Select contact__r.FirstName
FROM BOG_Membership__c 
WHERE BOG_Membership_Status__c = 'Active' 
AND BOG_Membership_Type_New__c <> '' 

Note :- In contact we have two fields for Name: 1. First Name 2. Last Name

You can create a formula field for full name by concatenating First and Last Name.

  • I get the same error Invalid field Contact__r.firstname for BOG_Membership__c – Apex N-u-b Mar 20 '16 at 9:09

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.