Set the entitlement of a case based on record type This is my first salesforce code. It seems to me there is too much data lookup but I don't know enough about saleforce apex coding to say.

The purpose of the code is to set the entitlement based on the record type when inserting a new Case record.

The business reason for this code is the proper assignment of Entitlements (SLAs) for the various Case types.

The code runs before the record is inserted into the table. It enforces the relationship between Case type and Entitlement type.

trigger SetCaseEntitlementOnInsert on Case (before insert) {
    System.debug('SetCaseEntitlementOnInsert executed');

    // 1) Loop through the Case(s) to be inserted.
    // 2) Get the Entitlement for each based on the RecordType.
    // 3) Retrieve the Entitlement.   
    // 4) Assign the Entitlement to the Case

    for(Case c : Trigger.New) {
        RecordType recordType = [SELECT Id, Name FROM RecordType WHERE Id =: c.RecordTypeId];
        String recordTypeName = recordType.Name;

        if(recordTypeName == 'Trouble Tickets') {
            Entitlement entitlement = [SELECT Id, Name FROM Entitlement WHERE Name = 'Priority 1'];
            c.EntitlementId = entitlement.Id;
        } else if (recordTypeName == 'Access Request/New Hire') {
            Entitlement entitlement = [SELECT Id, Name FROM Entitlement WHERE Name = 'Priority 2'];
            c.EntitlementId = entitlement.Id;
        } else if (recordTypeName == 'Purchase Order') {
            Entitlement entitlement = [SELECT Id, Name FROM Entitlement WHERE Name = 'Priority 3'];
            c.EntitlementId = entitlement.Id;
        } else {
            // Termination
            Entitlement entitlement = [SELECT Id, Name FROM Entitlement WHERE Name = 'Priority 4'];
            c.EntitlementId = entitlement.Id;
        }
    }
}
Well done on your first try for Apex code.

A few pointers:


Record types aren't always available for all users, when doing logic on Record types by Name I usually add a Formula to retrieve the name (one less query and I can control the permissions)
Don't add queries in for loops, you could / would hit SOQL limits very very quicly


I haven't proven that this will work but here is an example of what you should do:

trigger SetCaseEntitlementOnInsert on Case (before insert) {
    System.debug('SetCaseEntitlementOnInsert executed');

    // 1) Loop through the Case(s) to be inserted.
    // 2) Get the Entitlement for each based on the RecordType.
    // 3) Retrieve the Entitlement.   
    // 4) Assign the Entitlement to the Case

    // Assuming you create a custom field with a Formula to retrieve RecordType.Name with API name RecordTypeName__c

    // Collection of Entitlement Names
    Set&lt;String&gt; theEntitlementNames = new Set&lt;String&gt; {
        'Priority 1',
        'Priority 2',
        'Priority 3',
        'Priority 4'
    };

    // Query the Entitelments
    List&lt;Entitlement&gt; theEntitlements = [SELECT Id, Name FROM Entitlement WHERE Name IN :theEntitlementNames];

    // If we have Entitlements
    if (!theEntitlements.isEmpty()) {

        // Map the Entitlement Ids by Name
        Map&lt;String, Id&gt; theEntitlementIdsByName = new Map&lt;String, Id&gt;();
        for (Entitlement theEntitlement : theEntitlements) {
            theEntitlementIdsByName.put(theEntitlement.Name, theEntitlement.Id);
        }

        // Now for each Case
        for (Case theCase : Trigger.New) {
            if (theCase.RecordTypeName__c == 'Trouble Tickets') {
                theCase.EntitlementId = theEntitlementIdsByName.get(theEntitlementNames[0]);
            } else if (theCase.RecordTypeName__c == 'Access Request/New Hire') {
                theCase.EntitlementId = theEntitlementIdsByName.get(theEntitlementNames[1]);
            } else if (theCase.RecordTypeName__c == 'Purchase Order') {
                theCase.EntitlementId = theEntitlementIdsByName.get(theEntitlementNames[2]);
            } else {
                theCase.EntitlementId = theEntitlementIdsByName.get(theEntitlementNames[3]);
            }
        }

    }
}