You need to sign in to do that
Don't have an account?
Patrick G. Brown
Too Many DML Statements 151 Campaign Member Import
I have a trigger that works fine up to about 100 Campaign Members on import, but once I hit around 100 I receive the Too Many DML Statements 151 Error. Anyone have an idea how to fix?
trigger setLeadStatusandCurrentCampaign on CampaignMember (after insert) { Set<Id> leadIds = new Set<Id>(); for (CampaignMember cml: Trigger.new){ leadIds.add(cml.LeadId); } Map<Id, Lead> LeadMap2 = new Map<Id, Lead>([SELECT Id, Current_Campaign__c, Status FROM Lead WHERE Id IN :leadIds]); //Loop through Campaign Member Leads and look for Current Campaign values for (CampaignMember cml: Trigger.new){ List<Lead> leadToUpdate2=new List<Lead>(); Lead associatedLead2 = LeadMap2.get(cml.LeadId); if(associatedLead2.Current_Campaign__c==NULL) { associatedLead2.Status = 'In Campaign'; associatedLead2.Current_Campaign__c = cml.Campaign_Name__c; leadToUpdate2.add(associatedLead2); } update leadToUpdate2; } }
It seems that the statement below is not returning a Lead record instance for the specified Lead Record Id. This which causes the Current_Campaign__c check at line # 19 to fail since associatedLead2 is null.
I would recommend updating the code as follows which checks first, if the associated Lead record Id exists in LeadMap2 and then assigns it for further usage. Please mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.
All Answers
Thanks
PSM
The issue with your existing code is that you are performing an update which is a DML operation within a for loop. This code performs a DML for every iteration of the for loop. Salesforce imposes a governor limit of being able to perform only 100 DML operations within a single execution context. Hence, when you try loading more than 100 Campaign Member records the code fails, since it hits the governor limits of 100 DML operations within a single execution context.
Hence, it is recommended that you Bulkify your code which means instead of performing a DML operation on a single record, perform it on a collection of records using a List. The below code performs the update on multiple Campaign Members and executes a single update for every 200 record batch of records prcoessed by the trigger and will help you address the issue.
Moreover, I noticed that you are iterating over Trigger.new to get the Lead Record Ids which is not requried and you can instead use Trigger.newMap.keyset() to accomplish the same with less code. Please mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.
Would you recommend taking the if statement out of the for loop as well? Not sure how I could check the Current_Campaign__c for Null if I did that.
Your post back to me was perfect regarding the DML insert error. After removing the update from within the for loop, it's much more efficient. Thank you kindly.
It seems that the statement below is not returning a Lead record instance for the specified Lead Record Id. This which causes the Current_Campaign__c check at line # 19 to fail since associatedLead2 is null.
I would recommend updating the code as follows which checks first, if the associated Lead record Id exists in LeadMap2 and then assigns it for further usage. Please mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.
I'll continue to work on it. I really appreciate your excellent support.