+ Start a Discussion
StaciStaci 

business hours on cases using company default instead of milestone business hours

I have my entitlement and entitlement process without business hours.  The only place it is declared is the milestone and the Company Profile page.  Is something I need to include in my trigger to NOT make it default to the Company Profile?

Trigger
trigger CW_DefaultEntitlement on Case (Before Insert, Before Update) {
    
    List<Id> acctIds = new List<Id>();
    for(Case c: Trigger.new){
    if(String.isNotBlank(c.AccountId)){
        acctIds.add(c.AccountId);
             }
        }
        List <Entitlement> entls = [Select e.StartDate, e.Id, e.EndDate, e.AccountId From Entitlement e
                Where e.AccountId in :acctIds And e.EndDate >= Today And e.StartDate <= Today];
        if(entls.isEmpty()==false){
            for(Case c : Trigger.new){
                if(c.EntitlementId == null && c.AccountId != null){
                    for(Entitlement e:entls){
                        if(e.AccountId==c.AccountId){
                            c.EntitlementId = e.Id;
                            
                        }
                    }
                }
            }
        }
    }

 
SFDC_BigDogSFDC_BigDog

@Staci

Please try this and let me know if this works.

trigger CW_DefaultEntitlement on Case (Before Insert, Before Update) {
    
    Set<Id> acctIds = new Set<Id>();
    for(Case c: Trigger.new)
    {
      if(c.AccountId != Null)
      {
          acctIds.add(c.AccountId);
      }

    }
        List <Entitlement> entls = [Select id, StartDate, EndDate, AccountId From Entitlement  Where AccountId in :acctIds And EndDate >=: System.today() And StartDate <=: System.Today()];

        Map<String,Entitlement> emap = New Map<String,Entitlement>();

        for(Entitlement em:entls)
        {
          if(em.AccountId != Null)
          {
            emap.put(em.AccountId,em);
          }
        }
        System.debug(emap);

        if(emap.size() >0)
        {
            for(Case c : Trigger.new)
            {
                if(c.EntitlementId == null && c.AccountId != null && emap.containsKey(c.AccountId))
                {
                            
                            c.EntitlementId = emap.get(c.AccountId).id;
                            
                        
                    
                }
            }
        }
    }