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
Nancy GreshamNancy Gresham 

Assistance with test class for case trigger

Hi All! 
I'm having trouble writing a test for this trigger.  I can get 65% coverage, but not more.  I'm new to this, so I'm sure there is something small I'm missing somewhere.  Any help would be awesome! Thanks!
trigger CaseResolutionTrigger on Case (after update) 
{
    Set<String> caseIdSet = new Set<String>(); 
    if(CaseResolutionTriggerHandler.isFirstTime)
    {
        CaseResolutionTriggerHandler.isFirstTime = false;
        
	Set<ID> RTIds = new Set<ID>();  //set of Record Type IDs to match
        
        List<CaseComment> CommentList = new List<CaseComment>();
        List<Time_Entry__c> TEList = new List<Time_Entry__c>();
     
        for(RecordType rt : [select id from Recordtype where SobjectType='Case' and ((Name like 'e-MDs%') or (Name like 'Richmond%'))]){
  		  RTIds.add(rt.id);
			}   
    for(Case ca : Trigger.New)    {
    
      //Only when Resolution is changed and only for specified Record Types - Insert Comment and Time Entry     
	if(RTIds.contains(ca.Recordtypeid) && ca.Resolution__c != '' && ca.Resolution__c != Trigger.oldmap.get(ca.id).Resolution__c){
        CaseComment com = new CaseComment();
        com.ParentId = ca.id;
        com.CommentBody= 'RESOLUTION:  '+ '\n' + ca.Resolution__c;
        CommentList.add(com);
        
        Time_Entry__c te = new Time_Entry__c();
        te.Case__c  = ca.id;
        te.Minutes_Spent__c = ca.Minutes_SMB__c;
        TEList.add(te);
         
}
        //clear the number passed from the case to the Time Entry
      Case c = [Select id, Minutes_SMB__c from Case where id = :ca.id];
        c.Minutes_SMB__c = NULL;
        update c;
                            }

		Insert CommentList;
        Insert TEList;
        

}
}

 
Best Answer chosen by Nancy Gresham
Amit Chaudhary 8Amit Chaudhary 8


Try to update your code like below
@isTest
public class TestCaseResolutionTrigger {
    
	static testMethod void myCaseResolutionTest()    
	{
        Account testAccount = new Account();
        testAccount.Name = 'Test Account';
        insert testAccount;

        Id rt = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Richmond Support Case').getRecordTypeId();

        Case myCase = new Case();   
			myCase.AccountId = testAccount.id;
			myCase.RecordTypeId = rt;
			myCase.Status = 'New';
			myCase.Subject = 'Test Case';
			myCase.Description = 'Test Case Info';
        insert myCase; 
        
        
        Test.startTest();

			CaseResolutionTriggerHandler.isFirstTime= true;
			//update test case
			myCase.Status = 'Resolved';
			myCase.Resolution__c = 'new hello world';
			myCase.Minutes_SMB__c = 3;
			update myCase; 
        
        Test.stopTest();
	}
}

Try to bulky your Trigger as well
trigger CaseResolutionTrigger on Case (before update) 
{
    Set<String> caseIdSet = new Set<String>(); 
	
    if(CaseResolutionTriggerHandler.isFirstTime)
    {
        CaseResolutionTriggerHandler.isFirstTime = false;
		Set<ID> RTIds = new Set<ID>();  //set of Record Type IDs to match
        List<CaseComment> CommentList = new List<CaseComment>();
        List<Time_Entry__c> TEList = new List<Time_Entry__c>();
     
        for(RecordType rt : [select id from Recordtype where SobjectType='Case' and ((Name like 'e-MDs%') or (Name like 'Richmond%'))])
		{
			RTIds.add(rt.id);
		}   
		
		for(Case ca : Trigger.New)    
		{
			if(RTIds.contains(ca.Recordtypeid) && ca.Resolution__c != '' && ca.Resolution__c != Trigger.oldmap.get(ca.id).Resolution__c)
			{
				CaseComment com = new CaseComment();
				com.ParentId = ca.id;
				com.CommentBody= 'RESOLUTION:  '+ '\n' + ca.Resolution__c;
				CommentList.add(com);

				Time_Entry__c te = new Time_Entry__c();
				te.Case__c  = ca.id;
				te.Minutes_Spent__c = ca.Minutes_SMB__c;
				TEList.add(te);

			}
			c.Minutes_SMB__c = NULL;
		}
		
		if(CommentList.size() > 0 )
		{
			Insert CommentList;
			Insert TEList;
		}	
	}
}

Try to avoid SOQL inside for loop.

 

All Answers

Hemant_SoniHemant_Soni
Hi,
Can you please share your test class so i can make necessary change in that.
Thanks
Hemant
Nancy GreshamNancy Gresham
Thanks Hemant! Here is what I have at the moment.  
@isTest
public class TestCaseResolutionTrigger {
    
    static Account testAccount;
    static CaseComment ccom;
    static Time_Entry__c tte;
	static Case myCase;
    
	static testMethod void myCaseResolutionTest()    {
      
        testAccount = new Account();
        testAccount.Name = 'Test Account';
        insert testAccount;

        //insert test case
        Id rt = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Richmond Support Case').getRecordTypeId();
		List<Case> cases = new List<Case>();
        myCase = new Case();   
        myCase.AccountId = testAccount.id;
        myCase.RecordTypeId = rt;
        myCase.Status = 'New';
        myCase.Subject = 'Test Case';
        myCase.Description = 'Test Case Info';
        insert myCase; 
        
		List<Time_Entry__c> tTeList = new List<Time_Entry__c>();
        
        Test.startTest();
		
        //update test case
        myCase.Status = 'Resolved';
        myCase.Resolution__c = 'new hello world';
        myCase.Minutes_SMB__c = 3;
        update myCase; 
        
        Time_Entry__c tTe = new Time_Entry__c();
        tTe.Case__c = myCase.Id;
        tTe.Minutes_Spent__c = myCase.Minutes_SMB__c;
        tTEList.add(tte);     
        insert tTeList;
  
        Test.stopTest();

}
    }
Amit Chaudhary 8Amit Chaudhary 8


Try to update your code like below
@isTest
public class TestCaseResolutionTrigger {
    
	static testMethod void myCaseResolutionTest()    
	{
        Account testAccount = new Account();
        testAccount.Name = 'Test Account';
        insert testAccount;

        Id rt = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Richmond Support Case').getRecordTypeId();

        Case myCase = new Case();   
			myCase.AccountId = testAccount.id;
			myCase.RecordTypeId = rt;
			myCase.Status = 'New';
			myCase.Subject = 'Test Case';
			myCase.Description = 'Test Case Info';
        insert myCase; 
        
        
        Test.startTest();

			CaseResolutionTriggerHandler.isFirstTime= true;
			//update test case
			myCase.Status = 'Resolved';
			myCase.Resolution__c = 'new hello world';
			myCase.Minutes_SMB__c = 3;
			update myCase; 
        
        Test.stopTest();
	}
}

Try to bulky your Trigger as well
trigger CaseResolutionTrigger on Case (before update) 
{
    Set<String> caseIdSet = new Set<String>(); 
	
    if(CaseResolutionTriggerHandler.isFirstTime)
    {
        CaseResolutionTriggerHandler.isFirstTime = false;
		Set<ID> RTIds = new Set<ID>();  //set of Record Type IDs to match
        List<CaseComment> CommentList = new List<CaseComment>();
        List<Time_Entry__c> TEList = new List<Time_Entry__c>();
     
        for(RecordType rt : [select id from Recordtype where SobjectType='Case' and ((Name like 'e-MDs%') or (Name like 'Richmond%'))])
		{
			RTIds.add(rt.id);
		}   
		
		for(Case ca : Trigger.New)    
		{
			if(RTIds.contains(ca.Recordtypeid) && ca.Resolution__c != '' && ca.Resolution__c != Trigger.oldmap.get(ca.id).Resolution__c)
			{
				CaseComment com = new CaseComment();
				com.ParentId = ca.id;
				com.CommentBody= 'RESOLUTION:  '+ '\n' + ca.Resolution__c;
				CommentList.add(com);

				Time_Entry__c te = new Time_Entry__c();
				te.Case__c  = ca.id;
				te.Minutes_Spent__c = ca.Minutes_SMB__c;
				TEList.add(te);

			}
			c.Minutes_SMB__c = NULL;
		}
		
		if(CommentList.size() > 0 )
		{
			Insert CommentList;
			Insert TEList;
		}	
	}
}

Try to avoid SOQL inside for loop.

 
This was selected as the best answer
Nancy GreshamNancy Gresham
Thank you so much!