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
sami amisami ami 

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

Hi,

 

I am getting this error when I make REST calls .

System.AsyncException: Future method cannot be called from a future or batch method: Myfutureclass.method(LIST<Id>)

This is a trigger which calls that class which has future method and calls REST calls.

 

trigger checkcase on Case (after insert,after update) {
List<Id> caseIds = new List<Id>();
for(Case c : Trigger.New)
{
if(c.Checkbox__c)
{
caseIds.add(c.Id);
System.debug('*********Caseid'+CaseIds);
}
}
System.debug('***********Trigger.new'+trigger.new);
System.debug('***********Trigger.old'+trigger.old);
if(caseIds.size() > 0){
if(Trigger.isinsert){
Myfutureclass.method(caseIds);
}
if(trigger.isUpdate ){
if(trigger.old[0].Checkbox__c){
Myfutureclass.method(caseIds);
}
}
}
}

 

Please correct me what i am missing??Please

magicforce9magicforce9

Hi,

 

The possible reason is that your Future method is trying to do an update on case object which is causing the trigger to fire again....(recursive trigger)

 

You need to check if the trigger is being fired from the future method, add the below line as your first line of trigger.

if(system.isFuture()) return;

 

 

 

sami amisami ami
Thank you sir.
But when i insert a case,Its creating two cases in my third party.
When i update existing case and mark checkbox true,its not even executing this trigger.
What might be my mistake?
magicforce9magicforce9

Hi,

 

I'm not sure why its creating two cases in your third party system...do you mind sharing code for your webservices class ?

 

Also, Few things that I have noticed in your trigger...

 

On this is running on Update you are checking values from trigger.old..i.e 

 

if(trigger.old[0].Checkbox__c){

 I don't this will execute the code inside if condition when you select Checkbox__c field, because trigger.old will have unselected value. + When you use trigger.old[0], this means it will only check the value for one record. Hence these could be the two reasons that its not executing the logic inside the second If statement.

 

Can you also tell me exactly what you want to do in your trigger in simple plain text and I may suggest you some thing.

 

 

George Laird 55George Laird 55
@magicforce9 I can't tell you how helpful this was!   Specifically, your comment above on "if(system.isFuture()) return;"    I had a trigger that made a callout, but had to do an update from the response.  Then I also have a callout in my update trigger so I was getting the old "future can't be called from future" thing.  I googled for hours and got the same "first run" static variable approach, which doesn't work at all.  That little statement that you posted here did the trick.  Thank you sir!  Life saver.