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
StaciStaci 

test class for case comments

I have a trigger that adds a case comment depending on the field Macro option chosen.  Here's the trigger snippet 
trigger CW_DSS_CaseTrigger on Case (after update) {
  if(CWcheckRecursive.runOnce())
    {
    
   
   List<caseComment> caseCommentList = new List<caseComment>();
    for (Case c: Trigger.new)
    {
        
        caseComment cc = new caseComment();
        if(c.Macros__c == 'Application Support information'){
                cc.parentid = c.ID;
                cc.commentbody = 'For application (DSSi and Relay Server) support and troubleshooting requests, please provide the following information.'+'\n' +
                                    'Minimum DSS Ticket Information requirements:'+'\n' +
                                    'Site Name:' +'\n' +
                                    'Site Contact: (For possible Site IT related issues)' +'\n' +
                                    'DSSi Software Version:' +'\n' +
                                    'Problem/Inquiry:' +'\n' +
                                    'Troubleshooting Steps Taken:' +'\n' +
                                    'This information is required with all DSS support requests. Failure to provide this requested information may delay your request.';
                cc.ispublished = true;
                casecommentlist.add(cc);
               
        }
How do I get this covered in a test class?  Here's a snippet of my test class
private class TestCW_DSS_CaseTrigger{
    
    
    
    static testMethod void CW_DSS_CaseTriggerTest(){
        Id caseRecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Detect - Fatigue Management (DSS)').getRecordTypeId();
        ID accRecordTypeID = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Worksite').getRecordTypeId();
        Account a1 = new Account(Name='Carlin Test', RecordTypeID = accRecordTypeID, DSS_Account__c = true);
        insert a1;
      
        Case cs1 = new Case();
cs1.RecordTypeId = caseRecordTypeId;
cs1.Site_Dealer_Name_DSS__c = a1.Id;
cs1.OwnerId = UserInfo.getUserId();
cs1.CW_Type__c = 'Question';
cs1.Status = 'New';
cs1.Subject = 'This is my case';
cs1.Description = 'This is my description';
cs1.Incident_Start__c=Date.today();
cs1.Priority = 'Normal';
cs1.Subsystem_Type__c = 'Detect Analytics - API';
insert cs1;
    }

    static testMethod void testCaseMacros1() {
    Id ortId = [SELECT Id, Name FROM RecordType WHERE Name='Detect - Fatigue Management (DSS)'].id;
    RecordType rtAcct = [select Id from RecordType WHERE Name = 'Worksite' and SobjectType = 'Account' limit 1];
    //create account
    Account a1 = new Account(Name='Carlin Test', RecordType = rtAcct, DSS_Account__c = true);
    User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];        
    test.startTest();
    System.runAs (thisUser) {
        
        Case cse = new Case(RecordTypeId = ortId, Site_Dealer_Name_DSS__c = a1.Id, CW_Type__c = 'Incident', Subsystem_Type__c = 'Detect Analytics - API',
        Status = 'New', Subject = 'This is my case', Description = 'This is my description', Incident_Start__c=Date.today());
        Database.insert(cse);
        cse.Macros__c = 'Application Support information';
        Database.update(cse);
        cse.Macros__c = '';
        Database.update(cse);
        
      test.stopTest();

    }}


 
Maharajan CMaharajan C
Hi Staci,

The below test class is fine:
 
private class TestCW_DSS_CaseTrigger{
    
    static testMethod void CW_DSS_CaseTriggerTest(){
        Id caseRecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Detect - Fatigue Management (DSS)').getRecordTypeId();
        ID accRecordTypeID = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Worksite').getRecordTypeId();
        
		// Add all the mandatory fields to create the Account
		Account a1 = new Account(Name='Carlin Test', RecordTypeID = accRecordTypeID, DSS_Account__c = true);
        insert a1;
      
		// Add all the mandatory fields to create the Case
        Case cs1 = new Case();
		cs1.RecordTypeId = caseRecordTypeId;
		cs1.Site_Dealer_Name_DSS__c = a1.Id;
		cs1.CW_Type__c = 'Question';
		cs1.Status = 'New';
		cs1.Subject = 'This is my case';
		cs1.Description = 'This is my description';
		cs1.Incident_Start__c=Date.today();
		cs1.Priority = 'Normal';
		cs1.Subsystem_Type__c = 'Detect Analytics - API';
		insert cs1;
		
		test.startTest();
			cs1.Macros__c = 'Application Support information';
			update cs1;
			
			List<caseComment> caseCommentList = [Select Id from caseComment];
			system.assert(caseCommentList.size()>0, 'caseComment not created');
		test.stopTest();
    }
	
}

If the above test is failed means there is problem in the code.

Put debug statements inside the trigger then run the test class and verify the debug statements are printed or not. Let us know the lines which are not covering in trigger.

Run the test class from Developer console and see the lines which are not covered in red color . 
https://help.salesforce.com/articleView?id=code_dev_console_tests_coverage.htm&type=5

Thanks,
Maharajan.C
StaciStaci
@Maharajan C here's the lines it says aren't covered
cc.parentid = c.ID;
                cc.commentbody = 'For application (DSSi and Relay Server) support and troubleshooting requests, please provide the following information.'+'\n' +
                                    'Minimum DSS Ticket Information requirements:'+'\n' +
                                    'Site Name:' +'\n' +
                                    'Site Contact: (For possible Site IT related issues)' +'\n' +
                                    'DSSi Software Version:' +'\n' +
                                    'Problem/Inquiry:' +'\n' +
                                    'Troubleshooting Steps Taken:' +'\n' +
                                    'This information is required with all DSS support requests. Failure to provide this requested information may delay your request.';
                cc.ispublished = true;

i will try the debug statements 
Maharajan CMaharajan C
Staci,

It's looks like the   cs1.Macros__c = 'Application Support information' not getting updated. May be some other trigger is overiding Macros in case.

Add the below assert:

test.startTest();
    cs1.Macros__c = 'Application Support information';
    update cs1;
    
    Case cs = [Select Id,Macros__c from case where Id=:cs1.Id limit 1];
    system.debug('cs --> ' + cs);
    system.debug('cs --> ' + cs.Macros__c);

    system.assertEquals('Application Support information' , cs.Macros__c);
    
    List<caseComment> caseCommentList = [Select Id from caseComment];
    system.assert(caseCommentList.size()>0, 'caseComment not created');
test.stopTest();

Thanks,
​​​​​​​Maharajan.C
StaciStaci
@Maharajan C
I added debug to my code and the comment is getting added, found it in the logs:
09:02:57.0 (582653387)|USER_DEBUG|[23]|DEBUG|********************************************************************For application (DSSi and Relay Server) support and troubleshooting requests, please provide the following information. Minimum DSS Ticket Information requirements: Site Name: Site Contact: (For possible Site IT related issues) DSSi Software Version: Problem/Inquiry: Troubleshooting Steps Taken: This information is required with all DSS support requests. Failure to provide this requested information may delay your request.


I added your segments and this is what I get when I execute the test:
static testMethod void testCaseMacros1() {
    Id ortId = [SELECT Id, Name FROM RecordType WHERE Name='Detect - Fatigue Management (DSS)'].id;
    RecordType rtAcct = [select Id from RecordType WHERE Name = 'Worksite' and SobjectType = 'Account' limit 1];
    //create account
    Account a1 = new Account(Name='Carlin Test', RecordType = rtAcct, DSS_Account__c = true);
    User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];        
    //test.startTest();
    //System.runAs (thisUser) {
        
        Case cse = new Case(RecordTypeId = ortId, Site_Dealer_Name_DSS__c = a1.Id, CW_Type__c = 'Incident', Subsystem_Type__c = 'Detect Analytics - API',
        Status = 'New', Subject = 'This is my case', Description = 'This is my description', Incident_Start__c=Date.today());
        Database.insert(cse);
         test.startTest();
    System.runAs (thisUser) {
        cse.Macros__c = 'Application Support information';
        Database.update(cse);
        //cse.Macros__c = '';
       // Database.update(cse);
         system.debug('cs --> ' + cse);
    system.debug('cs --> ' + cse.Macros__c);
    system.assertEquals('Application Support information' , cse.Macros__c);
         List<caseComment> caseCommentList = [Select Id from caseComment];
            system.assert(caseCommentList.size()>0, 'caseComment not created'); 
        
       
      test.stopTest();

    }}

Test failed with this error:
System.AssertException: Assertion Failed: caseComment not created
 
Maharajan CMaharajan C
Are you getting this log while running test class ? or you are updating the record from salesforce UI.

Try running the test class from dev console. In developer console log you can see the test class debug also. 

Thanks,
Maharajan.C
StaciStaci
@Maharajan C
Yes it was updating the record.  I've run it in dev console, here's what I get

13:06:44.375 (2720693337)|STATEMENT_EXECUTE|[65]
13:06:44.375 (2720708187)|HEAP_ALLOCATE|[65]|Bytes:7
13:06:44.375 (2720946668)|HEAP_ALLOCATE|[65]|Bytes:309
13:06:44.375 (2720981782)|HEAP_ALLOCATE|[65]|Bytes:316
13:06:44.375 (2721019794)|USER_DEBUG|[65]|DEBUG|cs --> Case:{RecordTypeId=0121O000001WL0uQAG, Site_Dealer_Name_DSS__c=null, CW_Type__c=Incident, Subsystem_Type__c=Detect Analytics - API, Status=New, Subject=This is my case, Description=This is my description, Incident_Start__c=2021-01-07 00:00:00, Id=5001b00000AloiuAAB, Macros__c=Application Support information}
13:06:44.375 (2721034752)|STATEMENT_EXECUTE|[66]
13:06:44.375 (2721071492)|HEAP_ALLOCATE|[66]|Bytes:38
13:06:44.375 (2721108056)|USER_DEBUG|[66]|DEBUG|cs --> Application Support information
13:06:44.375 (2721118805)|STATEMENT_EXECUTE|[67]
13:06:44.375 (2721164091)|STATEMENT_EXECUTE|[68]
13:06:44.375 (2721168763)|HEAP_ALLOCATE|[68]|Bytes:26
13:06:44.375 (2721201865)|HEAP_ALLOCATE|[68]|Bytes:4
13:06:44.375 (2721537617)|SOQL_EXECUTE_BEGIN|[68]|Aggregations:0|SELECT Id FROM caseComment
13:06:45.437 (3437232443)|SOQL_EXECUTE_END|[68]|Rows:0
13:06:45.437 (3437306127)|HEAP_ALLOCATE|[68]|Bytes:4
13:06:45.437 (3437349174)|HEAP_ALLOCATE|[68]|Bytes:0
13:06:45.437 (3437414386)|HEAP_ALLOCATE|[68]|Bytes:4
13:06:45.437 (3437445022)|VARIABLE_SCOPE_BEGIN|[68]|caseCommentList|List<CaseComment>|true|false
13:06:45.437 (3437484385)|VARIABLE_ASSIGNMENT|[68]|caseCommentList|[]|0x5e72596c
13:06:45.437 (3437493967)|STATEMENT_EXECUTE|[69]
13:06:45.437 (3437574331)|HEAP_ALLOCATE|[69]|Bytes:23
13:06:45.437 (3437689957)|EXCEPTION_THROWN|[69]|System.AssertException: Assertion Failed: caseComment not created
13:06:45.437 (3437928243)|HEAP_ALLOCATE|[69]|Bytes:45
13:06:45.437 (3445764556)|FATAL_ERROR|System.AssertException: Assertion Failed: caseComment not created

Class.TestCW_DSS_CaseTrigger.testCaseMacros1: line 69, column 1
13:06:45.437 (3445789972)|FATAL_ERROR|System.AssertException: Assertion Failed: caseComment not created

Class.TestCW_DSS_CaseTrigger.testCaseMacros1: line 69, column 1
13:06:45.445 (3445800885)|CUMULATIVE_LIMIT_USAGE
13:06:45.445 (3445800885)|LIMIT_USAGE_FOR_NS|(default)|
Maharajan CMaharajan C
Hi Staci,

I hope you are having the below line in trigger. DML on Case Comment object in Trigger after the for loop completed.

If(caseCommentList.Size() > 0)
    insert caseCommentList;

Thanks,
Maharajan.C
StaciStaci
HI @Maharajan C

I only had insert caseCommentList;

I've added the If(caseCommentList.Size()>0) part but still the same results