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
Jay Parikh 36Jay Parikh 36 

Apex trigger to calling future method error : System.AsyncException: Future method cannot be called from a future or batch

Hi i have one apex class which has future method and every time i am calling it from apex trigger it is throwing this error : System.AsyncException: Future method cannot be called from a future or batch
Apex class :
Public class AccountProcessor {
    @future
    Public static void countContacts (set <ID> ActId){
    Double NumOfcontact ;
    set <id> actToUpdate = new set <id> ();
    list <account> act = [select id ,Number_of_Contacts__c  from account where id in : ActId];
    for (Account acLst : act){
    actToUpdate.add(acLst.id);
    }
    List <contact> ct = [select id , AccountId from contact where  AccountId in : actToUpdate ];
    if(ct.size() == 0 ){
    NumOfcontact = 0;
    }
    else if (ct.size() > 0){
    NumOfcontact = (ct.size());
    }
    Account act2 ;
    List <account> ActLatUpdate = new list <account>();
    for (Account actfinal : act ){
         act2 = new account ();
         act2.id = actfinal.id;
         act2.Number_of_Contacts__c =  NumOfcontact;
         ActLatUpdate.add(act2);
    }
    if(ActLatUpdate != null){
       Update ActLatUpdate;
    }
    }
}



Apex trigger :
trigger Toupdatecontact on account (after insert , after update){

   AccountProcessor.countContacts(Trigger.newMap.keyset());
   
}
Raj VakatiRaj Vakati
Try this
trigger Toupdatecontact on account (after insert , after update){
if(System.IsBatch() == false && System.isFuture() == false){ 
   AccountProcessor.countContacts(Trigger.newMap.keyset());
}
}