8

We have a scenario where we have contact roles added to an opportunity. We need to know if a contact role for a contact has changed for an opportunity. E.g John doe was an executive sponsor first for an opportunity and now John doe contact role is Influencer for the same opportunity. We need to log the old contact role and new contact roles for the contact and flag a field on the contact that the contact role changed. How can we do this and if any body can share any code snippet please? Thanks Buyan

3 Answers 3

16

Well, you've probably figured out by now that Triggers aren't supported on OpportunityContactRole (OCR). And, when an OCR is CRUD'd, there is no trigger that fires on the Opportunity either.

There is an idea for this - https://success.salesforce.com/ideaview?id=08730000000BrdvAAC

I've seen three workarounds

  • Run a scheduled batch apex job that sweeps the Opportunities (or Contacts) and looks at their OCR and compares them to previously-saved OCRs (saved in another child sobject or possibly as a hidden long text Opportunity/Contact field as serialized JSON)

  • Create an 'on view' trigger for your standard Oppo page layout that compares the current OCR to some prior value of the OCR (saved as above). See http://www.soliantconsulting.com/blog/2012/08/view-triggers-salesforce-trigger-opportunity-contact-roles for the code. Advantage here is that feedback to user is near immediate, subject to only how long the future method takes to run (see code in link)

  • Redo your Oppo page as a VF page with a custom component to add/delete/change OCRs. Your VF controller can then act on the before/after values and save in the Oppo/Contact.

1
  • Thanks for your reply. Option 2 on the soliant consulting blog would solve our need. Commented Aug 19, 2014 at 10:22
2

Something that is now possible with Summer '19 is to use Change Data Capture events in conjunction with an Apex trigger on OpportunityContactRoleChangeEvent to monitor for changes.

There are some details on this in Triggers on Opportunity/Account Contact Role objects – Using Change Data Capture by Narender Singh.

2

In case anyone ends up here, and hard as it is to believe, in Winter 20, apex triggers are now supported on OpportunityContactRole, along with custom fields and layouts

Opportunity contact role customization options give you the flexibility to track and attribute revenue to roles, titles, and individuals. With custom fields and page layouts, validation rules, and Apex triggers, you can design an Opportunity Contact Role to match your specific sales and reporting processes. You can capture new data, such as titles and roles, to help your sales reps be more efficient in targeting the right contacts.

Will be interesting to see how much code can now be retired...

2
  • Hello, can you comment/edit regarding why one would prefer Apex Trigger against the object itself vs against the Object_ChangeEvent? I'm confused on when one would prefer one or the other... they seem to cover the same purpose? Commented Oct 7, 2021 at 18:11
  • There probably isn't much difference now - for years though, we were waiting for ANY triggering to be possible - specifically around triggering updates to rollups on associated Contacts. In that instance, this would probably serve same purpose, though aren't there still limits around number of change events allowed in an org? Commented Oct 7, 2021 at 18:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .