You need to sign in to do that
Don't have an account?
Gary Payne 9
Need a sample Test Class for Case Trigger that updates the number of related cases
I have a Case Trigger that updates a custom number field, Number_of_Related_Cases__c., that I obtained from the Success Community. Following is the Trigger code:
trigger Case_Count_Related_Cases on Case (after insert, after update, after delete) {
//build list of parent case ids
Set<Id> ParentIds = new Set<Id>();
//If insert or update, use trigger.new
if (trigger.isinsert || trigger.isupdate) {
//if case has parent, add to set of parentids
for (Case c: Trigger.New) {
if(c.ParentId != null) {
ParentIds.add(c.ParentId);
}
//if case previously had parent, but no longer does, also add so we can recalc
if (trigger.isupdate) {
if (trigger.oldmap.get(c.id).parentid != null && c.ParentId == null) {
ParentIds.add(trigger.oldmap.get(c.id).parentid);
}
}
}
}
//if delete, make sure we update parent
else if (trigger.isdelete) {
for (Case c: Trigger.Old) {
if(c.ParentId != null) {
ParentIds.add(c.ParentId);
}
}
}
//then query for parent cases, and do inner query for related cases
List<Case> pcount = [Select Id, (Select Id from Cases) From CASE where Id IN :ParentIds];
//use size of inner cases to set number count
for (Case pc: pcount) {
pc.Number_of_Related_Cases__c = pc.cases.size();
}
//update parents
update pcount;
}
I am not a developer and need to create a Test Class for this trigger. What would a Test Class for this Trigger look like?
trigger Case_Count_Related_Cases on Case (after insert, after update, after delete) {
//build list of parent case ids
Set<Id> ParentIds = new Set<Id>();
//If insert or update, use trigger.new
if (trigger.isinsert || trigger.isupdate) {
//if case has parent, add to set of parentids
for (Case c: Trigger.New) {
if(c.ParentId != null) {
ParentIds.add(c.ParentId);
}
//if case previously had parent, but no longer does, also add so we can recalc
if (trigger.isupdate) {
if (trigger.oldmap.get(c.id).parentid != null && c.ParentId == null) {
ParentIds.add(trigger.oldmap.get(c.id).parentid);
}
}
}
}
//if delete, make sure we update parent
else if (trigger.isdelete) {
for (Case c: Trigger.Old) {
if(c.ParentId != null) {
ParentIds.add(c.ParentId);
}
}
}
//then query for parent cases, and do inner query for related cases
List<Case> pcount = [Select Id, (Select Id from Cases) From CASE where Id IN :ParentIds];
//use size of inner cases to set number count
for (Case pc: pcount) {
pc.Number_of_Related_Cases__c = pc.cases.size();
}
//update parents
update pcount;
}
I am not a developer and need to create a Test Class for this trigger. What would a Test Class for this Trigger look like?
Test class are very simple you just have to create the dummy records of the object which are been used the your class or trigger.
Simple Example of trigger and test class for that trigger.
Trigger on Contact Test Class(Apex class): By looking at the above example , create a text class and For your test class you have to follow the following steps:
Create a method Fisrt create a Case record.. /*From above code your delete event will be fired */
As you will write the code you should write test.startTest() at the beginning and Test.stopTest() at the end as shown in the above example.
This comes under best practices of writing test classes and for further knowledge you can go to the below link:-
https://trailhead.salesforce.com/trails/force_com_dev_beginner/modules/apex_testing
Regards
Ajay,
Please mark my answer as a solution if it is helpful.
All Answers
Test class are very simple you just have to create the dummy records of the object which are been used the your class or trigger.
Simple Example of trigger and test class for that trigger.
Trigger on Contact Test Class(Apex class): By looking at the above example , create a text class and For your test class you have to follow the following steps:
Create a method Fisrt create a Case record.. /*From above code your delete event will be fired */
As you will write the code you should write test.startTest() at the beginning and Test.stopTest() at the end as shown in the above example.
This comes under best practices of writing test classes and for further knowledge you can go to the below link:-
https://trailhead.salesforce.com/trails/force_com_dev_beginner/modules/apex_testing
Regards
Ajay,
Please mark my answer as a solution if it is helpful.
Regards,
Gary Payne