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 

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:
 
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);
}
}
}

 
PrakashbPrakashb
Hi Nikkey,

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
nikkeynikkey
Hi PrakashB , Thanks for your reply. The code works fine now ,but when i moved the code into production its showing code coverage as 0% while in Sandbox it is showing as 62%.Will the code work in production now.Any help very much appreciated.