+ Start a Discussion
Ricki ReayRicki Reay 

Prevent Trigger On Specific Case Record Type?

I am trying to create a trigger that automatically fires my org's case automation rules (i.e. auto assigns it to the Case Queue so that we can use the quick actions which do not have the "Use Active Assignment Rules" feature yet to create cases). However, there is one case type that does not go to a Case Queue and therefore, I do not want to it automatically use active assignment rules as it will automatically assign it to our Default Case Owner.

My current trigger is:
trigger AssignmentRulesonCaseTrigger on Case (after insert) {
List<Id> caseIds = new List<Id>{};
if(trigger.IsAfter && trigger.isInsert ){
for (Case theCase:trigger.new)
caseIds.add(theCase.Id);        
 List<Case> cases = new List<Case>{};
for(Case c : [Select Id from Case where Id in :caseIds])
 {
 Database.DMLOptions dmo = new Database.DMLOptions();
 dmo.assignmentRuleHeader.useDefaultRule = true;
c.setOptions(dmo);
     
 cases.add(c);
             
}
Database.upsert(cases);
        }
 }
I want to make a condition saying that the trigger does not fire if the case record type is = "Member Concern" or the specific Record Type ID associated to this record type.

How do I go about doing so?

Thanks in advance for any and all help. It is GREATLY appreciated.

 
Best Answer chosen by Ricki Reay
Maharajan CMaharajan C
Hi Ricki,

Please try the below changes in your trigger:

trigger AssignmentRulesonCaseTrigger on Case (after insert) {
    List<Id> caseIds = new List<Id>{};
    Id RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Member Concern').getRecordTypeId();
    if(trigger.IsAfter && trigger.isInsert ){
        for (Case theCase:trigger.new)
        {
            if(theCase.RecordTypeId != RecordTypeId)
            {
                caseIds.add(theCase.Id);
            }
        }        
        List<Case> cases = new List<Case>{};
        
        if(caseIds.size() > 0)
        {
            for(Case c : [Select Id from Case where Id in :caseIds])
            {
                Database.DMLOptions dmo = new Database.DMLOptions();
                dmo.assignmentRuleHeader.useDefaultRule = true;
                c.setOptions(dmo);
                cases.add(c);
            }
            Database.upsert(cases);
        }
    }
 }

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Ricki,

Please try the below changes in your trigger:

trigger AssignmentRulesonCaseTrigger on Case (after insert) {
    List<Id> caseIds = new List<Id>{};
    Id RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Member Concern').getRecordTypeId();
    if(trigger.IsAfter && trigger.isInsert ){
        for (Case theCase:trigger.new)
        {
            if(theCase.RecordTypeId != RecordTypeId)
            {
                caseIds.add(theCase.Id);
            }
        }        
        List<Case> cases = new List<Case>{};
        
        if(caseIds.size() > 0)
        {
            for(Case c : [Select Id from Case where Id in :caseIds])
            {
                Database.DMLOptions dmo = new Database.DMLOptions();
                dmo.assignmentRuleHeader.useDefaultRule = true;
                c.setOptions(dmo);
                cases.add(c);
            }
            Database.upsert(cases);
        }
    }
 }

Thanks,
Maharajan.C
This was selected as the best answer
Ricki ReayRicki Reay
Hi Maharajan,

Thank you so much for such a prompt reply. I just tweaked the third line so that it is referencing the case object (as it is the Member Concern case-type that I am restricting the trigger from acting on) and it appears to be working perfectly!
 
trigger AssignmentRulesonCaseTrigger on Case (after insert) {
    List<Id> caseIds = new List<Id>{};
    Id RecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Member Concern').getRecordTypeId();
    if(trigger.IsAfter && trigger.isInsert ){
        for (Case theCase:trigger.new)
        {
            if(theCase.RecordTypeId != RecordTypeId)
            {
                caseIds.add(theCase.Id);
            }
        }        
        List<Case> cases = new List<Case>{};
        
        if(caseIds.size() > 0)
        {
            for(Case c : [Select Id from Case where Id in :caseIds])
            {
                Database.DMLOptions dmo = new Database.DMLOptions();
                dmo.assignmentRuleHeader.useDefaultRule = true;
                c.setOptions(dmo);
                cases.add(c);
            }
            Database.upsert(cases);
        }
    }
 }