+ Start a Discussion
Jos VervoornJos Vervoorn 

Too many SOQL queries 201 ...

Hi, I'm running into the famous SOQL Governor limit's when executing Apex batch. One of the reasons is that whenever a case record is updated ... the MileStoneTimeCalculator class is called/executed which has 4 SOQL queries.

Maybe anyone of you can simplify the code and lower the SOQL queries. All help is gratefully appreciated ...
Global class U4_Legacy_INC_MilestoneTimeCalculator implements Support.MilestoneTriggerTimeCalculator {

    //** Constructor for this class.
    global Integer calculateMilestoneTriggerTime(String caseId, String milestoneTypeId)
        Integer MilestoneValue;
        Product_Milestone_Times__c ProductMilestoneSet;
        Milestone_Times__c EntitlementMilestoneSet;

        //** Select values from case
        Case caseRec = [SELECT Id, EntitlementId, ProductId, Priority, Contractual_Type__c FROM Case WHERE Id=:caseId LIMIT 1];
        MilestoneType mtype = [SELECT Name FROM MilestoneType WHERE Id=:milestoneTypeId];

        // Retrieve Milestone definition from Entitlement
        EntitlementMilestoneSet = [SELECT Response_Time__c, Resolution_Time__c, Software_Resolution_Time__c FROM Milestone_Times__c WHERE Entitlement__c =: caseRec.EntitlementId AND Priority__c =: caseRec.Priority];

        // Retrieve Internal Milestone definition from Product
        ProductMilestoneSet = [SELECT Response_Time__c, Resolution_Time__c, Software_Resolution_Time__c FROM Product_Milestone_Times__c WHERE Product__c =: caseRec.ProductId AND Priority__c =: caseRec.Priority];

        if(mtype.Name == SEConstants.FIRSTRESPONSETIME){
            if(caseRec.Contractual_Type__c == SEConstants.INTERNAL_ONLY)
                // Take First Response time from Product
                MilestoneValue = ProductMilestoneSet.Response_Time__c.intValue();
                // Take First Response time from Entitlement
                MilestoneValue = EntitlementMilestoneSet.Response_Time__c.intValue();
        } // Response Time
        if(mtype.Name == SEConstants.RESOLUTIONTIME){
            if(caseRec.Contractual_Type__c == SEConstants.INTERNAL_ONLY || caseRec.Contractual_Type__c == SEConstants.FIRST_RESPONSE_ONLY)
                // Take Resolution time from Product
                MilestoneValue = ProductMilestoneSet.Resolution_Time__c.intValue();
                // Take Resolution time from Entitlement
                MilestoneValue = EntitlementMilestoneSet.Resolution_Time__c.intValue();
        } // Resolution Time
        if(mtype.Name == SEConstants.SOFTWARERESOLUTIONTIME){
           If(caseRec.Contractual_Type__c == SEConstants.INTERNAL_ONLY || caseRec.Contractual_Type__c == SEConstants.FIRST_RESPONSE_ONLY || caseRec.Contractual_Type__c == SEConstants.FIRST_RESPONSE_RESOLUTION_ONLY || (caseRec.Contractual_Type__c == SEConstants.FIRST_RESPONSE_RESOLUTION_P1_P2_SOFTWARE && (caseRec.Priority == SEConstants.PRIORITY_3 || caseRec.Priority == SEConstants.PRIORITY_4)))
                // Take Software Resolution time from Product
                MilestoneValue = ProductMilestoneSet.Software_Resolution_Time__c.intValue();
                // Take Software Resolution time from Entitlement
                MilestoneValue = EntitlementMilestoneSet.Software_Resolution_Time__c.intValue();
        } // Software Resolution Time       
        Return MilestoneValue;

Parag Bhatt 10Parag Bhatt 10
Hi  Jos Vervoorn,
You have queried too many SOQL query in one method thats only when you run It throws an error of "201 Too Many Query".
The Only way to remove that error is to just divide the whole method to two or more. then The error will be resolved.
Because when the method is called all the soql excecutes and governer limits is reached. :)

just place those SOQL query which return larger results.

It will work as expected :)

Please let us know if this will help you.

Parag Bhatt