function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vishnu YadavalliVishnu Yadavalli 

AsyncApexExecutions Limit exceeded error for my batch apex on custom object.

I wrote a batch apex that selects all the records from  updated events object. When ever this batch apex is scheduled it throwing the forrlowing error:

AsyncApexExecutions Limit exceeded.

 

Following is my batch apex code.

 

 

global class SyncCalendarsUpdates implements Database.Batchable<sObject>, Database.AllowsCallouts {
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id, Event_ID__c, Description__c, Event_Owner__r.Id, Subject__c, Start_Datetime__c, End_Datetime__c, Click_Key__c FROM Updated_Event__c';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Updated_Event__c> scope) {
        Map<String, String> clickKeyMap = new Map<String, String>();
        for (Updated_Event__c e : scope) {
          
            Boolean doUpdate = true;
            if (e.Subject__c != null) {
                if (e.Subject__c.left(2) == 'SV' && e.Click_Key__c != null && e.Click_Key__c != '') {
                    doUpdate = false;
                }
            }
            if (doUpdate) {
                ClickCalendar cal = new ClickCalendar(e.Event_Owner__r.Id, e.Start_Datetime__c, e.End_Datetime__c, e.Subject__c, e.Click_Key__c);
                cal.buildUpdateRequest();
                if (!Test.isRunningTest()) {
                    HttpResponse resp = cal.getResponse();
                    
                  
                    if (e.Click_Key__c == null) {
                        String key = cal.getKeyFromResponse(resp);
                        if (e.Event_ID__c != null) {
                            clickKeyMap.put(e.Event_ID__c, key);
                        }
                    }
                }
            }
            e.Is_Synced__c = true;
        }
        update scope;
        
   
        List<Event> eventsToUpdate = [SELECT Id, Click_Key__c FROM Event WHERE Id IN :clickKeyMap.keySet()];
        for (Event e : eventsToUpdate) {
            e.Click_Key__c = clickKeyMap.get(e.Id);
        }
        update eventsToUpdate;
    }
    
    global void finish(Database.BatchableContext BC) { }
}

 

 

We have 1,500,000 records in that object.

Best Answer chosen by Admin (Salesforce Developers) 
zachbarkleyzachbarkley

update eventsToUpdate;

 

This is a DML Statement, which has a governing limit of 10,000.You are updating your batch at the end, and not on each iteration.

 

Put "LIMIT 9999" on the end of your query. If this works then this is your issue.

 

Your solution would be to do a database.executebatch(b, 1) which adds or breaks apart the records into 9999 records or less at a time to a list then updates that list.

All Answers

zachbarkleyzachbarkley

Hi Vishnu,

 

It looks like your hitting governing limits. Try to have them processed 1 by 1.

 

public class SyncCalendarsUpdates_Schedule implements Schedulable{
    public SyncCalendarsUpdates_Schedule(){}
    public void execute(SchedulableContext ctx){
        SyncCalendarsUpdates b = new SyncCalendarsUpdates();
        database.executebatch(b, 1);         
    }
}

 

zachbarkleyzachbarkley

Also, to activate this, add that class to your apex scheduled jobs or run this command in the DEBUG Console Execute code window (This will run it at 4am daily)

 

System.schedule('myScheduler', '0 0 4 * * ?', new SyncCalendarsUpdates_Schedule());

Vishnu YadavalliVishnu Yadavalli

It still throws me the same error.

zachbarkleyzachbarkley

update eventsToUpdate;

 

This is a DML Statement, which has a governing limit of 10,000.You are updating your batch at the end, and not on each iteration.

 

Put "LIMIT 9999" on the end of your query. If this works then this is your issue.

 

Your solution would be to do a database.executebatch(b, 1) which adds or breaks apart the records into 9999 records or less at a time to a list then updates that list.

This was selected as the best answer
Arthur LockremArthur Lockrem
This can be the result of exceeding the number of batches allowed.  If your batch size is too small for the quantity of records you are attempting to process you will receive this error.

Solution: Increase the batch size from Database.executeBatch(batchClass, 1) to Database.executeBatch(batchClass, 200)