+ Start a Discussion
John ClevelandJohn Cleveland 

Help with Test Code Coverage on Trigger

I have a trigger and a test class and am in need of getting about 11 more lines covered and I'm in desperate need of help to get this.  Any help would be greatly appreciated.  I've listed the lines that aren't covered in the test below that I need help, as well as the trigger and test class.

Line 44-51

{ // Create the email attachment 
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName(att.Name);
efa.setBody(att.body);
efa.setContentType(att.ContentType);
efa.setInline(false);
efaList.add(efa);
 }

Line 66-71
{   
    String teamMemId = ctm.MemberId;
    teamMemId = teamMemId.substring(0, teamMemId.length()-3);  
    String userEmailId = [select Id, Email from User where Id = :teamMemId].Email;   
    toEmails.add(userEmailId);
}    

Apex Trigger:
trigger  NewCaseEmail on Case (after insert,after update) {

for(Case t : trigger.new){
// Criteria for record type
 Id RecTypeId =  [Select Id, Name from RecordType where name = 'Test Record Type' limit 1].Id;

    if (t.Status == 'New' && t.Field_1__c  == 0 && t.RecordTypeId == RecTypeId ){
// New Email
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
// Setting the from to the OrgWideEmailAddress for Shared Inbox
    OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'testemail@email.com'];    //Looking for ID for Email Address
    if ( owea.size() > 0 ) {
       email.setOrgWideEmailAddressId(owea.get(0).Id);    //Setting email from address to the ID of the Inbox
    } 

      Case cse = [SELECT id,  Case.owner.Name, Case.owner.Email,  Status, CaseNumber, Field_2__c , Field_2__c from Case WHERE Id = :t.Id];                    


        // create email content
        String CaseId = cse.id; 
        
        CaseId = CaseId.substring(0,CaseId.length()-3);
        
        String subject = 'Test Subject: ' + Cse.Field_2__c + '- Case #: ' + Cse.CaseNumber; 
        email.setSubject(subject);


		String line1 = 'Line 1. ' + '\n\n';
        String line2 = 'Line 2: ' +  cse.owner.Name + '\n';
        String line3 = 'Line 3: '+ Cse. Field_1__c  + '\n'; 
        String line4 = 'Line 4: '+ Cse.Field_2__c + '\n'; 
        
        
        
        String body = line1 + line2 + line3 + line4;
        email.setPlainTextBody(body);
        
       
        //Put your record id in ParentId
List<Attachment> attList = [SELECT id, Name, body, ContentType FROM Attachment WHERE ParentId = : CaseId];
// List of attachments handler
Messaging.EmailFileAttachment[] efaList = new List<Messaging.EmailFileAttachment>();
for(Attachment att : attList)
{ // Create the email attachment 
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName(att.Name);
efa.setBody(att.body);
efa.setContentType(att.ContentType);
efa.setInline(false);
efaList.add(efa);
 }
// Attach files to email instance
email.setFileAttachments(efaList);

email.setPlainTextBody(body);

Id caseTeamRoleId= [SELECT Id FROM CaseTeamRole WHERE Name = 'Test Role' LIMIT 1].id;

List<CaseTeamMember> catmlst = [select Id, MemberId from CaseTeamMember where TeamRoleId = :caseTeamRoleId and ParentId = :t.Id];

String [] toEmails = new List<String>();

toEmails.add(cse.owner.Email);

for(CaseTeamMember ctm : catmlst)
{   
    String teamMemId = ctm.MemberId;
    teamMemId = teamMemId.substring(0, teamMemId.length()-3);  
    String userEmailId = [select Id, Email from User where Id = :teamMemId].Email;   
    toEmails.add(userEmailId);
}    


   
        email.setToAddresses(toEmails);
        if(email != null){
            Messaging.sendEmail(new Messaging.singleEmailMessage[] {email});
                           
          }
          
    
}
}

}

Test Class:
@isTest(seeAllData=true)
private class TestNewCaseEmailTrigger  {

    
    public static Case newCse;
    
   
    static void init(){
    
    newCse = new Case();

    newCse.Status = 'New';
    newCse.Field_1__c  = 0;
    newCse.RecordTypeID = [select ID from RecordType where Name = 'Test Record Type' and sObjectType = 'Case'].ID;

    }

    static testMethod void testNewCase() {
    init();
    Test.startTest();
    insert newCse;    
        
    Case cse = [select Id, Field_1__c from Case where Id = :newCse.id];
    cse.Field_1__c  = 1;
    update cse;
        
    Attachment attach=new Attachment(); 
    attach.Name='Test Attachment'; 
    Blob bodyBlob=Blob.valueOf('Test Attachment Body'); 
    attach.body=bodyBlob; attach.parentId=newCse.Id;
    insert attach;
        
        Id userId = [SELECT Id FROM User WHERE IsActive = true AND Profile.Name = 'System Administrator' LIMIT 1].id;
  
   
        Id caseTeamRoleId= [SELECT Id FROM CaseTeamRole WHERE Name = 'Test Role' LIMIT 1].id;
        List<CaseTeamMember> catmList=new List<CaseTeamMember>();
        
        CaseTeamMember tm=new CaseTeamMember();
        tm.ParentId=newCse.Id;
        tm.MemberId=userId;
        tm.TeamRoleId =caseTeamRoleId;
        catmList.add(tm);
        
        upsert catmList;
        
        
    Case cse2 = [select Id, Field_1__c  from Case where Id = :newCse.id];
    cse2.Field_1__c  = 0;
    update cse2;
                
    Test.stopTest();
    }
   

}

 
Suresh(Suri)Suresh(Suri)
Hi John,

Your code covered all the lines for me when i tried in my dev box.Please see below

User-added imageUser-added image
Please check in your ord correct records are available or not?

Thanks,
Suresh
John ClevelandJohn Cleveland
Can you post what values you have populated on the case and with what? I have a case created with the record type and with field 1 having 1 in it and where the case team role is what's in the code.
John ClevelandJohn Cleveland
I've created cases with all scenarios and still can't get this covered.  Any help is greatly appreciated.  I don't know what I'm missing.
John ClevelandJohn Cleveland
I put this code in my developer org and it passed too.  In the sandbox I'm developin in, for some reason nothing is being returned for line 59 of the apex trigger.

I didn't even create any records in my dev org I just threw this code in and ran the test class.  Any ideas?