简体   繁体   中英

salesforce soql campaigns with influenced opportunities

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:

SELECT Campaign.Name, Id, Name, Amount
FROM Opportunity
WHERE CampaignId != null AND CloseDate = THIS_MONTH

You can easily convert it to an aggregated query that'd give you totals per campaign too:

SELECT Campaign.Name, Campaign.NumberOfOpportunities, SUM(Amount), COUNT(Id)
FROM Opportunity
WHERE CampaignId != null AND CloseDate = THIS_MONTH
GROUP BY Campaign.Name, Campaign.NumberOfOpportunities

(if you'll get a discrepancy between Campaign.NumberOfOpportunities and count of Opps it's OK. It's because the first field is an "all time counter" and the later one is impacted by the WHERE 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.

Campaign Influence allows marketers to measure and report on multiple campaigns that have influenced a single opportunity.

So we could somehow start from Contact, go to Opportunities via OpportunityContactRole. And in same time from Contact go to CampaignMembers to Campaign.

SELECT Name, Email, Account.Name, 
    (SELECT HasResponded, Campaign.Name FROM CampaignMembers),
    (SELECT Opportunity.Name, Opportunity.Amount FROM OpportunityContactRoles)
FROM Contact 
LIMIT 10

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!)

SELECT Contact.Name, Opportunity.Account.Name, Opportunity.Name, Opportunity.Amount
FROM OpportunityContactRole
WHERE ContactId IN (SELECT ContactId FROM CampaignMember WHERE Campaign.Name = 'Sample Campaign')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM