1

I'm writing a trigger on Campaign members, and I'd like to use the ID of the Lead associated with the campaign member. When I try to access the Lead Id in the trigger it returns null.

Is it possible to access this ID?

trigger FirstCampaign on CampaignMember (after insert) {
    for(CampaignMember cm : Trigger.New) {
        system.debug('The lead ID IS: ' + cm.Lead.ID);
    }   
}

1 Answer 1

1

Relationships are not automatically populated, such that Trigger.new[0].Lead.Id will always be null. This is always true for all parent and child relationships. See answers like this one that explain what you'd do. This is a slightly different scenario, though. You simply need to use the correct field. Lead.Id is a reference to a sObject's field, while LeadId is the Lead's Id as it exists on the CampaignMember. Generally speaking, you will always want to use the lookup field's Id instead of the relationship's Id for performance reasons anyways.

trigger FirstCampaign on CampaignMember (after insert) {
    for(CampaignMember cm : Trigger.New) {
        system.debug('The lead ID IS: ' + cm.LeadId);
    }   
}

You must log in to answer this question.

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