0

I have a scenario where I'm inserting large number of records through trigger --> queueable job and running in to CPU timeout exception. I believe the best way of overcoming this is to split records in queueable and call the same job recursively.However, I'm not sure on how on to implement this

public class InsertnewRecords implements Queueable {
   public InsertnewRecords(List<processemails__c> emaillist){
              insert emaillist;
        } 
}

How do I split emaillist in to batches of 200.

  • Can you show us the exception? Also, how big is your list? And how many records in that object? – zaitsman Jan 20 at 4:04
0

Your DML operation needs to be in the execute method:

public class InsertnewRecords implements Queueable {
    // Store all the records to process
    ProcessEmails__c[] recordsList;
    public InsertnewRecords(List<processemails__c> emaillist){
        recordsList = emailList;
    }
    public void execute(QueueableContext context) {
        ProcessEmails__c[] temp = new ProcessEmails__c[0];
        // Grab up to 200 records
        while(!recordsList.isEmpty() && temp.size() < 200) {
            temp.add(recordsList.remove(0));
        }
        // If you might get errors, make sure you add error handling here
        insert temp;
        if(!recordsList.isEmpty()) {
            System.enqueueJob(this); // Do the next 200
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.