You need to sign in to do that
Don't have an account?
louisa barrett 7
Query limits on triggers
Could someone offer me an explanation as to why this trigger from the APEX developer guide would exceed SOQL limits?
---------------------------------------------------
This is another example of a flawed programming pattern. It assumes that fewer than 100 records are pulled in during a trigger invocation. If more than 20 records are pulled into this request, the trigger would exceed the SOQL query limit of 100 SELECT statements:
trigger MileageTrigger on Mileage__c (before insert, before update) {
for(mileage__c m : Trigger.new){
User c = [SELECT Id FROM user WHERE mileageid__c = m.Id];
}
}
--------------------------------------------------------------------
I completely understand why you shouldn't do this, but I don't understand how 20 records in the trigger would exceed 100 SELECT statements.
Could someone also confirm if the below is considered correct, my understanding would be this would only use 1 SELECT statement and the query would not be re-run over each iteration.
trigger oppTrigger on Opportunity (before delete) {
for (Quote__c q : [SELECT opportunity__c FROM quote__c
WHERE opportunity__c IN :Trigger.oldMap.keySet()]) {
Trigger.oldMap.get(q.opportunity__c).addError('Cannot delete
opportunity with a quote');
}
}
Many thanks
---------------------------------------------------
This is another example of a flawed programming pattern. It assumes that fewer than 100 records are pulled in during a trigger invocation. If more than 20 records are pulled into this request, the trigger would exceed the SOQL query limit of 100 SELECT statements:
trigger MileageTrigger on Mileage__c (before insert, before update) {
for(mileage__c m : Trigger.new){
User c = [SELECT Id FROM user WHERE mileageid__c = m.Id];
}
}
--------------------------------------------------------------------
I completely understand why you shouldn't do this, but I don't understand how 20 records in the trigger would exceed 100 SELECT statements.
Could someone also confirm if the below is considered correct, my understanding would be this would only use 1 SELECT statement and the query would not be re-run over each iteration.
trigger oppTrigger on Opportunity (before delete) {
for (Quote__c q : [SELECT opportunity__c FROM quote__c
WHERE opportunity__c IN :Trigger.oldMap.keySet()]) {
Trigger.oldMap.get(q.opportunity__c).addError('Cannot delete
opportunity with a quote');
}
}
Many thanks
The trigger you wrote is correct, the query will run once for all the records in the trigger.