---
schema_version: "1"
id: 079a97a4abae5a0af9b0b4b64cc364f6c53dbcae8d537c82c73a559b7e9429e3
title: Automatically Add Entitlements to Cases from Web, Email, and Experiences
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/service/264-0-0/entitlements_auto_add.md
canonical_url: https://help.salesforce.com/s/articleView?id=service.entitlements_auto_add.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: ebe9f08f497a9d78c2e74ec2332bfe9f8531fbe27723df57b9ff95a0848e9d48
description: 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.
products:
  - Service Cloud
release: "264"
---
# 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

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small edition"><colgroup><col></colgroup><tbody><tr class=""><td class="slds-cell-wrap" style="vertical-align:top;"><a href="https://help.salesforce.com/s/articleView?id=service.service_editions_reference.htm&amp;language=en_US" target="_blank">View supported editions</a>.</td></tr></tbody></table>

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small permission">
        <colgroup>
          <col style="width:50%">
          <col style="width:50%">
        </colgroup>
        <thead class="slds-text-title_caps ">
          <tr class="">
            <th scope="colgroup" class="slds-cell-wrap" colspan="2" style="text-align:center;vertical-align:top;" id="d58625e58">User Permissions
              Needed</th>
          </tr>
        </thead>
        <tbody>
          <tr class="">
            <td data-label="User Permissions Needed" class="slds-cell-wrap" style="vertical-align:top;" headers="d58625e58 ">To define Apex triggers:</td>

            <td class="slds-cell-wrap" style="vertical-align:top;" headers="d58625e58 ">Author Apex</td>

          </tr>
        </tbody>
      </table>

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](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-service-support-264-0-0-production-enus/dd14551c-a772-4db6-9a1d-d84df8df2980/images/icon_example.png)

Example

```java
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;
						}
					} 
				}
			} 
		}
	}
}
```

#### See Also

- [Set Up Entitlement Processes](https://help.salesforce.com/s/articleView?id=service.entitlements_process_setting_up.htm&release=264.0.0&type=5 "Entitlement processes are timelines that include all of the steps (milestones) that your support team must complete to resolve cases or work orders. Set up an entitlement process to apply to entitlements in your Salesforce org.")