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
cozimekcozimek 

APEX trigger to update opportunity contact role values on opportunity insert

Hi folks,

 

We're using a dev environment of the non-profit starter pack, and we're trying to fire a trigger that updates the opportunity contact role record's Role and IsPrimary values immediately upon conversion from a lead record.

 

If we understand everything correctly, we're unable to grab the value of the opportunity contact role on insert of the new opportunity. It seems like the reason for this is because when the Opportunity trigger fires upon "after insert", the opportunity contact role isn't created yet.  Therefore, our opportunity trigger can't find a related opportunity contact role, and errors out.  Interestingly, if there are other opportunity records, and we make our query generic enough, our trigger does properly update the Role and IsPrimary values...of another opportunity.

 

I've read that by unchecking the lead converstion setting (Your Name | Setup | Customize | Leads | Settings) for "Enable Validation and Triggers from Lead Convert" you can make sure that the opportunity contact role is created before any "after insert" triggers are fired, but it still doesn't seem to work.

 

Has anyone run into this problem?  How are we supposed to set a contact role on the insert of an opportunity if we can't fire a trigger to make this happen on the Opportunity object and there isn't a way to fire a trigger on the OpportunityContactRole object?

Cheers,

Ryan 

Best Answer chosen by Admin (Salesforce Developers) 
crop1645crop1645

Ryan, I can think of a couple of ways around this

 

First of all - "I've read that by unchecking the lead converstion setting (Your Name | Setup | Customize | Leads | Settings) for "Enable Validation and Triggers from Lead Convert" -- I don;t think this is true -- if the box is unchecked, no triggers or validation rules on account/contact/opportunity will fire

 

Option 1: Do the work in the after update trigger of the lead -- this has access to the converted oppo/account/contact

 

Option 2: use a future method fired by the Oppo trigger; the method won't execute until the oppo+contact+contactRole are all built. You would pass in the Opportunityid to the future method

All Answers

crop1645crop1645

Ryan, I can think of a couple of ways around this

 

First of all - "I've read that by unchecking the lead converstion setting (Your Name | Setup | Customize | Leads | Settings) for "Enable Validation and Triggers from Lead Convert" -- I don;t think this is true -- if the box is unchecked, no triggers or validation rules on account/contact/opportunity will fire

 

Option 1: Do the work in the after update trigger of the lead -- this has access to the converted oppo/account/contact

 

Option 2: use a future method fired by the Oppo trigger; the method won't execute until the oppo+contact+contactRole are all built. You would pass in the Opportunityid to the future method

This was selected as the best answer
cozimekcozimek

Eric,

 

You were absolutely right on option 1, worked just like a charm.  I just made a trigger after update on the Lead object using the Converted IDs, and then updated the OCR.  Thanks so much for that tip!

 

For reference, I thought this document was good in providing an overview on the conversion process, step by step.

 

The "ConvertLead" Lead Conversion Process of Salesforce

 

Best,

Ryan