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
jdogsailingjdogsailing 

Querying many-many Relationships

I have established a many-many relationship between Survey and Question objects using a SurveyQuestion junction object and two master-detail relationships as suggested. I'm now tearing my hair trying to query accross the relationship. I'm still an Apex SOQL noob so I must have missed something obvious but how would I:

 

- select all Questions for a Survey in a single query?

- select all Surveys for a Question in a single query?

 

Extra credit:

 

SurveyQuestion has an order_in_survey field. In the first query, I'd like the Questions ordered by it.

 

Jeff

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

select  question__r.field1, question__r.field2 from surveyQuestion where survey__c = 'some servey id' order by order_in_survey

 

select survey__r.field1 from surveyQuestion where question__c = 'some question id'