[SalesForce] 3 levels of child to parent SOQL query from a custom child to parent

Select Name, Contact.Name ,Pension_Contract__c.Contact.Account.Name From Pension_Contract__c Where Signing_Status__c = 'Completed'

Why do I get this error ?

Is this the wrong way of querying from child to parent ?

Select Name, Contact.Name
,Pension_Contract__c.Contact__r.Account__r.Name
^ ERROR at Row:1:Column:14 Didn't understand relationship 'Contact' in field path. If you are attempting to use a custom
relationship, be sure to append the '__r' after the custom
relationship name. Please reference your WSDL or the describe call for
the appropriate names.

Best Answer

If you need to return a custom field value from parent object, simply reference the Lookup/Master-Detail field's API name and change the __c to __r.

If your lookup field API name is Contact__c, then use Contact__r.somefield

For example:

Select Name, Contact__r.Name , Contact__r.Account.Name
From Pension_Contract__c 
Where Signing_Status__c = 'Completed'
Related Topic