60

I've got a project where I'm looking to tweak the standard lead conversion process.

Triggers and lead conversion has always been funky in past projects so I was hoping to start the design off by reviewing the order of execution, but after a long google search I'm coming up empty handed.

Anyone out there know the specific trigger order for lead conversion?

Edit: I'm not asking about trigger execution order for any object, but all the objects created in the lead conversion process. I'd think it would be something like this:

  • Lead Before Update Triggers (isConverted = false, convertedAccountId = null, convertContactId = null, convertedOpportunityId = null)
    • Account Before Insert Triggers (not fired if merged with existing account)
    • Account After Insert Triggers (not fired if merged with existing account)
    • Contact Before Insert Triggers (not fired if merged with existing contact)
    • Contact After Insert Triggers (not fired if merged with existing contact)
    • Opportunity Before Insert Triggers (not fired if no opportunity) (OpportunityContactRoles null)
    • Opportunity After Insert Triggers (not fired if no opportunity) (OpportunityContactRoles = convertedContactId)
    • Lead After Update Triggers (isConverted = true, convertedAccountId = XXX, convertedContactId = XXX, convertedOpportunityId = XXX)
3
  • Are you looking for the Trigger order process - I found this on SF site. Is that some help?
    – Saariko
    Commented Nov 1, 2012 at 7:16
  • No, that I'm aware of, I'm looking for the order of triggers fired during lead conversion. Commented Nov 1, 2012 at 23:12
  • 2
    A way to test this would be to fire up a new Dev org, and create a trigger on each of those objects with nothing but System.Debug("Trigger on Object before/after Action"); -- then you could convert a lead and check the system log.
    – Benj
    Commented Nov 2, 2012 at 20:26

3 Answers 3

52

The usual order of execution detailed at http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_order_of_execution.htm still applies with some special considerations

The Before triggers and validation rules do not fire unless that checkbox is ticked under Customise > Lead > Settings. Note that for orgs created prior to '08 you'll need to contact support to enable this (related answer)

After triggers still always fire irrespective of the above setting

To check if a Lead has been converted and do custom lead conversion logic, the best home for such is the LeadAfter trigger, where you can check

If (trigger.isUpdate && Lead.IsConverted && !trigger.oldmap.get(lead.id).IsConverted)
//do your stuff

Here you will have access to the convertedAccountId, convertedContactId, ConvertedOpportunityId and any OpportunityContactRoles

This is also a good place to re parent any custom related list records from the converted Lead to either of Company or Contact

Just added some debug logs to triggers, and here's the order of execution : (This is with the Enforce Validations and Triggers on Lead Conversion Enabled)

Account Custom Field Mappings
Account Before                 (Fires based on lead settings)
Account After
Contact Custom Field Mappings
Contact Before                 (Fires based on lead settings)
Contact After
Opportunity Field Mappings
Opportunity Before             (Fires based on lead settings)
Opportunity After              (OCR's not available)
Lead Before
Lead After                     (OCR's available)

If the Enforce Validations and Triggers setting is turned off, none of the Before triggers fire. The After triggers however still fire.

11
  • thanks, this is a great start, but do you happen to know the order all the related objects are created in? Commented Nov 1, 2012 at 23:20
  • Cheers @Ralph, Glad to be of assistance :) Commented Nov 6, 2012 at 8:08
  • @techtrekker Do you know when AccountFeed gets created in LeadConversion
    – sfdc
    Commented May 22, 2014 at 15:48
  • 2
    It's probably worth noting here that the Account/Contact/Opportunity owner will be incorrect until after the Lead After trigger finishes (e.g. when a different owner is selected as the new record owner).
    – sfdcfox
    Commented Jun 3, 2014 at 19:14
  • To add to this, it looks like Process Builder won't see the correct Owner either even though Triggers/Order of Execution states that Process Builder happens after the after triggers run. Commented Apr 29, 2016 at 20:34
6

Strangely, if the task details are entered on the lead conversion page, then the lead before and after triggers are called at the last (Lead after event).

Even adding an error on the task trigger (task.addError) will not show any error on screen and will not stop the account, contact, opportunity records being created, but only the task record will not be created for opportunity.

So the final flow is

1.0 Account Custom Field Mappings

1.1 Account Before (Fires based on lead settings)

1.2 Account After

2.0 Contact Custom Field Mappings

2.1 Contact Before (Fires based on lead settings)

2.2 Contact After

3.0 Opportunity Field Mappings

3.1 Opportunity Before (Fires based on lead settings)

3.2 Opportunity After (OCR's not available)

4.0 Lead Before

4.1 Lead After (OCR's available)

5.0 Commit to database

6.0 Task Before

6.1 Task After

7.0 Commit to database (task)

0

Below is rock solid method to check if the lead was converted.

 private static Boolean checkIfNewConversion(Lead record){
        if(record.ConvertedOpportunityId != null
           && record.ConvertedAccountId != null && record.ConvertedDate != null){
               Lead oldLead = OldMap.get(record.Id);
               if(oldLead.ConvertedOpportunityId == null
                  && oldLead.ConvertedAccountId == null && oldLead.ConvertedDate == null){
                      return true;
                  }

           }
        return false;
    }
3
  • the convertedOpportunityId check might be overkill since in some orgs you don't have to convert to an opportunity Commented Nov 26, 2019 at 22:11
  • 1
    yea your right, that can be replaced with isConverted boolean flag
    – Remario
    Commented Nov 26, 2019 at 22:17
  • The following adds a strict condition for converted leads. On the premise that the converted status must be true and the previous value is null. Along with the converted account not being null either.
    – Remario
    Commented Aug 18, 2020 at 20:01

You must log in to answer this question.

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