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
GailGail 

Custom Opportunity - Campaign Link

My client wants a link between an Opportunity's contact roles and associated campaigns. Campaign Influence will not work as this is not accessible in the API so I'm going to create a new object "Opportunity-Campaign Link" that will simply link the campaign to the opportunity if any contact role on the Opportunity is associated with the campaign. We're not worried about when the contact became a campaign member or anything like that. I'm going to need triggers to ensure these get added and removed as necessary and wondering if y'all could look through this and see if I've missed anything in my logic. Also, if you know of anything already written to accomplish this, that would be great to know too.

When to run the logic (the triggers):

- when contact role on opp is added, edited or deleted - add opp id to opportunity list
- when campaign member is created or deleted - query contact id for any opportunity contact roles, add opp id to opportunity list

The logic (apex class):
get list of opportunities (from the triggers)
cycle through each opportunity in the list and:
            get list of all contact roles on opportunity
            get set of all campaigns associated to contact roles on opportunity (contact-campaign set)
            get list of all campaigns associated to opp (opp-campaign list)
                        cycle through opp-campaign list - if list item not in contact-campaign set, then add opp-campaign link id to remove_from_opp list
                        cycle through contact-campaign set - if set item not found in opp-campaign list, then add campaign id and opp id to add_to_opp list
delete all opp campaigns in remove_from_opp list
create new opp-campaign link for each campaign/opp id pair in add_to_opp list
GailGail
Well, the first thing I've found out is you can't have a trigger on an Opportunity Contact Role. I found a helpful post at http://www.soliantconsulting.com/blog/2012/08/view-triggers-salesforce-trigger-opportunity-contact-roles to help me get around that problem.