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
Ricki ReayRicki Reay 

APEX Test Class Help - Create Test Class for Custom APEX Trigger on Case Object

Hello All, 

I am working on deploying a custom APEX trigger on the Case object that automatically initiates my org's active case assignment rules whenever a new case is created (except cases created with the record type name = "Member Concern"). In our configured org., we are having users create most new cases through Action Buttons, which unfortunately has the limitation that it cannot default the assignment rules and therefore, I require this apex trigger.

Here is my current trigger code that I have tested in my sandbox org: 
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);
        }
    }
 }
I am very new to Apex and I am having difficulty navigating the creation of a custom test class that will allow me to deploy the above trigger into Production. Could someone provide me guidance/assistance on how to write a test class that will provide the coverage needed to deploy this trigger?

Thanks in advance - any and all help is GREATLY appreciated.

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

Try the below test class:

@isTest
public class AssignmentRulesonCaseTriggerTest {
    static testmethod void testAssignmentRulesonCaseTrigger()
    {
        Case cs = new case(Status='New',Origin='Phone');
        // Please include if there is any mandatory fields  is required to create case record.
        insert cs;
    }
}


============

If the above is not giving proper coverage then the below one :

@isTest
public class AssignmentRulesonCaseTriggerTest {
    static testmethod void testAssignmentRulesonCaseTrigger()
    {
        //  In below line replace the Sample Type  with any other record type name apart from Member Concern
        Id RecTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Sample Type').getRecordTypeId();
        Case cs = new case(Status='New',Origin='Phone',RecordTypeId=RecTypeId);
        // Please include if there is any other mandatory fields is required to create case record.
        insert cs;
    }



Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
HI Ricki,

Try the below test class:

@isTest
public class AssignmentRulesonCaseTriggerTest {
    static testmethod void testAssignmentRulesonCaseTrigger()
    {
        Case cs = new case(Status='New',Origin='Phone');
        // Please include if there is any mandatory fields  is required to create case record.
        insert cs;
    }
}


============

If the above is not giving proper coverage then the below one :

@isTest
public class AssignmentRulesonCaseTriggerTest {
    static testmethod void testAssignmentRulesonCaseTrigger()
    {
        //  In below line replace the Sample Type  with any other record type name apart from Member Concern
        Id RecTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Sample Type').getRecordTypeId();
        Case cs = new case(Status='New',Origin='Phone',RecordTypeId=RecTypeId);
        // Please include if there is any other mandatory fields is required to create case record.
        insert cs;
    }



Thanks,
Maharajan.C
This was selected as the best answer
Ajay K DubediAjay K Dubedi
Hi Ricky,

Replace the Member Type with any other record type name apart from Member Concern. code coverage 100%:
 
@isTest
private class AssignmentRulesonCaseTriggerTest {
    @isTest static void AssignmentRulesonCaseMethod()
    {
        Id RecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Member').getRecordTypeId();
        Case caseObj = new Case();
        caseObj.Status = 'Working';
        caseObj.Origin = 'Phone';
        caseObj.RecordTypeId =RecordTypeId; 
        insert caseObj;
    }

}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com