function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Forcedotcom737Forcedotcom737 

Help with simple SOQL relationship queries that wont work?

Trying to understand relationship queries. Could you please review the quesries that wont work below and comment on what I am doing incorrectly?

I have 2 objects:
Festivals__c (Parent Object to Attendee)
Attendee object has two lookup fields Account & Festivals (name of festival)

These wont work

 (Trying to query Attendee Name, Tickets purchased and Festival Name (Master-Detail); going from child to parent)

SELECT Name,Tickets_Purchased__c,Festivals__r.Name FROM Attendee__c

 (Trying to Query  festival Name and attendee name - going from parent to child)

SELECT Festivals__c.Name, (SELECT Name FROM Attendee__C) FROM Festivals__c

 (Trying to Query Attendee Name, tickets purchased and lookup Account)

 SELECT Name,Tickets_Purchased__c,Account.Name FROM Attendee__c

 These statements work:

SELECT First_Name__c,Tickets_Purchased__c  FROM Attendee__c

 SELECT Festivals__c.Name FROM Festivals__c

 SELECT Name FROM Attendee__C

Sample Error Messages:

Didn't understand relationship 'Festivals__r' in field path.

Didn't understand relationship 'Attendee__C'

Didn't understand relationship 'Account' in field path
Best Answer chosen by Forcedotcom737
James LoghryJames Loghry
First off, if you haven't already, take a look at the Relationship Query documentation here: http://www.salesforce.com/us/developer/docs/dbcom_soql_sosl/Content/sforce_api_calls_soql_relationships.htm

Secondly, your queries look ok for the most part, but you just need to pay attention to the differences between the API name of the lookup field, versus the Child Relationship API name (Used by subqueries looking from the parent down to the child, you can find the Child Relationship API  name on the lookup or master detail field on the child object).

Without knowing the exact API names in your org, here's what the three queries *should* be:
  • SELECT Name,Tickets_Purchased__c,Festival__r.Name FROM Attendee__c (Removed the s from Festivals, typically the lookup to the parent is the singular version)
  • SELECT Festivals__c.Name, (SELECT Name FROM Attendees__r) FROM Festivals__c (Changed __c to __r and used the Plural version, which is common for a subquery)
  •  SELECT Name,Tickets_Purchased__c,Account__r.Name FROM Attendee__c (Account is a custom field, so any relationships will have a __r suffix)

All Answers

James LoghryJames Loghry
First off, if you haven't already, take a look at the Relationship Query documentation here: http://www.salesforce.com/us/developer/docs/dbcom_soql_sosl/Content/sforce_api_calls_soql_relationships.htm

Secondly, your queries look ok for the most part, but you just need to pay attention to the differences between the API name of the lookup field, versus the Child Relationship API name (Used by subqueries looking from the parent down to the child, you can find the Child Relationship API  name on the lookup or master detail field on the child object).

Without knowing the exact API names in your org, here's what the three queries *should* be:
  • SELECT Name,Tickets_Purchased__c,Festival__r.Name FROM Attendee__c (Removed the s from Festivals, typically the lookup to the parent is the singular version)
  • SELECT Festivals__c.Name, (SELECT Name FROM Attendees__r) FROM Festivals__c (Changed __c to __r and used the Plural version, which is common for a subquery)
  •  SELECT Name,Tickets_Purchased__c,Account__r.Name FROM Attendee__c (Account is a custom field, so any relationships will have a __r suffix)
This was selected as the best answer
Forcedotcom737Forcedotcom737
Thanks! They all work.

The parent to child queries are clear to me where you use the 'plural version'. I was able to locate this in the child relationship name field when viewing the custom lookup / master detail relationship field.

I am not clear on the child to parent query where you use the singular version (Festival__r). Where can I find the singular name / relationship name that needs to be used for child to parent type scenarios? The object name is Festivals. Could you please explain how this works?