+ Start a Discussion
StephenCraneStephenCrane 

Entitlement Trigger Not Working On Sandbox

Hey there,

I'm super new to developing with Salesforce - so it's entirely possible I missed something easy. But with this code as reference: https://help.salesforce.com/articleView?id=entitlements_auto_add.htm&language=en_US&type=0 - I put it in my sandbox as a Case Trigger. It saved correctly (I had to make some bracket changes), and says code coverage 0%.  However when I email for email-to-case, the active entitlement is not applied to the case. Code below...help? :)
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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)
}

 
Best Answer chosen by StephenCrane
VamsiVamsi
A case creation should cause the trigger to execute. Did you apply any filters for cases after line 72 or after line 89 

All Answers

VamsiVamsi
Hi,

Is this code commented out 

List<Id> contactIds = new List<Id>();
059   List<Id> acctIds = new List<Id>();
060   for (Case c : Trigger.new){
061      if (c.EntitlementId == null && c.ContactId!= null && c.AccountId != null){
062         contactIds.add(c.ContactId);
063         acctIds.add(c.AccountId);
064      }
065   }
StephenCraneStephenCrane
Hey Vamsi,

Not sure why it's showing up like that, but it's not commented out (attached photo, you can see the different shading). I also just tested it with a case created in the system and it works as intended, entitlement is added :)

Not sure if this has to do something with it being a sandbox environment?

User-added image
VamsiVamsi
So its not working only for cases created from email to case ?
StephenCraneStephenCrane
Correct
VamsiVamsi
A case creation should cause the trigger to execute. Did you apply any filters for cases after line 72 or after line 89 
This was selected as the best answer
StephenCraneStephenCrane
Actually, one thing I've realized is that it assigns the entitlements after the ticket is edited no matter the source....so on creation it doesn't add them, but if I edit the ticket the entitlement is added. What do I change to get it on creation?
StephenCraneStephenCrane
So this is working as expected now...not sure what was going on before, or if I just didn't understand it. But entitlements are applied to, and working nicely.

The only thing that's missing is if it's an email-to-case they don't show up until action is taken on teh ticket, but that's fine for now. Thanks Vamsi!