You need to sign in to do that
Don't have an account?
dave_mwi
DML rate limit exceeded at 21?
I'm stumped about rate limits in my Trigger. Here is the code:
I know the code might not be the 'soundest' but why am I exceding my limit at 21? What's happening now is that I have leads created now with incomplete campaign member or conversion object assocations. The rate can be exceeded at any one of the DML statements above...whichever happens to be 21...
If someone could point me in a good direction here or give me some good advice I would whole-heartedly appreciate it. Thanks.
- Dave
Code:
trigger campaignAssociationLeadTrigger on Lead(after insert, after update){ // Get the campaign object based on the hitbox identifier List<Campaign> camps = new Campaign[0]; // Iterate through the 'new' list in the Trigger, which is Lead objects. This is for 'Update' of a Lead try { if(Trigger.isUpdate) { for(Lead l : Trigger.new) { // The only time we don't want to create a campaign or conversion is when // The old values match the incoming values. At least for now. boolean oldMatchSource = true; boolean oldMatchConversion = true; if(Trigger.oldMap.get(l.id) != null) { Lead oldLead = Trigger.oldMap.get(l.id); oldMatchSource = (l.Hitbox_Code__c != oldLead.Hitbox_Code__c); oldMatchConversion = (l.Conversion__c != oldLead.Conversion__c); } if(oldMatchSource && l.Hitbox_Code__c != null && l.Hitbox_Code__c != '') { // Get the campaing code by that name. try { camps.add([SELECT id FROM campaign WHERE name = :l.Hitbox_Code__c]); } catch(Exception e) { Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {'me@mydomain.com'}; mail.setToAddresses(toAddresses); mail.setSubject('Apex Exception in campaignAssociationContactTrigger'); mail.setPlainTextBody(e.getMessage()+'\n\n'+e.getCause()+'\n\nCode was: '+l.Hitbox_Code__c); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } // Create a campaign member from the first in the list if(camps.size()>0) { CampaignMember cmp = new CampaignMember(LeadId=l.id,CampaignId=camps[0].id); // Insert insert cmp; } } if(oldMatchConversion && l.Conversion__c != null && l.Conversion__c != '') { // If there was a hitbox code, add it to the conversion. if(camps.size()>0) { Conversion__c conv = new Conversion__c(Lead__c=l.id, Name=l.Conversion__c, Campaign__c = camps[0].id); insert conv; } else { Conversion__c conv = new Conversion__c(Lead__c=l.id, Name=l.Conversion__c); insert conv; } } } } else { // This is an Insert trigger for(Lead l : Trigger.new) { // If there is a lead source code continue to add a campaign. if(l.Hitbox_Code__c != null && l.Hitbox_Code__c != '') { // Get the campaing code by that name. try { camps.add([SELECT id FROM campaign WHERE name = :l.Hitbox_Code__c]); } catch(Exception e) { Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {'me@mydomain.com'}; mail.setToAddresses(toAddresses); mail.setSubject('Apex Exception in campaignAssociationContactTrigger'); mail.setPlainTextBody(e.getMessage()+'\n\n'+e.getCause()+'\n\nCode was: '+l.Hitbox_Code__c); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } // Create a campaign member from the first in the list if(camps.size()>0) { CampaignMember cmp = new CampaignMember(LeadId=l.id,CampaignId=camps[0].id); // Insert insert cmp; } } // If the conversion is not empty, then create a conversion object if(l.Conversion__c != null && l.Conversion__c != '') { // If there was a hitbox code, add it to the conversion. if(camps.size()>0) { Conversion__c conv = new Conversion__c(Lead__c=l.id, Name=l.Conversion__c, Campaign__c = camps[0].id); insert conv; } else { Conversion__c conv = new Conversion__c(Lead__c=l.id, Name=l.Conversion__c); insert conv; } } } } } catch(Exception e) { Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {'me@mydomain.com'}; mail.setToAddresses(toAddresses); mail.setSubject('Apex Exception in campaignAssociationContactTrigger'); mail.setPlainTextBody(e.getMessage()+'\n\n'+e.getCause()); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } }
I know the code might not be the 'soundest' but why am I exceding my limit at 21? What's happening now is that I have leads created now with incomplete campaign member or conversion object assocations. The rate can be exceeded at any one of the DML statements above...whichever happens to be 21...
If someone could point me in a good direction here or give me some good advice I would whole-heartedly appreciate it. Thanks.
- Dave
example link here:
http://wiki.apexdevnet.com/index.php/LeadDuplicatePreventer.apex
a useful first step is to pull all the queries outside the outermost for loop.
instead loop across tirgger.new and gather all the
then insert all at once
Ron,
Thanks. This was something that I was mulling over after making this post...I will try it out and thanks much for your response.
Thanks.
even write a testMethod that creates an array of 200 new leads and then inserts them.
You should have no DML error from that process
you may also be able to use this handy feature in the future:
Creating Maps From sObject Arrays
Maps from an ID or String data type to an sObject can be initialized from a list of sObjects. The IDs of the objects (which
must be non-null and distinct) are used as the keys. One common usage of this map type is for in-memory "joins" between
two tables. For instance, this example loads a map of IDs and Contacts:
Map<ID, Contact> m = new Map<ID, Contact>([select id, lastname from contact])
In the example, the SOQL query returns a list of contacts with their id and lastname fields. The new() method uses the
list to create a map. For more information, see SOQL Queries on page 50.
I was searching for that in the docs as I expected that there was a way to accomplish that.
Thanks for the input!
Dave