2

A Campaign Member After Update trigger should fire after a lead in a campaign is converted, correct? My AfterUpdate trigger is not firing at all in unit testing. I have enabled "Require Validation for Converted Leads".

I can see some managed package activity when the lead is converted, and then my CampaignMember AfterUpdate trigger is skipped, but the contact is being created and is populating the existing CampaignMember record.

trigger CampaignMemberTriggers on CampaignMember (after insert, after update) {
    if (Trigger.isAfter && Trigger.isInsert) {
        CampaignMemberAfterInsertHandler.process(Trigger.new);
    }
    if (Trigger.isAfter && Trigger.isUpdate) {
        system.debug('*** after update ***');
        CampaignMemberAfterUpdateHandler.process(Trigger.new, Trigger.oldMap);
    }
}

Update: I created a clean developer instance and it doesn't work there either. I created a simple afterupdate trigger, wrote a unit test and ran it, and the trigger doesn't fire.

trigger CampaignMemberTriggers on CampaignMember (after update) {
    if (Trigger.isAfter && Trigger.isUpdate) {
        system.debug('*** after update ***');
    }
}

@isTest
public class UnitTest {
    static testmethod void testTrigger() {
        Account acct = new Account(Name = 'Apex Test');
        insert acct;

        Campaign camp = new Campaign(Name = 'apex test', isActive = true);
        insert camp;

        Lead myLead = new Lead(LastName = 'Apex Test', Company = 'Apex Test');
        insert myLead;

        CampaignMember cm = new CampaignMember(CampaignId = camp.Id, LeadId = myLead.Id);
        insert cm;

        Database.LeadConvert lc = new Database.LeadConvert();
        lc.setLeadId(myLead.Id);
        lc.setAccountId(acct.Id);
        lc.setDoNotCreateOpportunity(true);

        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted = true LIMIT 1];
        lc.setConvertedStatus(convertStatus.MasterLabel);
        Database.LeadConvertResult lcr = Database.convertLead(lc);
        System.assert(lcr.isSuccess());
        CampaignMember cmNew = [SELECT LeadId, ContactId FROM CampaignMember WHERE Id = :cm.Id];

    }
}
1
  • I wonder about your premise; note that LeadConvertStatus does not have any reference to a campaignmemberId() indicating that it is unaffected by the lead conversion
    – cropredy
    Nov 21, 2014 at 23:49

2 Answers 2

2

It turns out that if a Lead has a CampaignMember record, and the Lead is converted, that does not fire the AfterUpdate trigger on CampaignMember even though the record is changed (ContactId field is populated by the conversion).

I had to write my trigger on the Lead object instead.

0

I am not sure what you are trying to achieve and I am also not sure if something changes at all in the campaignMember object on lead conversion as it is still the same member.

You might think about using an after (or before) update trigger on lead, that if converted that it checks if it is also a campaign member and if so,to do what ever you like to do with that information.

It might also be possible that those installed package activity blocks the "update" event on the campaign member?

You must log in to answer this question.

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