+ Start a Discussion
Ankit Khiwansara 10Ankit Khiwansara 10 

Unable to Use Child to Parent Relationship Query

Hello ,
I have created two lookup relationship on Contact object with Account Object. 
But i am unable to use SOQL query with one of the relationship. Can you please help me on this.
 Relationship Working with CustomAccount FieldRelationship not working with CustomContacts RelationshipUser-added imageUser-added image
Could you please help me to understand, as both the relationship from contact to account are same, why SOQL is working with other relationship and not with other one
Best Answer chosen by Ankit Khiwansara 10
GovindarajGovindaraj
Hi Ankit,

The second query should be like below,

SELECT FirstName, LastName, Parent_Account__r.Name, Parent_Account__r.Industry FROM Contact

This is becsue, API name of the field is 'Parent_Account__c' and not CustomContacts__r

Thanks,
Govindaraj.S

All Answers

GovindarajGovindaraj
Hi Ankit,

The second query should be like below,

SELECT FirstName, LastName, Parent_Account__r.Name, Parent_Account__r.Industry FROM Contact

This is becsue, API name of the field is 'Parent_Account__c' and not CustomContacts__r

Thanks,
Govindaraj.S
This was selected as the best answer
Ankit Khiwansara 10Ankit Khiwansara 10

Thanks Govind it worked. 

Could you please confirm dont we have to use relationship name in SOQL query ?

GovindarajGovindaraj
Hi Ankit,

We will, when need to query child from parent.

SELECT Name, (SELECT Email FROM CustomContacts__r) FROM Account  .....Something like this.

Please let us know, if this helps.

Thanks,
Govindaraj.S
Ankit Khiwansara 10Ankit Khiwansara 10
Thanks Govind. What i understand is when we need to query Child to Parent (Contacts to account) we have to use API name with __r as suffix whereas when we need to query parent to child (Account to contact ) we have to use relationship name.

Please confirm whether my understanding is correct
GovindarajGovindaraj
Yes. Below link will be useful to you.

https://webkul.com/blog/relationship-query-salesforce/