+ Start a Discussion
Priya AakankshaPriya Aakanksha 

I created one batch class that will update one checkbox

I created one batch class that is throwing error : (First error: Update failed. First exception on row 0 with id 00Q0w000002hR7ZEAU; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, LeadTrigger: execution of AfterUpdate

caused by: System.AsyncException: Future method cannot be called from a future or...)
-----------------------------------------------------------------------
I need to run one batch class that will update one checkbox with below conditions :
If true, update Delay in response field = TRUE.
The batch class should only address leads where the ‘Lead Response time by SM’ is in the last 24 hours.
----------Code----
global class LeadActivityBatchClass implements database.Batchable<sobject>,schedulable{
  
    global Database.queryLocator start(Database.BatchableContext bc){
        String RecordUrl = '';
        Datetime Response = System.now().addhours(-24);
        String query ='SELECT Id,Delay_in_response__c,Status,DC_Lead_Status__c,Lead_Response_time_by_SM__c,(select id,recording_url__c from Tasks) FROM Lead where Lead_Response_time_by_SM__c >:Response';
        
            return Database.getQueryLocator(query); // you can modify the query as per your requirement.
    }
    
    
    global void execute (Database.BatchableContext BC, List<Lead> LdList){
    List<Lead> leadlist = new list<lead>();
        For(lead ld:ldlist){
       if(!System.isFuture()){
      If(ld.Status == 'Meeting Scheduled' && ld.DC_Lead_Status__c == 'Meeting Scheduled'){
                ld.Delay_in_response__c=true;
                leadlist.add(ld);
      }
        }
        }
        update leadlist;
       }            
     
     global void finish(Database.BatchableContext BC){      
        
    }
 global void execute(SchedulableContext sc) {
        LeadActivityBatchClass b1 = new LeadActivityBatchClass();
        ID batchprocessid = Database.executeBatch(b1,200);   
        
    }  
}
-------------
Please provide some solution.
SwethaSwetha (Salesforce Developers) 
HI Priya,
As the error suggests,  you need to detect when you're already in an @future or batch context, and then simply not call your @future method(s).
Recommend checking Defensive Apex Programming (http://www.slideshare.net/developerforce/defensive-apex-programming)
public void defensiveFutureCall{
    if(System.isFuture() || System.isBatch()){
        regularSyncCall();
    } else {
        if(Limits.getFutureCalls() < Limits.getLimitFutureCalls()){
            doFutureCall();
        } else {
            // Do some explicit notification so that you know your @future call was denied
        }
    }
}

@future
public void doFutureCall(){
    regularSyncCall();
}

public void regularSyncCall(){
    // Put the code, that you'd otherwise put in your @future method, in here
}

Reference: https://salesforce.stackexchange.com/questions/102196/error-future-method-cannot-be-called-from-a-future-or-batch-class/102200

If this information helps, please mark the answer as best. Thank you