Search code examples
salesforceparent-childwhere-clausesoql

Salesforce SOQL Query to access a child field in WHERE statement


I am attempting to write a query on an object, Opportunity, this object has a child object Quotes.

In Quotes where have a field named, Order_Ready.

What I need to do is filter in all opportunities that have approved quotes (Order_Ready__c == true).

Here is the query I have been attempting to get working,

SELECT Id, Name (SELECT Order_Ready__c FROM Quotes) FROM Opportunity WHERE Opportunity.Quotes.Order_Ready__c = true

I have tried a few variations of this,

SELECT Id, Name (SELECT Order_Ready__c FROM Quotes) FROM Opportunity WHERE Quotes.Order_Ready__c = true

SELECT Id, Name (SELECT Order_Ready__c FROM Quotes) FROM Opportunity WHERE Order_Ready__c = true

I have to admit, I'm not the strongest with SQL/SOQL. Any insight into where my mistake or misunderstanding might be?

Thanks!


Solution

  • SELECT Id, Name FROM Opportunity WHERE Id IN 
    (Select OpportunityId FROM Quote WHERE Order_Ready__c = true)