0

I wrote a trigger to update the lead associated to new campaign members with values from the associated campaign. The leads should only be updated from the campaign if the fields are blank on the lead.

What I am seeing is that the leads get updated from the campaign, but are getting updated even if they are already populated with a value on the lead.

Not sure why the if empty statement is getting ignored.

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

    for (CampaignMember cm : Trigger.new) {
        if (cm.LeadId != NULL && cm.ContactId == NULL) {
            campaignIds.add(cm.campaignId);
            leadIds.add(cm.LeadId); 
        }
    }

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

    Map<Id, Lead> leadMap = new Map<Id, Lead> ([SELECT System_Type__c, Systems_Group__c
                                                FROM Lead
                                                WHERE Id IN :leadIds
                                                AND Systems_Group__c = NULL
                                                AND Systems_Group__c = NULL]);

    List<Lead> leadToUpdate = new List<Lead>();

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

                if(!leadToUpdate.isEmpty()) {
                    leadToUpdate.add(thisLead);
                }

            }
        }
    }

    if(!leadToUpdate.isEmpty()) {
        update leadToUpdate;
    }

}

1 Answer 1

0

First buggy statement: In this query you have the same condition repeated:

Map<Id, Lead> leadMap = new Map<Id, Lead> ([SELECT System_Type__c, Systems_Group__c
                                                FROM Lead
                                                WHERE Id IN :leadIds
                                                AND Systems_Group__c = NULL
                                                AND Systems_Group__c = NULL]);

Probably you wanted to set the second filter to System_Type__c = NULL.

Second buggy statement is the first occurrence of:

                if(!leadToUpdate.isEmpty()) {
                    leadToUpdate.add(thisLead);
                }

This would prevent any lead from being added to the leadToUpdate list, as the list is initially empty. You'd also want to use a Map (with the Id as key) since the same lead can be a campaign member to different campaigns and that would cause the update to fail.

To summarize: As it is, your trigger should not be performing any updates to any Leads. I would look for other trigger, workflow, process builder automation in your Org that may be doing so.

3
  • 3
    I cannot recommend strongly enough against using Set<SObject>. Unless you absolutely know and can explain how it works, it is a very bad idea.
    – Adrian Larson
    Feb 20, 2020 at 1:52
  • Updated the answer to reflect you statement
    – Ideasforce
    Feb 20, 2020 at 2:08
  • Yes, there was an other workflow running in the background that was writing to these fields. Once that was updated, the trigger worked as expected. Thank you!
    – Jeff
    Feb 20, 2020 at 18:58

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.