function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
kim.stiteskim.stites 

Primary Campaign Source updated through integration

We are creating an opportunity integration and would like to capture the most recent campaign on the contact associated with an opportunity in the primary campaign source.  How can this be done?

jkucerajkucera

It sounds like you want to grab the Contact Roles, then find all the campaigns associated with those contacts, and then put the most recent one in Opportunity.primaryCampaignSource - is that correct?

 

The tricky part is that any time you create a new campaign, you might need to update a bunch of opportunities, and this could be a very large number.

 

The "best" way would be to make this a batch job so you don't have to worry about cases where over 200 opportunities need to be updated.

 

Depending on how often you create new campaigns, and the size of the campaigns, you might want to have this as a scheduled job that fires once / day or once / week instead of every time you create a new campaign.  If you only create campaigns once a day or less, and they tend to be large, you could call the batch job from a campaign trigger.

 

High level steps:

  1. Create batch job that gets all campaigns created in the last "X" days/hours, gets all members, gets contactID's, gets Oppty's associated with those contacts, and updates those oppty's primary campaign source with the latest CampaignID
  2. Create a schedule apex job to fire as frequently as you need

 

For a live example on how to do scheduled apex, you can install my Chatter Unfollow Rules app, which uses several scheduled jbos to call Batch jobs.  It has pretty robust redundancy in there if you want to get fancy too - it handles the case where you can't call the batch job if 5 others are already running.

 

http://appexchange.salesforce.com/reviews?listingId=a0N30000003IP2gEAG

 

If you get install errors you can ignore them as they are related to a namespace bug that only affects the unmanaged version and causes test failures.