I have 2 Custom Objects, Parent and Child.

Child has a lookup relationship with Parent.

If I query Parent__c on the Child object, I get back the Parent object's Id, which is correct,

But if I query Parent__r.Name, I get the following error:

Invalid field Parent__r.Name for Child__c

So it's not getting to my Parent object at all.

As a test, I queried for a field I knew was not on the Parent object, noField__c

And I got the following error:

No such column 'noField__c' on entity 'Parent__c'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.

So for that query, it made it to my Parent object, but failed because the field is not there.

Can anyone possibly explain why the first query for Parent__r.Name might be failing before it even reaches my Parent object?

String soql = 'select id, name, Parent__r.Name from Child_c';

This sounds like a permission issue for that field or object. Have you tried running the query as anonymous apex to see if you get a different result or checking the field level and object security for the profile you're using to test?

The error will say the field doesn't exist if the user doesn't have access to it.

| improve this answer | |

Ultimately it turned out to be some issue with relationship field lookups, I'm honestly still pretty unclear on it, but the following line has fixed my issue :

String myField;
String soql = 'select id, name, Parent__r.Name from Child_c';

objs = Database.query(soql);

for (SOBject o: objs) {

    myField = (String) o.getSObject('Parent__r').get('Name'); <------ This line did the trick.

Thanks for all the advice and suggestions.

| improve this answer | |
  • Did you check the permissions? If you're presenting this on a VF page as a string instead of the field on an object it can't check the field level security for it. – Phil B May 18 '15 at 17:18
  • That makes sense, you didn't mention you were using dynamic apex in the initial question. I'm assuming your initial error was in o.get('Parent__r.name');? – ca_peterson May 18 '15 at 19:30

Probably data type of lookup relationship is not Master-Detail so make sure the relationship data type is Master-Detail.

| improve this answer | |
  • At the moment the data type is Lookup Relationship. I thought that was sufficient. – Daft May 18 '15 at 14:40
  • Changing the data type to Master-Detail had no effect. Thanks anyway. – Daft May 18 '15 at 14:47
  • @Daft Lookup Relationship is sufficient. Have you tried the query with a field that you know is on the Parent object other than "Name" just to see what happens? If you post the query itself we may be able to better help you. – dBeltowski May 18 '15 at 14:48

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.