0

This is actually a continuation of https://success.salesforce.com/answers?id=90630000000CkWa.

I'm looking to write a trigger that will run when a Lead is converted.

The trigger should create an OpportunityContactRole like this: OpportunityId: Lead.ConvertedOpportunityId ContactId: Lead.ConvertedContactId IsPrimary: TRUE

I'd greatly appreciate if you could point me in the right direction. Thanks!

2
  • are you sure you need this? when I did this on my org, SFDC automatically created an OCR with isPrimary = true between the contact and the opportunity
    – cropredy
    Commented Dec 5, 2014 at 0:50
  • Good point - I just double checked, and it looks like the Contact Role is being created. In that case, I can't figure out what the issue is here. I have a trigger that runs when an Opportunity record is created or updated, but it doesn't run when on Opps that are created by the conversion process. Here's my code: gist.github.com/anonymous/09d02f388683179319fb. Do you think you can take a look?
    – user3515
    Commented Dec 5, 2014 at 15:09

1 Answer 1

0

First off

Are you sure you need this? when I did this on my org, SFDC automatically created an OCR with isPrimary = true between the contact and the opportunity

Secondly, triggers on Account, Contact, Opportunity won't execute on Lead Conversion unless the checkbox Require Validation for Converted Leads in Setup | Customize Lead | Settings is checked. Note this is an all-or-nothing thing; if these triggers will throw validation errors, the Lead won't convert

For more great info on Lead Conversion triggers - see https://salesforce.stackexchange.com/a/3961/2602

6
  • Thanks for answering. That checkbox is already checked off. Anything else that could be preventing the trigger from running?
    – user3515
    Commented Dec 6, 2014 at 23:57
  • Your trigger has two issues: 1: there won't be any OCR when the Oppo trigger fires. The lead convert process will add the OCR after the Oppo has been inserted. 2: you have soql inside a for loop which means the trigger is not bulkified. See apex dev doc for how to bulkify a trigger
    – cropredy
    Commented Dec 7, 2014 at 2:30
  • Thanks for answering! I'll work on one issue at a time. Do you think if I switch the trigger to be 'after insert', it'll fix the issue of not having the OCRs?
    – user3515
    Commented Dec 8, 2014 at 0:00
  • see salesforce.stackexchange.com/a/3961/2602 - it will tell you that you have to write the trigger as after update on Lead, not on Opportunity - both Opportunity and Contact have to be fully created before SFDC can insert the OCR
    – cropredy
    Commented Dec 8, 2014 at 14:47
  • I see - so I need a separate trigger that does the same thing, for when leads are converted. I'll work on that.
    – user3515
    Commented Dec 8, 2014 at 15:27

You must log in to answer this question.

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