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 

code fix

Hi,
following code gets me the bus hours for case but its getting wrong bus hour id.
Because debug log shows values stored is like this in map

{Europe/London=01mG00000008TvcIAE}
{Europe/London=01mG00000008TvbIAE}

-----
--it should simulate this code--
-----
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;
                   
                    System.debug ('====C'+ c + '====BID'+ c.BusinessHoursId);
                    update c;
-----


CURRENT CODE to fix that gets wrong business hours:
----------------------------------------------------------------------------------
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 = new List<case>{};

       Map<String,Id> caseListMap=new Map<String,Id>();
      
      for(BusinessHours bh:bHours)
       {
            for(Case c:caseList)
            {
            caseListMap.put(c.Case_Timezone__r.TimeZoneSidKey__c,bh.Id);
            }
        }
      System.debug('######This is my MAPDATA:'+ caseListMap);

      
        for (Case c :caseList)
        {
            if (c.EntitlementId != null)
            {
                try
                {
               
  Boolean bEntryCriteria = caseListMap !=  null && c.Case_Timezone__c != null && c.Case_Timezone__r.TimeZoneSidKey__c != null && caseListMap.containsKey(c.Case_Timezone__r.TimeZoneSidKey__c);
if (bEntryCriteria && (caseListMap.get(c.Case_Timezone__r.TimeZoneSidKey__c) != null || caseListMap.get(c.Case_Timezone__r.TimeZoneSidKey__c) != ''))
            
            {   
               // if (caseListMap.get(c.Case_Timezone__r.TimeZoneSidKey__c) != null || caseListMap.get(c.Case_Timezone__r.TimeZoneSidKey__c) != '')
                    //   {
                    
                    if (c.Entitlement.SlaProcess.Name != null && 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 != null && 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 != null && c.Entitlement.SlaProcess.Name == 'Standard')
                        c.BusinessHoursId = caseListMap.get(c.Case_Timezone__r.TimeZoneSidKey__c);
                       
                       // }
magicforce9magicforce9
Hi,

One thing I've noticed in the part where you said the code is working...You have three if conditions but inside them the all execute the same statement..so why having three different if conditions...?

I have refactored the code and hope it should give you the correct BusinessHoursId, please see the code below....

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, TimeZoneSidKey FROM BusinessHours WHERE Name Like '08x05%'];
Map<String, Id> timeZoneSidToBusinessHoursMap = new Map<String, Id> ();
//Since TimeZoneSidKey are unique lets create a map of TimeZoneSidKey with BusinessHours record Id
for(BusinessHours bh : bHours)
    timeZoneSidToBusinessHoursMap.put(bh.TimeZoneSidKey, bh.Id);

List<case> caseListUpdate = new List<case>{};

for (Case c :caseList)
{
    try
    {
        if (c.EntitlementId != null && c.Entitlement.SlaProcess.Name != null 
            && timeZoneSidToBusinessHoursMap.containsKey(c.Case_Timezone__r.TimeZoneSidKey__c)
            && (c.Entitlement.SlaProcess.Name == 'Mission Critical' 
            || c.Entitlement.SlaProcess.Name == 'Mission Critical (GCS Management Override)'
            || c.Entitlement.SlaProcess.Name == 'Enterprise' 
            || c.Entitlement.SlaProcess.Name == 'Enterprise (GCS Management Override)'
            || c.Entitlement.SlaProcess.Name == 'Standard'))
       
            c.BusinessHoursId = timeZoneSidToBusinessHoursMap.get(c.Case_Timezone__r.TimeZoneSidKey__c);
    }Catch(Exception e){
        //do some thing
    }
}


Hope it helps !