Search code examples
salesforcesoql

How to write multiple relation query in SOQL


I am performing SOQL query to get the contact detail of a customer my parent and child table relationship is as follow.

child_table
parent_table
grandparent_table

I am trying to get contact_name from Contact(Grand Parent) from Event(Child) and I trying something like this.

select Name, 
       Venue__r.Contact__r.Name
from Event__c;

Can anyone suggest me what I am doing wrong and what is a correct way to get it?


Solution

  • Your query syntax is fine. Make sure that your lookup names are correct. In your linked parent diagram, the object is named 'Vanue'.

    Also, it's very important that you use the local field name for each object relationship reference (__r).

    Given your example SOQL

    SELECT Name, Venue__r.Contact__r.Name FROM Event__c; 
    

    we can only assume the following details:

    1. CUSTOM OBJECT Event__c has a STANDARD FIELD 'Name' and a CUSTOM FIELD 'Venue__c'

    2. CUSTOM FIELD Event__c.Venue__c is a lookup to a PARENT or MASTER object which may or may not be named 'Venue__c', so we'll refer to it as [V].

    3. CUSTOM OBJECT [V] has a CUSTOM FIELD 'Contact__c'

    4. CUSTOM FIELD [V].Contact__c is a lookup to a PARENT or MASTER object which may or may not be named 'Contact__c ', so we'll refer to it as [C].

    5. CUSTOM OBJECT [C] has a STANDARD FIELD 'Name'