You need to sign in to do that
Don't have an account?
Ricki 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:
Thanks in advance - any and all help is GREATLY appreciated.
Ricki
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
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
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
Replace the Member Type with any other record type name apart from Member Concern. code coverage 100%:
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