0

I have a trigger where when a case field called Macro is updated, it creates a case comment accordingly. I'm trying to get code coverage, I only get 21%, and I can't seem to get the following lines for each Macro option covered: ParentId Commentbody IsPublished casecommentlist(add)

Here's a smidge of test code (there's the same code for each Macro option)

static testMethod void testCaseMacros1() {
test.startTest();

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()];        
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);
   
  
     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();

}}

Here's a smidge of trigger code, again, same for each Macro option:


  

    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);
                System.debug('********************************************************************'+ cc.commentbody);
               
        }If(caseCommentList.Size() > 0){
            insert caseCommentList;
            System.debug('********************************************************************'+ caseCommentList);
            }
            
          
    
    }
    }
1

I'm almost certain that this is causing the problem:

if(CWcheckRecursive.runOnce())

Most "recursion checks" tend to mess up unit tests, because static variables are not reset between trigger calls within the same transaction. You should dump this and go with a rising-edge-based solution:

 trigger CW_DSS_CaseTrigger on Case (after update) {
    List<caseComment> caseCommentList = new List<caseComment>();
    for (Case c: Trigger.new) {
        Case oldCase = Trigger.old.get(c.Id);
        caseComment cc = new caseComment();
        // If field changed, and is a certain value, add comment.
        if( oldCase.Macros__c != c.Macros__c && 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);
        }
    }
    insert caseCommentList;
}

Note also you had the DML operation inside the loop. I moved it outside for you. Make sure you pay attention to your curly brackets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.