You need to sign in to do that
Don't have an account?
kim.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?
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.
How would I go about doing that query as we do create our opportunities off of contacts? How does that work exactly?
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.