Posts

Showing posts with the label Queueable Apex

Salesforce Hacks: System.LimitException: Too many queueable jobs added to the queue: 2

Queueable classes are great to use and provides a wide range of advantages when compared to future methods. But there are some undocumented limitations which you will come across if you are using Queueable classes extensively inside your batch classes. Lets take an example of badly written trigger on Account.  trigger AccountTrigger on Account (before insert) {     for(Account acc: Trigger.new){         Database.executeBatch(new CreateContactBatch(), 2);     } }   The above trigger calls a batch class for every account record that gets inserted in database. So if you insert N accounts in a single transaction via dataloader or anonymous block or through any other means, the trigger will execute the batch class N number of times. Below is the code for batch class: global class CreateContactBatch implements Database.Batchable<sObject> {          global Database.QueryLocator start(Database.BatchableContext BC) {         String query = 'SELECT Id FROM Account'