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
NevDevNevDev 

Trigger to Auto Assign An Entitlement To A Case

Hi guys,

Can anyone help me with a trigger requirment. I am looking to automatically assign an entitlement to a case based on the entitlement that is related to the Account associated to the Case.

I need it to work via email to case, web to case and manual update.
karthikeyan perumalkarthikeyan perumal
Hello, 

Use Below Code, 
 
trigger DefaultEntitlement on Case (Before Insert, Before Update) {
    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;
                        }
                    } 
                }
            } 
        } 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;
                            }
                        } 
                    }
                } 
            }
        }
    } 
}

Reference from https://help.salesforce.com/articleView?id=entitlements_auto_add.htm&language=en_US&type=0

Hope this will help you, 
Mark Best ANSWER if its works for you. 

Thanks
karthik
NevDevNevDev
Hi Karthik,

I have tried this but I am running into an error on line 5. Variable does not exist: contactIds
 
karthikeyan perumalkarthikeyan perumal
Hello, 

Here is the updated Code i missed some of the Code @ begining, 
 
trigger DefaultEntitlement on Case (Before Insert, Before Update) {
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){
    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;
                        }
                    } 
                }
            } 
        } 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;
                            }
                        } 
                    }
                } 
            }
        }
    } 
}
Hope this will Help you, 

Mark Best ANSWER if its work for you. 

Thanks
karthik

 
John McAfeeJohn McAfee
Quick question. I am doing my forst trigger and have it in my sandbox. I think I need to creat a unit test for it before deploying it.. does anyone have a quick bit of code I can use to test ?  also, thanks for the above!! 
SandySandy
Hi 
Can anyone help me in writing above trigger code with helper class?
Bal Krishna YadavBal Krishna Yadav

Hi karthikeyan perumal, 

The above trigger is not working with email-to case