I have a client with an existing Lead trigger (written by someone else) that creates campaign members when the lead is inserted.

Client now wants additional functionality in the trigger, specifically: convert all Leads when inserted, but make sure the campaign member links continue to the new contacts.

My question: if I add my convert code to the end of the trigger, after the campaign members have been inserted, will the campaign member records get transferred to the new contacts?

That would be nice. Otherwise I'll have to make my convert code a future method, and I'd like to avoid that.

Thanks David

2 Answers 2


If you convert a Lead using the UI then any CampaignMember records that refer to that Lead refer to the Contact instead.

Using Database.convertLead() results in exactly the same behavior.

The only thing to keep in mind is that you cannot convert Lead records until after you have inserted them, so the call to Database.convertLead() will have to be in an after insert trigger.

Take a look at the LeadConvert Class documentation for more information on how you can control the Lead Convertion.

Here is a tested example of doing it in an after insert trigger.

trigger CampaignMemberConvert on Lead (after insert) 
    List<CampaignMember> membersToInsert = new List<CampaignMember>();
    List<Database.LeadConvert> leadsToConvert = new List<Database.LeadConvert>();
    LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted = true LIMIT 1];

    for(Lead lead : Trigger.new)
        CampaignMember member = new CampaignMember
            // Hardcoding IDs is bad, but this is just an example
            CampaignId = '701b0000000T1KW', 
            LeadId = lead.Id


        Database.LeadConvert leadConvert = new Database.LeadConvert();


    insert membersToInsert;

    Database.LeadConvertResult[] leadConvertResults = Database.convertLead(leadsToConvert, false);
    // Do something with the results

It turns out the client's existing trigger is deprecated and they are using Marketo to create leads with campaign membership, so I'll have to use Batch Apex to convert the leads instead of a trigger.

It would still be interesting to know if you can create lead campaign members and then convert the lead (and have the campaign membership carry over to the contact), all within a single afterinsert trigger.

  • Does my answer not cover that? After the Leads have been inserted, create the CampaignMember records and then use Database.convertLead() and the CampaignMembers will point to the new Contact records. Alternatively you can create CampaignMember records after Database.convertLead() using the ContactId field of the LeadConvertResult objects returned. If you need any clarification then let me know. Feb 12, 2014 at 23:40
  • My question is: since this is all happening in a single trigger context, have the campaign members been committed to the database so that the follow-on lead conversion is aware of them? It sounds like you are saying yes. Feb 13, 2014 at 23:47
  • Yes, that's correct. I've updated my answer with a tested example of doing this in an after insert trigger. Feb 14, 2014 at 9:17
  • There is more information on the order of execution here which may help salesforce.com/us/developer/docs/apexcode/Content/…, when the DML operations are commited to the database in step 17 they are executed sequentially, when you think about it this makes sense as otherwise you'd never be able to guarantee that Ids when available after an insert for example. Feb 14, 2014 at 9:37

You must log in to answer this question.

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