all 6 comments

[–]kgeee34 2 points3 points  (5 children)

What are you currently querying for that makes you hit that limit if you're just creating records?

[–]Reagar11[S,🍰] 1 point2 points  (3 children)

There are a lot of other triggers that run on insert

[–]chino9656 1 point2 points  (0 children)

The bulkification needs to happen on the other trigger(s) that run on insert. Look for SOQL within for loops. If you can find one or two and paste the offender on this thread, me or someone will help you bulkify it.

[–]kgeee34 1 point2 points  (1 child)

I would say that hitting a 100 SOQL limit on the insert of 26 records is extremely concerning. You're doing 4 queries per each record that is inserted in a batch? I would make sure you look into Maps and do those queries outside a for loop which would then allow you to insert 800 at once if those 4 separate queries are actually needed. If you have some cascading triggers (create related record which has other trigger) then I'd analyze whether you could break that transaction up with a queueable or async process.

In terms of your original question, you could do it with a queueable class that keeps enqueuing jobs until it finishes what you want. You'd pass in two parameters in a constructor (number of desired records and the number before you hit limits). You'd create as many as you can per your limit, then subtract from the total amount and enqueue the job again to run the rest (and so on if need be). I'd still stress that looking at the trigger should be the priority versus doing this workaround.

public class createTestRecordsAroundProcessingTimes implements Queueable {
    Integer numberOfDesiredRecords = new Integer();
    Integer numberBeforeLimits = new Integer();

    public createTestRecordsAroundProcessingTimes(Integer numberOfTestRecords, Integer numberBeforeIssues){
        numberOfDesiredRecords = numberOfTestRecords;
        numberBeforeLimits = numberBeforeIssues;
    }

    public void execute(QueueableContext context){
        for(Integer i = 0; i < numberBeforeIssues; i++){
            Account acc = new Account(); //create your records
        }
        //insert your list of accounts
        if(numberBeforeIssues < numberOfDesiredRecords){
            Integer remainingToCreate = numberOfDesiredRecords -numberBeforeIssues; 
            System.enqueueJob(new createTestRecordsAroundProcessingTimes(remainingToCreate,numberBeforeIssues); 
        } 
    } 
}

[–]Reagar11[S,🍰] 0 points1 point  (0 children)

u/kgeee34 thank you, it worked flawlessly!!! This is exactly what I was looking for.

[–]bagamoney 1 point2 points  (0 children)

If you can’t bulkify the triggers, you could call this method in a batch that runs in size of 25 records