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
Sushmitha B 15Sushmitha B 15 

Schedulable batch class with future callout

Hi,

We have a trigger which calls an static method specified with @future(callout = true) on every insert.

When creating records(insert) from  Schedulable batchclass  the following error occurs

First error: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, triggername: execution of AfterInsert

caused by: System.AsyncException: Future method cannot be called from a future or batch method:

Any solution for this please.

please find my batchclass sample here

 
global class SchedulableBatchClass implements Database.Batchable<sObject>, Schedulable,Database.AllowsCallouts
{
  global Database.QueryLocator start(Database.BatchableContext BC)
  {
    return Database.getQueryLocator( // query here);
  }
  
  global void execute(Database.BatchableContext BC, List<Contact> contacts)
  {
    for(Contact newcontact : contacts)
    {
       
                   
          INSERT newobject; // more than 1000 inserts
       
    }       
  }

  
  global void finish(Database.BatchableContext BC)
  {

  }
  
  global void execute(SchedulableContext SC)
  {
      SchedulableBatchClassFor sbc = new SchedulableBatchClassFor();
      Database.executeBatch(sbc, 600);
  }
}

//trigger
trigger tTrigger on t__c (after insert) 
{
    If(Trigger.isafter && Trigger.isInsert)
    {
      for(Integer i=0; i<trigger.new.Size();i++)
      {
        SomeClass.Staticmethod(trigger.new[i])  ;
      }
    }
}

public class someclass{
@future(callout= true)
    public static void ExecuteSend()
   {
//http request here.
}
}

Thanks.
SwethaSwetha (Salesforce Developers) 
HI Sushmitha,
A similar scenario was reported here:https://salesforce.stackexchange.com/questions/367113/getting-error-system-asyncexception-future-method-cannot-be-called-from-a-futur

The System class has some methods to help you out here. System.isFuture() and System.isBatch(). You will need to (at the very least) wrap your call to your @future method inside of an if block that checks to see if you're already in a future/batch context.
if(!System.isFuture() && !System.isBatch()){
    // your future method call here
}

Also see https://salesforce.stackexchange.com/questions/244092/error-future-method-cannot-be-called-from-a-future-or-batch-method

If this information helps, please mark the answer as best. Thank you
Sushmitha B 15Sushmitha B 15
if( !System.isBatch())
{
    // your future method call here
}
else
{
 //another method without future call out.
}

Added the above code in trigger

But getting the following error

First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, tTrigger: execution of AfterInsert

caused by: System.CalloutException: Callout from triggers are currently not supported.