You need to sign in to do that
Don't have an account?
Justin Mitchell
Help with SOQL query - inner and outer selects same object
I'm using Conga Composer (so Apex isn't an option here) and trying to create a query. It looks like this:
Explanation:
I'm trying to recreate a report that shows fields from <product_2>, but only if the parent opportunity has products on it with <product_code_1>.
I can do this with a standard report, so I assume there must be a way to do it with SOQL. Here's a screenshot of what that looks like (Sales Price is an Opportunity Product field):
SELECT <fields> FROM OpportunityLineItem WHERE OpportunityId = <id> AND OpportunityId IN (SELECT OpportunityId FROM OpportunityLineItem WHERE ProductCode = <product_code_1>) AND ProductCode = <product_code_2>When I try the above with real values I get an error:
The inner and outer selects should not be on the same object type
Explanation:
I'm trying to recreate a report that shows fields from <product_2>, but only if the parent opportunity has products on it with <product_code_1>.
I can do this with a standard report, so I assume there must be a way to do it with SOQL. Here's a screenshot of what that looks like (Sales Price is an Opportunity Product field):
Thanks for the quick response. That's not going to work though. The <fields> I have are all on the Opportunity Product (OpportunityLineItem) object, not on the Opportunity. Your query says "SELECT <fields> (...) FROM Opportunity..."
Probably solved yet but here is the solution nevertheless.
"The inner and outer selects should not be on the same object type": it is a strong constraint of SOQL.
The solution is like that:
The problem is to get all the values of (SELECT ProductCode ,OpportunityId FROM OpportunityLineItems)
We have to get these values with some apex code.
The result of this request is only the opportunities having the product codes 'GC5040' and 'GC1040' (at least).
The complete list of the product codes for these opportunities is given by: (SELECT ProductCode ,OpportunityId FROM OpportunityLineItems)
Noticed the "s" at the end (it is the name of relationship for a standard object). That would be "__r" for a custom object.
Alain