You need to sign in to do that
Don't have an account?

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()); }
Try this