0

I am trying to write values to the Campaign Member fields based on values from the related Lead record. When I attempt to run this I get the error message:

addCampaignMemberValues: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.addCampaignMemberValues: line 7, column 1

If I wanted to use dot notation to traverse from the Campaign Member down to the Lead level fields like Lead Source, etc. how can this be done?

I'm also looking to run the default assignment rules at the end of the trigger and if am only could let me know if this approach looks OK, that would be very helpful!

Thanks!

trigger addCampaignMemberValues on CampaignMember (after insert) {
    List<Lead> ls = new List<Lead>();

    for (CampaignMember cm: trigger.new) {

        if(cm.Lead.Primary_Campaign__c == NULL && cm.Lead.LeadSource != 'Tradeshow') {
            cm.Lead.Primary_Campaign__c = cm.CampaignId;
            //check if Lead.System_Type__c is null and Campaign.System_Type__c is not null
            if(cm.Lead.System_Type__c == NULL && cm.Campaign.System_Type__c != NULL ) {
                cm.lead.System_Type__c = cm.Campaign.System_Type__c;
            }
            //check if Lead.System_Group__c is null and Campaign.System_Group__c is not null
            if(cm.Lead.Systems_Group__c == NULL && cm.Campaign.Systems_Group__c != NULL ) {
                cm.Lead.Systems_Group__c = cm.Campaign.Systems_Group__c;
            }

            ls.add(new Lead(id = cm.leadid));
        }

    }
    //run assignment rules on leads
    Database.DMLOptions dmo = new Database.DMLOptions();
    dmo.assignmentRuleHeader.useDefaultRule = true;
    Database.update(ls, dmo);

}
1

You cannot access second level fields through the trigger.new list. You need to query all 2nd level fields you need —e.g. cm.Lead.Primary_Campaign__c— and iterate through that query results instead.

There's also a problem with how you are adding the leads to be modified to your lists, you need to add the entire records you modified and not just the ids.

Not sure about the assignment rules part, you'll have to test it.

Here's the code with the two modifications:

trigger addCampaignMemberValues on CampaignMember (after insert) {
    List<Lead> ls = new List<Lead>();

    //You want to query all 2nd level fields explicitly
    for (CampaignMember cm: [SELECT Id, CampaignId, LeadId, Lead.Primary_Campaign__c, Lead.LeadSource, Lead.System_Type__c, Lead.System_Group__c, Campaign.System_Type__c, Campaign.System_Group__c FROM CampaignMember WHERE LeadId != null AND Id IN :trigger.newMap.keySet()]) {

        if(cm.Lead.Primary_Campaign__c == NULL && cm.Lead.LeadSource != 'Tradeshow') {
            cm.Lead.Primary_Campaign__c = cm.CampaignId;
            //check if Lead.System_Type__c is null and Campaign.System_Type__c is not null
            if(cm.Lead.System_Type__c == NULL && cm.Campaign.System_Type__c != NULL ) {
                cm.Lead.System_Type__c = cm.Campaign.System_Type__c;
            }
            //check if Lead.System_Group__c is null and Campaign.System_Group__c is not null
            if(cm.Lead.Systems_Group__c == NULL && cm.Campaign.Systems_Group__c != NULL ) {
                cm.Lead.Systems_Group__c = cm.Campaign.Systems_Group__c;
            }

            ls.add(cm.Lead); //Here you will be adding the Lead with the modifications for the queried fields and not just Id
        }

    }
    //run assignment rules on leads
    Database.DMLOptions dmo = new Database.DMLOptions();
    dmo.assignmentRuleHeader.useDefaultRule = true;
    Database.update(ls, dmo);

}
| improve this answer | |
  • Sorry for the multiple edits, I'm multitasking here :) – Ideasforce Jan 20 at 20:32
  • Yes! This was very helpful info, thank you! – Jeff Jan 21 at 22:09

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