+ Start a Discussion
FSamorganFSamorgan 

How to query owners for opportunities including opportunities that have split owners

I am trying to write an soql that queries owners in opportunities as well as including the opportunities that have those split owners.

For example if I want Opportunities that Angela and Bob own I write:
SELECT Id from Opportunity where OwnerId in (:AngelaId, :BobId);
but I also want to include opportunities that Angela and Bob own as split owners. How do I do this?

a bad example to try to show what I want:
SELECT Id from Opportunity where OwnerId in (:AngelaId, :BobId) or OpportunitySplit.SplitOwnerId in (:AngelaId, :BobId);
Best Answer chosen by FSamorgan
JayantJayant
Can't you query on OpportunitySplit object instead ? All opportunities would have child records in OpportunitySplit object irrespective of Opportunity itself being splitted or not.

Like - 

SELECT OpportunityId FROM OpportunitySplit WHERE SplitOwnerId IN ('BobId', 'AngelaId');

Now you can iterate over the query results to collect all OpportunityIds in a Set and use it in another query on Opportunity/other objects.

You may also make it a relationship query, if that may fulfill your use case like - 

SELECT OpportunityId, SplitAmount, SplitPercentage, Opportunity.Name, Opportunity.Amount FROM OpportunitySplit WHERE SplitOwnerId IN ('BobId', 'AngelaId');