Search code examples
salesforceapex-code

Database.executeBatch cannot be called from a batch or future method


i am calling a batch class from finish method of another batch class and facing the above error

The method code:

public class BatchPublicCampaignsToGoogle 
implements Database.Batchable<SObject>, Database.Stateful, 
           Database.AllowsCallouts {

    public void finish(Database.BatchableContext BC){   
        if(newBatch == true) {           
            CalendarSettings__c c = 
                [Select Id, Name, CalendarId__c,
                 CalendarQuery__c,FieldToDisplay__c 
                 from CalendarSettings__c 
                 WHERE Name = 'Internal marketing Calendar' LIMIT 1] ;

            BatchPublicCampaign bjob = new BatchPublicCampaign();
            Database.executeBatch(bjob);
        }
    }

}

But I get the following error:

Database.executeBatch cannot be called from a batch or future method. 

After finishing batch request, I am trying to call it (I have to call it for two times) again. So I call it.

How can I fix this error?


Solution

  • Apex batch class Minimum version should be 27.0 i was using 25.0 thats why i was facing this issue!!