• Vishnu Yadavalli
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 1
    Replies

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.