+ Start a Discussion
jlhmitchelljlhmitchell 

Custom object with lookup field, but the relationship is one-way and can't figure out the query

This is driving me crazy. I have an object (called WebSurveyQuestion__c) with a lookup field to an object called WebSurveyAnswerType__c. I would expect to have a child relationship on WebSurveyQuestion__c down to the WebSurveyAnswerType__c, but it's the reverse (the WebSurveyAnswerType__c object is the parent).

 

I keep trying to write the query on the question object, as that's where I can limit the results:

 

Select Id, (Select Public_Question_Label__c from WebSurveyQuestions__r), Public_Question_Label__c, Answer_Type__c

From WebSurveyQuestion__c WHERE Public__c = trueAND Parent_Question__c = ''order by Question_Order__c 

 

What I can't figure out is how to get the name for the AnswerType (Answer_Type__c in the above returns the ID, and I want to return the name).

 

What am I missing?

 

Is there some some way to traverse from child to parent? What relationship name would I use (the relationship name is defined for the child on the parent object, but not the opposite)?

Best Answer chosen by Admin (Salesforce Developers) 
forecast_is_cloudyforecast_is_cloudy

In your case, WebSurveyQuestion__c is the Child and WebSurveyAnswerType__c is the Parent. I'm not sure what the 'WebSurveyQuestions__r' child relationship is in your SOQL snippet.

Please refer to the following documentation on how to access parent-child relationship data in SOQL (from either the Parent down to the Child - i.e. from WebSurveyAnswerType__c down to WebSurveyQuestion__c - or from Child up to Parent - i.e. from WebSurveyQuestion__c up to WebSurveyAnswerType__c) - http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_relationships.htm.

All Answers

osamanosaman
The nested query should extract the data from child object. In your query both parent and child are same object.
forecast_is_cloudyforecast_is_cloudy

In your case, WebSurveyQuestion__c is the Child and WebSurveyAnswerType__c is the Parent. I'm not sure what the 'WebSurveyQuestions__r' child relationship is in your SOQL snippet.

Please refer to the following documentation on how to access parent-child relationship data in SOQL (from either the Parent down to the Child - i.e. from WebSurveyAnswerType__c down to WebSurveyQuestion__c - or from Child up to Parent - i.e. from WebSurveyQuestion__c up to WebSurveyAnswerType__c) - http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_relationships.htm.

This was selected as the best answer
jlhmitchelljlhmitchell

That held the answer. I kept seeing examples that used Account and Contact, neither of which allowed me to understand the nomenclature.

 

So, I had a complexity in my query already in that I am storing all questions, but some are parents to others (the self-referencing part of the original query). What I couldn't understand was how to point to the parent. In the link you sent me to, in the section called "Understanding Relationship Names, Custom Objects, and Custom Fields", it shows how to refer to the parent using the screenshot of how it's defined in the custom field.

 

My resulting query works:

Select Id,
(Select Public_Question_Label__c, Answer_Type__r.Name, Answer_Type__r.ListDirection__c from WebSurveyQuestions__r),
Public_Question_Label__c, Parent_Question__c, Answer_Type__r.Name, Answer_Type__r.ListDirection__c From WebSurveyQuestion__c WHERE Public__c = true AND Parent_Question__c = '' order by Question_Order__c

 

Thanks for your help!!