0
\$\begingroup\$

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;
        }
    }
}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. Questions should include a description of what the code does \$\endgroup\$ Mar 12 '18 at 22:04
  • \$\begingroup\$ @Dannnno - I have added some additional text about the code. \$\endgroup\$
    – DaveB
    Mar 12 '18 at 23:02
1
\$\begingroup\$

Well done on your first try for Apex code.

A few pointers:

  1. 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)
  2. 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<String> theEntitlementNames = new Set<String> {
        'Priority 1',
        'Priority 2',
        'Priority 3',
        'Priority 4'
    };

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

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

        // Map the Entitlement Ids by Name
        Map<String, Id> theEntitlementIdsByName = new Map<String, Id>();
        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]);
            }
        }

    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Thanks for the code review. I will look into making the code changes you have suggested. \$\endgroup\$
    – DaveB
    Mar 21 '18 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.