0

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.

2
  • 1
    Are you sure that issue is with CaseComment trigger? Because your sample code there is bulkified. Maybe it's some other complex code and when you move you simple code to Queueable it saved the day. Check if original transaction has more than 90 SOQL
    – ytiq
    Commented Aug 17, 2020 at 22:47
  • 1
    Asynchronous methods like queueable has limit of 200 soql per transaction that is twice of synchronous, that explains why there's no error with queueable. As @ytiq said there is something else wrong. Probably unoptimized code in Case trigger or some other object.
    – Raul
    Commented Aug 17, 2020 at 23:13

1 Answer 1

6

No, queueable apex does not automatically break up dml operations. The closest thing to that would be batch apex (where you can set the scope size).

What queueable apex does do, though, is execute the code asynchronously. That means that you get expanded governor limits (200 SOQL queries, 12 MB of heap space, and a few more things).

1
  • 1
    Completely missed the increased 200 SOQL limit on async. Thanks.
    – S.B.
    Commented Aug 18, 2020 at 20:22

You must log in to answer this question.

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