2

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

5

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.

Edit:
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
        );

        membersToInsert.add(member);

        Database.LeadConvert leadConvert = new Database.LeadConvert();
        leadConvert.setLeadId(lead.id);
        leadConvert.setConvertedStatus(convertStatus.MasterLabel);

        leadsToConvert.add(leadConvert);
    }

    insert membersToInsert;

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

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. – Alex Tennant Feb 12 '14 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. – David Cheng Feb 13 '14 at 23:47
  • Yes, that's correct. I've updated my answer with a tested example of doing this in an after insert trigger. – Alex Tennant Feb 14 '14 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. – Alex Tennant Feb 14 '14 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.