UPDATE: New Tool Available (January 2018)
I’ve developed a new, more robust tool, Mass Action Scheduler, that lets you declaratively schedule Process Builder, Flows, Quick Actions, Email Alerts, Workflow Rules, and Apex to process records from Reports and List Views.
This post is inspired by Tammie Silber’s question on the Success Community if it was possible to have new contacts automatically added to her campaign based on a daily report she had scheduled to save her precious time each day.
“But wait”, you say, “there’s already an ‘Add to Campaign‘ button on Contact and Lead reports.”
You are correct, there is such a button — if you want to manually add contacts and leads to your campaigns… And if you are interested in manual solutions, check out Johan Yu’s blog post Multiple Ways to Add People to Campaign.
But what if I told you you could automate the process to add contacts to your campaigns that meet your report criteria? What if I told you that report can run on a schedule, say daily, and add any new records in the report to your campaign?
This turns out to be a great use case for the Report Notifications features introduced in the Spring ’15 release. This package uses the Apex Analytics API to automate adding
contacts from a report as campaign members using report subscription notifications.
Why Use a Report?
Rather than write apex code with the specific SOQL query criteria in it and which would require redeployments or complex rules engine created to be dynamic anytime the business logic needed to change, we can leverage all the power of Salesforce reports out-of-the-box to narrow down to our desired contacts to add to the campaign. We can even leverage advanced reporting techniques like cross-object filtering or date range filters!
This is awesome because it keeps the control clearly in the administrator’s hands using declarative tools he or she is very familiar with. An added benefit of using the report subscription is that this feature can just as easily be disabled at the click of a button if the administrator ever wants to turn it off. With apex triggers, a redeployment would be necessary… yuck!
Speaking of flexibility, this package does utilize a custom setting to control which campaign a report should add contacts to as new members. How it works is when the apex code is triggered by the report subscription then it looks up the associated Campaign ID by finding the custom setting instance whose
Name value equals the Report ID. This keeps maintenance clean and declarative, and supports as many reports and campaigns as you want =)
1) Deploy these customizations to your sandbox or developer org to test out first. You can easily install these components to your org as an unmanaged package. See my github readme for the most up-to-date package URLs.
2) Create a Contact or Lead report in
Matrix or other complex report formats are not supported by this package at this time. **Technically, you should be able to report on any object so long as at least one of the columns refers to a Contact or Lead field. For example, you could create an “Opportunity with Contact Roles” report to have those contacts added to campaigns.
3) Create a Campaign that you want the contacts or leads from the report to be added to as members whenever the report runs.
4) Go to Setup | Develop | Custom Settings and click
Manage link next to Add Contact to Campaign Report Settings.
New button to add a new custom setting entry. Specify the 15 character Report ID from step 2 in the
Name field then specify the 15 character Campaign ID from step 3 in the
Campaign ID field. The simplest way to obtain these values is to go to the report page and campaign detail page, respectively, and copy the ID from the end of the URLs.
6) Go back and run your report from step 2. On the report results page, click on the
Subscribe button. It should appear just after the
Add to Campaign button. Ironically enough, this solution has nothing to do with the
Add to Campaign button — though you’re welcome to use that button to manually add contacts to your campaigns. But if you’re still interested in automating the process, keep reading because you’re almost done!
7) On the Report Subscription page, this is where you request how often you want your Contact or Lead report to run (e.g. weekdays at 3am) and when you want to be notified (e.g. when record count is greater than zero) and then how you want to be notified when those conditions are met (e.g. chatter post, email, execute a custom action). You can choose as many notification actions you want, but for our usage you must minimally choose
Execute a Custom Action and select the apex class AddContactsToCampaignReportAction.
8) To test the subscription, click the Save & Run Now button. If successful then your campaign should now include all the contacts from the report.
Automate Adding Leads to Campaigns
Despite that this post’s title and the code references say “Adding Contacts to Campaigns” this solution also supports Lead reports. You follow all the same instructions as outlined above, just know that the report you use can include Lead and/or Contact fields and work fine.
Many thanks to Peter Knolle’s examples on Salesforce Stack Exchange to help me get started learning this API quick!
Kudos to Andy Fawcett and Dominick Medley for figuring out how to construct Reports.EvaluatedCondition object so that I could write the apex unit tests!