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
sieb4mesieb4me 

how to fix this code ...crosses 100 limit


ERROR:
15:33:36.389 (12389752771)|SOQL_EXECUTE_BEGIN|[1180]|Aggregations:0|select Id from BusinessHours where (TimeZoneSidKey = :tmpVar1 and Name like '08x05%')
15:33:36.389 (12389782974)|EXCEPTION_THROWN|[1180]|System.LimitException: Too many SOQL queries: 101
15:33:36.389 (12389962129)|METHOD_EXIT|[31]|01pG00000045Dj6|CT_CaseTriggerHandler.setBusinessHours(LIST<Case>)
15:33:36.390 (12390066361)|FATAL_ERROR|System.LimitException: Too many SOQL queries: 101

Class.CT_CaseTriggerHandler.setBusinessHours: line 1180, column 1
Trigger.CaseAfterUpdate: line 31, column 1
15:33:36.390 (12390091595)|FATAL_ERROR|System.LimitException: Too many SOQL queries: 101

Class.CT_CaseTriggerHandler.setBusinessHours: line 1180, column 1
Trigger.CaseAfterUpdate: line 31, column 1
15:33:36.390 (12390326072)|CODE_UNIT_FINISHED|CaseAfterUpdate on Case trigger event AfterUpdate for [50011000002857m, 500110000028QQu, 50011000002BXoA,




CODE:
//Set the business hours on the case
    public static void setBusinessHours(List<Case> lCases)
    {
        List<Case> caseList = [SELECT Id, BusinessHoursId, EntitlementId, Entitlement.SlaProcess.Name, Case_Timezone__r.TimeZoneSidKey__c FROM Case WHERE Id in :lCases];
       
        for (Case c :caseList)
        {
            if (c.EntitlementId != null)
            {
                try
                {
                    if (c.Entitlement.SlaProcess.Name == 'Mission Critical' || c.Entitlement.SlaProcess.Name == 'Mission Critical (GCS Management Override)')
                        c.BusinessHoursId = [SELECT Id FROM BusinessHours WHERE TimeZoneSidKey = :c.Case_Timezone__r.TimeZoneSidKey__c AND Name Like '08x05%'].Id;
                    else if (c.Entitlement.SlaProcess.Name == 'Enterprise' || c.Entitlement.SlaProcess.Name == 'Enterprise (GCS Management Override)')
                        c.BusinessHoursId = [SELECT Id FROM BusinessHours WHERE TimeZoneSidKey = :c.Case_Timezone__r.TimeZoneSidKey__c AND Name Like '08x05%'].Id;
                    else if (c.Entitlement.SlaProcess.Name == 'Standard')
                        c.BusinessHoursId = [SELECT Id FROM BusinessHours WHERE TimeZoneSidKey = :c.Case_Timezone__r.TimeZoneSidKey__c AND Name Like '08x05%'].Id;
                   
                    update c;
                }
                catch (exception e)
                {
                    System.debug ('===== Error in SetBusinessHours: ' + e.getMessage() + ' =====');
                }      
            }
        }
    }
sieb4mesieb4me

line 1180 is below
c.BusinessHoursId = [SELECT Id FROM BusinessHours WHERE TimeZoneSidKey = :c.Case_Timezone__r.TimeZoneSidKey__c AND Name Like '08x05%'].Id;
sieb4mesieb4me
Even after putting limit 1
c.BusinessHoursId = [SELECT Id FROM BusinessHours WHERE TimeZoneSidKey = :c.Case_Timezone__r.TimeZoneSidKey__c AND Name Like '08x05%' LIMIT 1].Id;..it still gives same errors
Arunkumar RArunkumar R
Since Your Code seems you have wrote SOQL Query within the for loop and update also happening within the loop..

It will cause Too many soql issues. So please change your code as follow,
1. Remove SOQL Queries within for loop, and query your condition outer loop and process those soql queries list within loop.
2. Create a list and add your for loop processed values within the list and update the list outer the for loop.


Please read best practices and change according to this,
https://developer.salesforce.com/page/Apex_Code_Best_Practices
sieb4mesieb4me
Thanks Arun. I know that practice but with this code i am not sure how do do that.
Arunkumar RArunkumar R
Hi,

I have not tested this code, please let me know if any issue
public static void setBusinessHours(List<Case> lCases)
    {
       List<Case> caseList = [SELECT Id, BusinessHoursId, EntitlementId, Entitlement.SlaProcess.Name, Case_Timezone__r.TimeZoneSidKey__c FROM Case WHERE Id in :lCases];
       List<BusinessHours> bHours=[SELECT Id FROM BusinessHours WHERE Name Like '08x05%']
	   List<case> caseListUpdate;
	   Map<Id,Id> caseListMap=new Map<Id,Id>();
	   
	 

	   for(BusinessHours bh:bHours)
	   {
			for(Case c:caseList)
			{
			caseList.put(c.Case_Timezone__r.TimeZoneSidKey__c,bh.Id);
			}
	    }
	   
	   
        for (Case c :caseList)
        {
            if (c.EntitlementId != null)
            {
                try
                {
                    if (c.Entitlement.SlaProcess.Name == 'Mission Critical' || c.Entitlement.SlaProcess.Name == 'Mission Critical (GCS Management Override)')
                        c.BusinessHoursId = caseListMap.get(c.Case_Timezone__r.TimeZoneSidKey__c);
                    else if (c.Entitlement.SlaProcess.Name == 'Enterprise' || c.Entitlement.SlaProcess.Name == 'Enterprise (GCS Management Override)')
                        c.BusinessHoursId = caseListMap.get(c.Case_Timezone__r.TimeZoneSidKey__c);
                    else if (c.Entitlement.SlaProcess.Name == 'Standard')
                        c.BusinessHoursId = caseListMap.get(c.Case_Timezone__r.TimeZoneSidKey__c);
                   
                   caseListUpdate.add(c);
                }
                catch (exception e)
                {
                    System.debug ('===== Error in SetBusinessHours: ' + e.getMessage() + ' =====');
                }      
            }
        }
		update caseListUpdate;
    }


sieb4mesieb4me
Thanks a lot. This code surely helps.
Following line  needed change ..thanks once again. This was very helpful.

caseList.put(c.Case_Timezone__r.TimeZoneSidKey__c,bh.Id);
to
caseListMap.put(c.Case_Timezone__r.TimeZoneSidKey__c,bh.Id);


sieb4mesieb4me
  hi, with your code when i update case, i get this error
Apex trigger cause exception...AfterUpdate......StringException Invalid Id:America/LosAngeles L External entry point,

Seems like its storing string in Id field. why is that? In Map it seems like you are calling Id, unless I am wrong?
sieb4mesieb4me
Hi Arun,
Youe code is good but it always gets one value for business hour.
Its not getting the right business hour since map stores value like this

DEBUG|UUUUU This is my MAPDATA:{Europe/London=01mG00000008TvbIAE}
DEBUG|UUUUU This is my MAPDATA:{Europe/London=01mG00000008TvcIAE}

What do we do to get the right value?

Thanks.