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
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   }
VamsiVamsi
So its not working only for cases created from email to case ?
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