Loading
Prepare for Email to Become the Default Login ExperienceRead More
Service
Automatically Add Entitlements to Cases from Web, Email, and Experiences

Automatically Add Entitlements to Cases from Web, Email, and Experiences

Entitlements don’t automatically apply to cases created using Web-to-Case, Email-to-Case, or experience. However, you can add entitlements to these features using Apex code.

Required Editions

View supported editions.
User Permissions Needed
To define Apex triggers: Author Apex

When a case is created via Web-to-Case, Email-to-Case, or an experience, it isn’t automatically associated with an entitlement. When a case’s Entitlement field is empty, this sample trigger checks whether the case contact has an active entitlement. If the contact has an active entitlement, the entitlement is added to the case. If the contact doesn’t have an active entitlement, the trigger then checks whether the case account has an active entitlement. If the case account has an active entitlement, the entitlement is added to the case. The trigger helps ensure that cases are resolved according to your customer support agreements.

  1. For Classic, from Setup, enter Case Triggers in the Quick Find box, then select Case Triggers.
  2. Click New.
  3. Copy the code below and paste it in the text field.
  4. Click Save.
  5. For Lightning, from the developer console, select File > New > Apex Trigger.
  6. Enter a name for the trigger and then select the Object as Case.
  7. Click Submit.
  8. Copy the code below and paste it in the text field.
  9. Click Save.
Example
Example
trigger DefaultEntitlement on Case (Before Insert, Before Update) {
	Set<Id> contactIds = new Set<Id>();
	Set<Id> acctIds = new Set<Id>();
	for (Case c : Trigger.new) {
		contactIds.add(c.ContactId);
		acctIds.add(c.AccountId);
	}
    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;
						}
					} 
				}
			} 
		}
	}
}
 
Loading
Salesforce Help | Article