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
pierrefrazny.ax358pierrefrazny.ax358 

Didn't understand relationship 'Affliations__r'

I have 2 object:

- Contact

- Affiliation__c. There is a master-detail field called Contact__c on Affiliation which related Contact and Affiliation. (the Child Relationship Name is Affiliations)

 

 

Contact[] conts = new List<Contact>(); conts = [select Id, (select Id from Affiliations__r where Type = 'Volunteer') from Contact where ID in :contIDs ];

 

I get the following error:" Didn't understand relationship 'Affiliations__r' in FROM part of query call. 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. "

 

Any ideas?

Thanks

Pierre

 

Best Answer chosen by Admin (Salesforce Developers) 
Greg HGreg H

The relationship name in a child relationship query is not always the name of the object appended with the "__r". Unless you specify the child relationship name within setup, the value will be defaulted by Salesforce. It is the 18 character Id for the custom field definition that references the object and that Id is prefixed with the letter "R."

Since your custom object Affiliation__c has a custom field defined as the relationship back to the Contact, you'll want the 18 character Id for that Contact__c custom field definition. We all know that the Ids for custom fields are unique so let's just say that this value is "00N30000001twHcEAI". You would prepend the letter R to that value and use that in the query. The alteration to your original query would look something like this: SELECT Id, (SELECT Id FROM R00N30000001twHcEAI WHERE Type = 'Volunteer') FROM Contact WHERE Id in :contIDs

You'll need to lookup the actual relationship name value and this can be done within Eclipse using the schema browser.
-greg

All Answers

Greg HGreg H

The relationship name in a child relationship query is not always the name of the object appended with the "__r". Unless you specify the child relationship name within setup, the value will be defaulted by Salesforce. It is the 18 character Id for the custom field definition that references the object and that Id is prefixed with the letter "R."

Since your custom object Affiliation__c has a custom field defined as the relationship back to the Contact, you'll want the 18 character Id for that Contact__c custom field definition. We all know that the Ids for custom fields are unique so let's just say that this value is "00N30000001twHcEAI". You would prepend the letter R to that value and use that in the query. The alteration to your original query would look something like this: SELECT Id, (SELECT Id FROM R00N30000001twHcEAI WHERE Type = 'Volunteer') FROM Contact WHERE Id in :contIDs

You'll need to lookup the actual relationship name value and this can be done within Eclipse using the schema browser.
-greg

This was selected as the best answer
pierrefrazny.ax358pierrefrazny.ax358
Thanks Greg. It worked!
Dipesh KumarDipesh Kumar
For parent-to-child relationships, the parent object has a name for the child relationship that is unique to the parent, the plural of the child object name. For example, Account has child relationships to Assets, Cases, and Contacts among other objects, and has a relationshipName for each, Assets, Cases, and Contacts
check this link for help
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_understanding.htm