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 

Contact Role Apex Trigger

I would like to create an APEX trigger that pulls the Contact Id from the contact role object and into the opportunity record.  I was in Eclipse and this doesn't seem to be possible.  Is there any way to do this?

kibitzerkibitzer

You can't trigger on OpportunityContactRole - it's a feature that's long been requested.

 

However, if you implement an After Insert trigger on opportunity, and the opportunity was created off of a contact (or conversion), the contact roles will already be defined - you can do a query at that time and pull the contact ID onto the opportunity if you wish.

 

 

kim.stiteskim.stites

How would I go about doing that query as we do create our opportunities off of contacts?  How does that work exactly?

kibitzerkibitzer

Something like this, where opportunityidlist is the list of newly created opportunities in the opportunity after insert trigger (basically trigger.new)

 

        Map<ID, OpportunityContactRole> roles = new Map<ID, OpportunityContactRole>([SELECT Id, Contact.Name, ContactId, IsPrimary, OpportunityId from OpportunityContactRole where OpportunityId in :opportunityidlist ]);

Once you have the OpportunityContactRole objects, you'll have the ContactID for each newly created opportunity.