0

This question already has an answer here:

when i am changing the case owner for more than 10 records in the case list it is throwing the error : Apex trigger LFIQTriggerForCaseChanges caused an unexpected exception, contact your administrator: LFIQTriggerForCaseChanges: System.LimitException: Too many future calls: 11

I believe we have to do some changes in Trigger. Can you please help?

Trigger :

trigger LFIQTriggerForCaseChanges on Case (after insert,after update) {
    /** WK URLs. **/


    //String TDGStagingURL = 'link';
   //String TDGProductionURL = 'link';
    for(Case listCases: Trigger.New)
    {
        List<Case> caseDetail= [SELECT User_ID__c,Account.Name, CaseNumber,ClosedDate,CreatedDate,CST__c,Description,Origin,Priority,Product__c,Status,Subject,Type,Owner.Name FROM Case where CaseNumber =: listCases.CaseNumber];
        for(Case c: caseDetail)
        {
            List<User> salesidDetail = [SELECT  OW_Employee_ID__c,Name FROM User WHERE Id IN (SELECT OwnerId FROM Case where CaseNumber =: c.CaseNumber)];
            if(salesidDetail.size() > 0) {
            LFIQNotifyCase.main(salesidDetail[0].OW_Employee_ID__c,c.Account.Name,c.CaseNumber,c.ClosedDate,c.CreatedDate,c.CST__c,c.Description,c.Origin,c.Priority,c.Product__c,c.Status,c.Subject,c.Type,c.Owner.Name,WKProductionURL);
            LFIQNotifyCase.main(salesidDetail[0].OW_Employee_ID__c,c.Account.Name,c.CaseNumber,c.ClosedDate,c.CreatedDate,c.CST__c,c.Description,c.Origin,c.Priority,c.Product__c,c.Status,c.Subject,c.Type,c.Owner.Name,WKStagingURL);
           } 
        }
    }
}

LFIQNotifyCase class :

//Notify LFIQ webservices on changes to CASES object

public class LFIQNotifyCase 
{  
    public static boolean isApexTest = false;

    @Future(callout=true) 
    public static void main(String SFId,String Name,String CaseNumber,DateTime ClosedDate,DateTime CreatedDate,String CSTID,String Description,String Origin,String Priority,String ProductName,String Status,String Subject,String Type,String OwnerName,String ServiceURL)
    {
         try {

                String result = 'error';

                HttpRequest req = new HttpRequest();
                HttpResponse res = new HttpResponse();
                Http http = new Http();
                req.setEndpoint(ServiceURL + '/InsertSFNotification?UserId='+ (SFId !=null?SFId:'0')+'&ACCOUNT_NAME=' + (Name !=null?Name.replace(' ','%20'): Name) + '&CASENUMBER=' + CaseNumber + '&CLOSEDDATE=' + (ClosedDate !=null?ClosedDate.format('yyyy-MM-dd'): null) + '&CREATEDATE=' + (CreatedDate !=null?CreatedDate.format('yyyy-MM-dd'): null)  + '&CSTID=' + CSTID + '&DESCRIPTION=' + (Description !=null?Description.replace(' ', '%20'): Description) + '&ORIGIN=' + (Origin !=null?Origin.replace(' ', '%20'): Origin) + '&PRIORITY=' + (Priority !=null?Priority.replace(' ', '%20'): Priority) + '&PRODUCT=' + (ProductName !=null?ProductName.replace(' ', '%20'): ProductName) + '&STATUS=' + (Status !=null?Status.replace(' ', '%20'): Status) + '&SUBJECT=' + (Subject !=null?Subject.replace(' ','%20'): Subject) + '&TYPE=' + (Type !=null?Type.replace(' ', '%20') : Type) + '&OWNERNAME=' + (OwnerName !=null?OwnerName.replace(' ', '%20'): OwnerName));               
                req.setMethod('POST');
                req.setHeader('Content-Length','0');

                req.setCompressed(false);

                if(!isApexTest) { 
                  res = http.send(req);
                }

            } 
            catch(System.CalloutException e) 
            {
                System.debug('Error Calling LFIQ Webservice (' +  ServiceURL + ') : '+ e);
                //System.debug(res.toString());
            }  
    }
}

marked as duplicate by Vamsi Krishna, Samuel De Rycke, Christian Deckert, Eric, Peter Knolle Aug 21 '14 at 13:59

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Vinoth, please search for 'too many future calls' and you will find lot of previously answered threads which will give you some direction on this issue. – Vamsi Krishna Aug 21 '14 at 13:12
1

You're calling LFIQNotifyCase.main() for every single Case in trigger.new, this is why you're getting this error.

Please see this page for a guide to 'bulkifying' your code: link

Once familiar with the concept of making your trigger bulk proof, here's something like what you'll need to move towards:

Trigger:

trigger LFIQTriggerForCaseChanges on Case (after insert,after update) {

    LFIQNotifyCase.main(trigger.newMap.keySet());
}

Class:

public class LFIQNotifyCase {

public static boolean isApexTest = false;

@Future(callout=true)
public static void main (Set<Id> caseIds) {

    Map<Id, Case> caseMap = new Map<Case>([
        SELECT
            Id,
            <OtherFields>
        FROM
            Case
        WHERE
            Id IN (SELECT OwnerId FROM Case where CaseNumber IN : caseNumbers)
    ]);

    Set<Integer> caseNumbers = new Set<Integer>();

    for (Case c : caseMap.values()) {

        caseNumbers.add(c.CaseNumber);
    }

    List<User> salesidDetail = [
        SELECT  
            OW_Employee_ID__c,
            Name 
        FROM 
            User 
        WHERE 
            Id IN :caseNumbers
    ];

    // Now that you have all Case & User records, perform your callout(s) here
}

}

With this structure, LFIQNotifyCase.main() is only being called once & is being passed a set containing the IDs of all Case in trigger.new - instead of the details of one Case like in your code.

You'll need to further modify your callout code to handle this bulk pattern.

Not the answer you're looking for? Browse other questions tagged or ask your own question.