You need to sign in to do that
Don't have an account?
nikkey
Test class for Apex Trigger fails
Can any one help me out on this?how to write a Test class for the trigger.Any help is very much appreciated.
Trigger:
Test Class :
Trigger:
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; }
Test Class :
@istest public class TestReparentComment{ static TestMethod void testcomment(){ list<casecomment> ccomment = new list<casecomment>(); for(integer i=0;i<200;i++){ case c= new case(subject='RE:'); insert c casecomment cc = new casecomment(cc.commentBody = c.Description) Parentid=c.id; insert cc; List<case> insertedcases =[Select Name ,Subject,status from case Where Id in :c.Id Order by CreatedDate]; for(case c :insertedcases){ system.assertEquals( 'NotClosed',c.status); } } }
You are doing the insert within the loop which will cause salesforce governor limit exception. You can try the below code for your scenario.
list<casecomment> ccomment = new list<casecomment>();
list<Case> caseList = new List<Case>();
for(integer i=0;i<200;i++){
case c= new case(subject='RE:');
caseList.add(c);
}
for(Case c : caseList){
casecomment cc = new casecomment(cc.commentBody = c.Description)
Parentid=c.id;
ccomment.add(cc);
}
insert ccomment;
List<case> insertedcases =[Select Name ,Subject,status from case Where Id in :c.Id Order byCreatedDate];
for(case c :insertedcases){
system.assertEquals(14 'NotClosed',c.status);
}
Regards,
Prakash B