I have a case comment trigger that queries data based on the trigger.new using IN
. Ex: SELECT Id,Type FROM Case WHERE Id IN:caseIds
where caseIds
is a list of parentIds
of the CaseComments
being passed in.
When inserting a List<Case>
of 100 cases I run into a 101 SOQL limit error, the debug logs indicate that for each comment inserted using Case.Comments, the trigger is being invoked once per case resulting in 100 queries even thought the
101 SOQL Limit Code:
List<Case> caseList = new List<Case>();
for (integer i=0; i < 100; i++) {
Case newCase = new Case();
newCase.Subject = 'Test';
newCase.Comments = 'Test';
caseList.add(newCase);
}
insert caseList;
However, if I pass the same caseList to a queueable with no changes to the CaseComment trigger, I do not run into the 101 SOQL limit error. All cases are successfully created and created with comments.
Working Queueable Code:
public class AsyncExecutionExample implements Queueable {
List<Case> caseList = new List<Case>();
public AsyncExecutionExample(List<Case> rCaseList) {
caseList = rCaseList;
}
public void execute(QueueableContext context) {
insert caseList;
}
}
public static void run() {
List<Case> caseList = new List<Case>();
for (integer i=0; i < 100; i++) {
Case newCase = new Case();
newCase.Subject = 'Test';
newCase.Comments = 'Test';
caseList.add(newCase);
}
System.enqueueJob(new AsyncExecutionExample(caseList));
}
Question
Why does the queueable not run into the limit? The logic is the same and I would expect even though this is queued to run into the same SOQL limits error on the case comments.