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
jd_labbejd_labbe 

Too many future calls error in Apex class - please help!!

Hello,

 

We had a vendor write an Apex class that takes a string sent from BigMachines (quoting tool) to SF.com and compiles it into data sent to a custom object. This class works in production. However, we are attempting to deploy 2 triggers from sandbox to production and now receiving the following error:

 

Failure Message: System Limitation too many future calls 11 Failure Stack Trace: "Class.BigMachinesSyncBOMClass.syncQuoteBOMWithOpty: line 5, column10 Trigger.syncBOMTrigger: line 54, column 9"

 

SF.com developer support provided the following input, but I have no idea how to do this:

 

 

The error you are getting about ' Too many future calls' in the Apex class 'BigMachinesSyncBOMClass' due to the method updateBOMLater within the method 'syncQuoteBOMWithOpty'.

 

It is coming due to there is a governor limit of a maximum of 10 asynchronous calls within the context of a single Apex logical transaction. You are hitting this limit because it is calling the method more than ten times.

 

Total number of methods with the future annotation allowed per Apex invocation = 10

 

Below mentioned is the helpful link to know more about it:

 

http://www.salesforce.com/us/developer/docs/apexcode/index_CSH.htm#apex_gov_limits.htm?SearchType=Stem

 

To avoid this governor limit, you can incorporate batch apex in your implementation.Each iteration of the batch resets the limit, so pass the limitation you want to process into the batch.

 

 

Apex Code is below:

 

public class BigMachinesSyncBOMClass {
    public static void syncQuoteBOMWithOpty(ID quoteId, ID opportunityId, String BOM, Boolean delOldBOMs) {
         updateBOMLater(quoteId, opportunityId, BOM, delOldBOMs);
    }
    
    
    private static BOM_Line_Item__c[] getOldOpportunityBOM(ID opportunityId) {
        return [select Id, Opportunity__c from BOM_Line_Item__c
                where Opportunity__c = :opportunityId];       
    }
    
    private static BOM_Line_Item__c[] getNewOpportunityBOM(ID quoteId, ID opportunityId, String BOM) {
        List<BOM_Line_Item__c> oppBOMToCreate = new List<BOM_Line_Item__c>();
        
        //String BOM = 'N-GEO-01-00250%%3%%1%%family%%line%%model%%configuration%%12345&N-GEO-01-00251%%3%%1%%family%%line%%model%%configuration%%12346&';
        
        /* Parse BOM String
         *  '&'     -   splits lines
         *  '%%'    -   splits attributes
         */
        String[] BOMArr = BOM.split('&');
        List<String> BOMCodes = new List<String>();
        Map<String, String> BOMProdMap = new Map<String,String>();
        //Only run if BOM is not empty
        if (BOMArr.size() > 0 && BOM != 'empty') {
            //Create Map of BOM line string to SFDC Product2 object
            for (Integer i=0; i<BOMArr.size(); i++){
                String productCodeStr = BOMArr[i].substring(0,BOMArr[i].indexOf('%'));
                BOMCodes.add(productCodeStr);
            }
        
            Product2[] BOMProds = [select Id, ProductCode from Product2 where ProductCode in :BOMCodes];
        
            for (Integer i=0; i<BOMProds.size(); i++) {
                BOMProdMap.put(BOMProds[i].ProductCode,BOMProds[i].Id);
            }
            
             //Create BOM_Line_Item object list for each line
            //loop through BOMProds, get line string from Map
            for (Integer i=0; i<BOMArr.size(); i++){
                //split line to get attributes
                String[] BOMAttributes = BOMArr[i].split('%%'); 
                
                //Create now BOM_Line_Item object and add it to List
                oppBOMToCreate.add(
                    new BOM_Line_Item__c(   
                            Product_Code__c = BOMAttributes[0], 
                            Quantity__c = integer.valueOf(BOMAttributes[1]), 
                            Document_Number__c = BOMAttributes[2], 
                            Product_Family__c = BOMAttributes[3],
                            Product_Line__c = BOMAttributes[4],
                            Product_Model__c =BOMAttributes[5],
                            Configuration_Name__c = BOMAttributes[6],
                            //Serial_Number__c =BOMAttributes[7],
                            Opportunity__c = opportunityId,
                            Product__c = BOMProdMap.get(BOMAttributes[0])
                    )
                );
            }
            
        }
        //Return BOM list
        return oppBOMToCreate;
    }
    @future private static void updateBOMLater(ID quoteId, ID opportunityId, String BOM, Boolean delOldBOMs) {
     
        if(delOldBOMS){
            BOM_Line_Item__c[] oppBOMToDelete = getOldOpportunityBOM(opportunityId);
            delete oppBOMToDelete;
        }
        
        if(BOM != '' && BOM != 'empty' && BOM != 'noBOM' && BOM != ''){
            BOM_Line_Item__c[] oppBOMToInsert = getNewOpportunityBOM(quoteId, opportunityId, BOM);
            insert oppBOMToInsert;
        }
    }
    public class QuoteSyncException extends Exception {}    
    
    static testMethod void testSyncQuoteWithOpty() {
        Opportunity opty = new Opportunity();
        opty.Name = 'BigMachines test opportunity';
        opty.StageName = 'Prospecting';
        opty.CloseDate = Date.today();
        insert opty;
        // BigMachines__Quote__c.ensurePrimary - begin test
        BigMachines__Quote__c[] quotes = new BigMachines__Quote__c[3];
        for (Integer i=0; i<quotes.size(); i++) {
            quotes[i] = new BigMachines__Quote__c();
            quotes[i].Name = 'BigMachines test quote ' + (i+1);
            quotes[i].BigMachines__Opportunity__c = opty.Id;
            //quotes[i].Stage__c = 'Prospecting';
            quotes[i].BOM__c = '850010100%%1%%27%%Network Management%%GeoProbe%%SpIprobe - 2U/14U%%Moorooka%%&696-0024-04-002%%1%%27%%Network Management%%GeoProbe%%SpIprobe - 2U/14U%%Moorooka%%&850010100%%1%%35%%Network Management%%GeoProbe%%SpIprobe - 2U/14U%%Mascot%%&696-0024-04-002%%1%%35%%Network Management%%GeoProbe%%SpIprobe - 2U/14U%%Mascot%%&';
            quotes[i].BOM2__c = '850010100%%1%%27%%Network Management%%GeoProbe%%SpIprobe - 2U/14U%%Moorooka%%&696-0024-04-002%%1%%27%%Network Management%%GeoProbe%%SpIprobe - 2U/14U%%Moorooka%%&850010100%%1%%35%%Network Management%%GeoProbe%%SpIprobe - 2U/14U%%Mascot%%&696-0024-04-002%%1%%35%%Network Management%%GeoProbe%%SpIprobe - 2U/14U%%Mascot%%&';
            quotes[i].BOM3__c = '850010100%%1%%27%%Network Management%%GeoProbe%%SpIprobe - 2U/14U%%Moorooka%%&696-0024-04-002%%1%%27%%Network Management%%GeoProbe%%SpIprobe - 2U/14U%%Moorooka%%&850010100%%1%%35%%Network Management%%GeoProbe%%SpIprobe - 2U/14U%%Mascot%%&696-0024-04-002%%1%%35%%Network Management%%GeoProbe%%SpIprobe - 2U/14U%%Mascot%%&';
            quotes[i].BOM4__c = '850010100%%1%%27%%Network Management%%GeoProbe%%SpIprobe - 2U/14U%%Moorooka%%&696-0024-04-002%%1%%27%%Network Management%%GeoProbe%%SpIprobe - 2U/14U%%Moorooka%%&850010100%%1%%35%%Network Management%%GeoProbe%%SpIprobe - 2U/14U%%Mascot%%&696-0024-04-002%%1%%35%%Network Management%%GeoProbe%%SpIprobe - 2U/14U%%Mascot%%&';
            quotes[i].BOM5__c = '850010100%%1%%27%%Network Management%%GeoProbe%%SpIprobe - 2U/14U%%Moorooka%%&696-0024-04-002%%1%%27%%Network Management%%GeoProbe%%SpIprobe - 2U/14U%%Moorooka%%&850010100%%1%%35%%Network Management%%GeoProbe%%SpIprobe - 2U/14U%%Mascot%%&696-0024-04-002%%1%%35%%Network Management%%GeoProbe%%SpIprobe - 2U/14U%%Mascot%%&';
            
        }
        insert quotes;  
        // BigMachines__Quote__c.ensurePrimary - end test
        
        
    
        quotes[0].BigMachines__Is_Primary__c = true;
        update quotes; 
        quotes[0].BigMachines__Is_Primary__c = false;
        quotes[1].BigMachines__Is_Primary__c = true;
        update quotes; 
        
                
        
        updateBOMLater(quotes[1].id, opty.id, quotes[1].BOM__c,true);
        // BigMachinesSyncBOMClass.syncQuoteWithOpty - end positive test
    } 
}

 

jd_labbejd_labbe

Can someone out there please help me? I believe that I just need a jumpstart on how to write batch apex to work around this future call limit error to complete my test.

 

 

 

Thanks!!