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
Shawn Reichner 29Shawn Reichner 29 

Too many future calls: 51 Error - Please Help

Hello Devs, I have the following Trigger and Class that was working fine until this morning when I received the following error of Too Many future calls: 51. 

Any idea how to alleviate gettign this error?  Here is my trigger and class code....thank you all for any help you can provide....

Trigger:
 
trigger DeleteExtraBookingsTrigger on Opportunity (after insert) {

    For(Opportunity opps : Trigger.new){
        If(opps.Auto_Bookings__c == True){
            
            
        DeleteExtraBookingsClass.deleteRecords(Trigger.newMap.keySet());    
            
        }
    }
    
    
}

Class code:
 
public class DeleteExtraBookingsClass {

    @future
    public static void deleteRecords(Set<ID> oppIdsToDelete){
        List<Opportunity> opp = [SELECT ID, Armor_Product_Category__c From Opportunity WHERE ID IN : oppIdsToDelete AND Auto_Bookings__c = True
                                AND (Armor_Product_Category__c = null or Armor_Product_Category__c = 'Armor | null')];
        
        If(opp.size()>0){
        delete opp;
        database.emptyRecycleBin(opp);
        }
    }
    
}

Best Answer chosen by Shawn Reichner 29
GauravGargGauravGarg
Hi Shawn,

Please do not call another class within loop. Try to extract data into one List and send list to process. Please find the updated code below:
 
trigger DeleteExtraBookingsTrigger on Opportunity (after insert) {

	List<Opportunity> oppList = new List<Opportunity>();
    For(Opportunity opps : Trigger.new){
        If(opps.Auto_Bookings__c == True){
            oppList.add(opps);
        }
    }
	
	if(oppList != null && oppList.size() > 0)	
		DeleteExtraBookingsClass.deleteRecords(oppList);    
    
    
}

Thanks,
Gaurav
Skype: gaurav62990