I wrote a trigger to pull out campaign field values and update them on leads that meet specific criteria.

I am getting an error at line 25, column 1 The error is as followings:

deleteThisTrigger: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.deleteThisTrigger: line 25, column 1

I'm not sure why I am getting that error since the expectation is that the lead would be getting assigned the campaign value (which from the system debug has a value)


trigger deleteThisTrigger on CampaignMember (before insert) {
    Set<Id> campaignIds = new Set<Id>();

    for (CampaignMember cm : Trigger.new) {
        if (cm.LeadId != NULL && cm.ContactId == NULL && cm.Lead.System_Type__c == NULL && cm.Lead.Systems_Group__c == NULL) {


    Map<Id, Campaign> campaignMap = new Map<Id, Campaign> ([SELECT System_Type__c, Systems_Group__c
                                    FROM Campaign
                                    WHERE Id IN :campaignIds]);


    for (CampaignMember cm : Trigger.new) {
        if (cm.LeadId != NULL && cm.ContactId == NULL && cm.Lead.System_Type__c == NULL && cm.Lead.Systems_Group__c == NULL) {
            Campaign thisCampaign = campaignMap.get(cm.CampaignId);


            **cm.Lead.Systems_Group__c = thisCampaign.Systems_Group__c;** 
            cm.Lead.System_Type__c = thisCampaign.System_Type__c;

  • Looks like error is when you access cm.Lead.Systems_Group__c, if you want to update the leads you have to collect the lead records in a list and insert. Doesn't seem campaignMap.get(cm.CampaignId) would be null, better to add more system debugs.
    – Raul
    Feb 13 '20 at 17:13
  • 1
    Techincally thisCampaign could be null if you tried to insert a CampaignMember with its CampaignId blank.
    – Adrian Larson
    Feb 13 '20 at 17:20
  • @Adrian good point, been a while I worked on campaign and assumed campaign is a mandatory field. Confusing part was the debug lines before the actual failing line here which didn't made sense
    – Raul
    Feb 13 '20 at 21:53

The reason for your error is because a trigger on CampaignMember has implicit access to the fields of CampaignMember ONLY via Trigger.new. Thus cm.Lead.xxxx will not resolve to a value as cm.Lead is a lookup relationship.

You'll need to collect all the CampaignMember.LeadId values and use that in a query that fetches the Lead data and save that in a map with key leadId

  • I pulled the leads into a list and fetched the lead data, but now hitting an issue where the lead values get overwritten regardless if they are blank or have a value. Expected functionality would be to only overwrite if blank. I added the new code above to my original post.
    – Jeff
    Feb 13 '20 at 19:07
  • i would suggest you mark this answer as resolved and then post a new question with the current state of the code and the current issue you are facing.
    – cropredy
    Feb 13 '20 at 19:10

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.