I’m trying to create a SOQL query that returns a list of Campaigns with Influenced Opportunties, similar to the standard campaign report with the same name. The following doesn’t seem to be returning the same values as the report:
SELECT Id,Name FROM Opportunity
where CampaignId in (SELECT CampaignId from CampaignMember where CampaignId = '??')
Any help would be greatly appreciated.
If you’re after something simple – this might be enough:
You can easily convert it to an aggregated query that’d give you totals per campaign too:
(if you’ll get a discrepancy between
Campaign.NumberOfOpportunitiesand count of Opps it’s OK. It’s because the first field is an “all time counter” and the later one is impacted by theWHERE CloseDate = THIS_MONTH)If you need whole set of data exactly as returned by the report (especially the “Responded”) it’s going to be tricky because the ERD diagram for these objects is a bit nasty.
So we could somehow start from Contact, go to Opportunities via OpportunityContactRole. And in same time from Contact go to CampaignMembers to Campaign.
You’d however have to process the results of the query to obtain the campaign names (and I don’t think subqueries are allowed to be exported with Data Loader for example).
Last but not least – if you are interested in only one Campaign or only one Opp it can be simplified (and you were quite close!)