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
nikkeynikkey 

How to Write TEST Classes for APEX TRIGGER???

Can any one help me out in writting test classes for the triggers.I shall appreciate your help.


trigger ReparentComment on CaseComment (before insert) {
Map<String, Case> newCaseMap = new Map<String, Case>();
Set<Id> caseIds = new Set<Id>();
Map<Id, List<CaseComment>> ccMap = new Map<Id, List<CaseComment>>();
List<CaseComment> newCCList = new List<CaseComment>();
for(CaseComment cc : trigger.new) {
caseIds.add(cc.ParentId);
List<CaseComment> ccList = ccMap.get(cc.ParentId);
if(ccList == null) ccList = new List<CaseComment>();
ccList.add(cc); ccMap.put(cc.ParentId, ccList);
}
for(Case c : [Select Id, Subject From Case Where Id in :caseIds Order by CreatedDate])
if(newCaseMap.containsKey(c.Subject)) for(CaseComment cc : ccMap.get(c.Id)) {
CaseComment newCC = cc.clone();
newCCList.add(newCC);
newCC.ParentId = newCaseMap.get(c.Subject).Id;
}
else newCaseMap.put(c.Subject, c);
for(Case c : [Select Id, Subject From Case Where Subject in :newCaseMap.keySet() And Id not in :caseIds And Status != 'Closed']) for(CaseComment cc : ccMap.get(newCaseMap.get(c.Subject).Id)) {
CaseComment newCC = cc.clone();
newCCList.add(newCC);
newCC.ParentId = c.Id;
}
insert newCCList;
}
Thanks in Advance
Parvinder SinghParvinder Singh
I will give you the concept, to write code for your trigger replicate the DML scenario in your test class, for ex if your trigger is firing on inster of case comment, in your test class you will have to create all the parent data before you can create a case comment, then all you have to do is do an insert on your case comment which should fire the trigger and cover your trigger. Thanks!
Deepak Kumar ShyoranDeepak Kumar Shyoran
For that you need to insert a CaseComment record inside your test class and as your trigger is on insert event of Case Comments it'll execute and will cover unit testing for this trigger.
For more help of how to write test class visit
​https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_test.htm