0

I have almost 25 record types and for some of the record types only I want my trigger logic to be executed at the time of Case creation. How to developed the code for the same ?

I have written some code below, but needs lots of changes & bulkification.

trigger CaseTrigger on Case (before insert, before update, after update) {

    List<Id> recordTypesId = new List<Id>();
    for(Case c : Trigger.new){
        recordTypesId.add(c.RecordTypeId);
    }

    Map<Id,String> mapRecTypeDevName = new Map<Id,String>();
    for(RecordType rt : [Select Id,DeveloperName From RecordType Where SobjectType = 'Case' and Id = :recordTypesId]){
        mapRecTypeDevName.put(rt.id, rt.DeveloperName);
    }
    ......
    .......
}

I want my Trigger to be executed

if(DeveloperName == 'Void')
   VoidUtil.voidRequest(Trigger.new);
if( DeveloperName == 'Refund' || DeveloperName == 'Partial Refund')
   RefundUtil.refundRequest(Trigger.new);

I dont want to run my code if the Case record type doesn't match. Any pointers ?

3 Answers 3

1

Querying RecordType uses a rather precious resource (SOQL governor limits), so you'd be better off getting a describe call and then you can do the translation there:

trigger CaseTrigger on Case(after insert) {
    Map<Id, RecordTypeInfo> recordTypes = Case.SObjectType.getDescribe().getRecordTypeInfosById();
    Case[] voids = new Case[0], refunds = new Case[0];
    for(Case record: Trigger.new) {
        if(record.RecordTypeId != null) {
            String recordTypeName = recordTypes.get(record.RecordTypeId).getRecordTypeName();
            if(recordTypeName == 'Void') {
                voids.add(record);
            } else if(recordTypeName == 'Refund' || recordTypeName == 'Partial Refund') {
                refunds.add(record);
            }
        }
    }
    if(!voids.isEmpty()) {
        VoidUtil.voidRequest(voids);
    }
    if(!refunds.isEmpty()) {
        RefundUtil.refundRequest(refunds);
    }
}
4
  • Think you should mention that getRecordTypeName is the label of the record type not its developer name.
    – Keith C
    Commented Aug 26, 2017 at 15:17
  • @KeithC Actually, I made the assumption that they're not using DeveloperName anyways; DeveloperName can't contain spaces in it.
    – sfdcfox
    Commented Aug 26, 2017 at 15:19
  • Yep I noticed that too, but the query is of the DeveloperName.
    – Keith C
    Commented Aug 26, 2017 at 15:23
  • Yes, Its a Underscore basically in names. I used the simple logic to compare from the map mapRecTypeDevName.containsKey('Partial_Refund'), done !
    – user48562
    Commented Aug 26, 2017 at 17:28
1

All the answers looks correct, but you can implement or refactored your code like below as well. Instead of making a map of RecordTypeId to DeveloperName, Just make a map other way around. If you've almost 25 RecordTypes then you should not allow other class to unnecessary call if particular record type not associated with the Case. Just to increase performance.

trigger CaseTrigger on Case (before insert, before update, after update) {  
    Set<Id> rId = new Set<Id>();
    for(Case c : Trigger.new){
        rId.add(c.RecordTypeId);
    }

    Map<String,Id> mapDToR = new Map<String,Id>();
    for(RecordType rt : [Select Id,DeveloperName From RecordType Where SobjectType = 'Case' and Id = :rId]){
        mapDToR.put(rt.DeveloperName, rt.id);
    }

    if(mapDToR.containsKey('Void')){
        VoidUtil.voidRequest(Trigger.new);
    }
    if(mapDToR.containsKey('Partial_Refund')){
        VoidUtil.voidRequest(Trigger.new);
    }
}
0

You have the hardest part done in that you have created a map that translates the record type Id that is directly on Case into the developer name.

So assuming you don't want to refactor the util classes:

Case[] voids = new Case[] {};
Casep] refunds = new Case[] {};
for (Case c : Trigger.new) {
    String n = mapRecTypeDevName.get(c.RecordTypeId);
    if (n == 'Void') voids.add(c);
    else if (n == 'Refund' || n == 'Partial Refund') refunds.add(c);
}
VoidUtil.voidRequest(voids);
RefundUtil.refundRequest(refunds);

(Better to make recordTypesId a set too.)

You must log in to answer this question.