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
Ratheven SivarajahRatheven Sivarajah 

I am trying to stick a entitlement to every new case getting created

I am writing a trigger where every new case gets an entitlement that I created automatically. I want the same entitlement for every case even if the accounts are different. What I have done so far I have created a entitlement process and created a dummy account. I then created a new entitlment with the entitlement process and dummy account. I then put entitlement in my case layout so when I create a case I can search for the entitlment and append to any case. I want to make the entitlement stick to every case that gets generated automatically without me selecting it in my case layout. 
Best Answer chosen by Ratheven Sivarajah
Shri RajShri Raj
trigger AddEntitlementToCase on Case (before insert) {
    // Get the Id of the entitlement process and dummy account
    Id entitlementProcessId = ...;
    Id dummyAccountId = ...;
    
    // Create a list of entitlements to add to each new case
    List<Entitlement> entitlements = new List<Entitlement>();
    for (Case c : Trigger.new) {
        Entitlement e = new Entitlement();
        e.EntitlementProcessId = entitlementProcessId;
        e.AccountId = dummyAccountId;
        entitlements.add(e);
    }
    
    // Insert the entitlements
    insert entitlements;
    
    // Map the entitlements to the cases
    Map<Id, Id> caseToEntitlementMap = new Map<Id, Id>();
    for (Entitlement e : entitlements) {
        caseToEntitlementMap.put(e.CaseId, e.Id);
    }
    
    // Update the cases with the entitlements
    for (Case c : Trigger.new) {
        c.EntitlementId = caseToEntitlementMap.get(c.Id);
    }
}

Note: You need to replace ... with the actual Id of your entitlement process and dummy account.