0

I have a trigger on contacts that is causing DML errors and I'm not sure where the problem is as I only have one update on a list.

This is occurring when I am using the Data loader to update about 30k contacts.

public class ContactTriggerHandler extends TriggerHandler{

protected override void beforeUpdate() {
    
    Map<Id, Contact> newMarketingQualifiedLeads = new Map<Id, Contact>();
    
    for(Contact tempContact : (list<Contact>)trigger.new){
        Contact oldContact = (Contact)trigger.oldMap.get(tempContact.Id);
        
        //Check that the contact is an MQL and that the value was newly checked.            
        boolean tempContactMQL = tempContact.PB_Marketing_Qualified_Lead__c;
        if((tempContactMQL == true) && (tempContactMQL != oldContact.PB_Marketing_Qualified_Lead__c)){
            newMarketingQualifiedLeads.put(tempContact.Id, tempContact);
        }
    }    

    //check the campaign produced MQL for all new MQLS
    if(checkRecursive.runContactTrigger1 && !newMarketingQualifiedLeads.isEmpty()){
        checkRecursive.runContactTrigger1 = false;
        checkCampaignProducedMQL(newMarketingQualifiedLeads);
    }
    
} // end beforeUpdate()    

/* **********************
 * When the “Marketing Qualified Lead” checkbox is populated on a Lead or Contact, 
 * find the most recent campaign the Lead/Contact was associated to and 
 * check the “Campaign Produced MQL” checkbox (which is on the Campaign Member object).
 * **********************/
private void checkCampaignProducedMQL(Map<Id, Contact> newMarketingQualifiedLeads){

    
    // Query to determine the contacts who have already been assigned a Campaign Produced MQL 
    List<Contact> contactsWhoAlreadyHaveSetCPMQL = [SELECT Id FROM Contact WHERE Id IN (SELECT ContactID FROM CampaignMember 
                                                                               WHERE ContactID IN :newMarketingQualifiedLeads.keySet()
                                                                               AND Campaign_Produced_MQL__c = TRUE)
                                                   ];        
    
    // Remove the contacts who have already been assigned a Campaign Produced MQL from the list
    for(Contact tempContact : contactsWhoAlreadyHaveSetCPMQL){
        newMarketingQualifiedLeads.remove(tempContact.Id);
    }
    
    // Query for the most recent Campaign assigned to each contact
    List<Contact> contactsWithNewestCampaignMember = [SELECT Id, Name, (SELECT ID, CreatedDate From CampaignMembers ORDER BY CreatedDate DESC LIMIT 1)
                                                                        FROM Contact WHERE Id IN :newMarketingQualifiedLeads.keySet()];
    
    // Mark each campaignMember as the Campaign Produced MQL
    CampaignMember campaignMember;
    List<CampaignMember> cmList = new List<CampaignMember>();
    
    for(Contact tempContact : contactsWithNewestCampaignMember){            
        
        try{
            campaignMember = tempContact.CampaignMembers;
            campaignMember.Campaign_Produced_MQL__c = true;
            cmList.add(campaignMember);
        }   
        catch (exception e){
            system.debug('Unable to set a Campaign Produced MQL for user ID: ' + tempContact.Id);
        }
    }                
    update cmList;
}    

}

2
  • 3
    Do you have other automation, such as Processes, Flows, or Workflow Rules, on Contact or on CampaignMember?
    – David Reed
    Commented Sep 30, 2020 at 16:59
  • Yes, there are quite a few other automations on both.
    – Scott B
    Commented Oct 2, 2020 at 14:09

1 Answer 1

1

Your trigger is not at fault here. In fact, at a quick read, it appears that your trigger is correctly and effectively bulkified. However, you note in a comment that

there are quite a few other automations on both [Contact and Campaign Member].

This suggests that the fault lies not with this trigger per se but with the totality of automation on either or both of these objects.

You can use the logs and analysis panes in Developer Console to drill down in the transaction and attempt to determine what is happening. You may have other Apex triggers that are poorly optimized, Flows that consume excess limits, recursion taking place, or some combination of issues. Once you're able to get a better fix on where the limits consumption is taking place, you'll be in a position to attempt to resolve it.

You must log in to answer this question.

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