+ Start a Discussion
testerforcetesterforce 

Automatically Associate Entitlement to Case

I'm looking for some Sample APEX that would, upon Case creation, do a lookup to the Contacts Account and find the Entitlement Associated to the Account.  In my case, there will only ever be one Entitlement associated to an Account so the results would return one record.  I need this entitlement to be automatically associated to the Case and automatically kick off.  Essentially, when a case is created, I need the entitlement to kick off immediately.  Does anyone have any sample APEX I can look at that might point me in the right direction.

Orion@gmail.comOrion@gmail.com

 

trigger defaultEntitlement on Case (Before Insert, Before Update) {
   /*
   If the Entitlement Name is not set then, check to see if the Contact on the Case has an active Entitlement
    and select the first one.  If not then check to see if the Account on the Case has an active Entitlement.
   */
   List<Id> contactIds = new List<Id>();
   List<Id> acctIds = new List<Id>();
   for (Case c: Trigger.new){
      if (c.EntitlementId == null && c.ContactId!= null && c.AccountId!= null){
         contactIds.add(c.ContactId);
         acctIds.add(c.AccountId);
      }
   }
   if(contactIds.isEmpty()==false || acctIds.isEmpty()==false){
      /* Added check for active entitlement */
      List <EntitlementContact> entlContacts = [Select e.EntitlementId,e.ContactId,e.Entitlement.AssetId From EntitlementContact e
                                                Where e.ContactId in:contactIds
                                                And e.Entitlement.EndDate >= Today And e.Entitlement.StartDate <= Today];
      if(entlContacts.isEmpty()==false){
         for(Case c: Trigger.new){
            if(c.EntitlementId == null && c.ContactId!= null){
               for(EntitlementContact ec:entlContacts){
                  if(ec.ContactId==c.ContactId){
                     c.EntitlementId = ec.EntitlementId;
                     if(c.AssetId==null && ec.Entitlement.AssetId!=null)
                        c.AssetId=ec.Entitlement.AssetId;
                     break;
                  }
               } // end for
            }
         } // end for
      } else{
         List <Entitlement> entls = [Select e.StartDate, e.Id, e.EndDate, e.AccountId, e.AssetId
                                     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;
                        if(c.AssetId==null && e.AssetId!=null)
                           c.AssetId=e.AssetId;
                        break;
                     }
                  } // end for
               }
            } // end for
         }
      }
   } // end if(contactIds.isEmpty()==false)
}

 

I found this code in the Cook book. I haven't tested or deployed it yet cause I am trying to add more functionality before I deploy it. 

 

 

 

Tall 642Tall 642

I am trying to use this code and it is not working for me on Email to case. Does anyone know if there is something wrong with it?

LinusLinus

This trigger looks exactly like  what I have been searching for. However, I have never used Apex before so need some help to implement it.. I created a trigger in Force IDE and uploaded it to my developer org. I have understood that I need a test class to implement it in my production org, can someone give an example of what a test class would look like for this trigger? 

 

Scott RussoScott Russo

The code works for me for email to case

 

Can you provide the test code used to unit test this code, so you could move it to production?

deegoodeegoo

If you're using this trigger and have different Business Hours on different Entitlements, watch out!

 

When a Case is created, it's created with your organization's specified default Business Hours.

 

As per http://eu1.salesforce.com/help/doc/en/entitlements_fields.htm, a Case's Business Hours override an Entitlement's Business Hours. This seems totally braindead, but there it is in black and white.

 

This means that unless you modify this trigger to also set the Case Business Hours to the Business Hours specified on the Entitlement you're applying, your system will calculate Milestones based on the wrong Business Hours.

 

It's a real howler, and nearly cost us in Service Credits with our customer!