You need to sign in to do that
Don't have an account?

Problem in test class i am getting only 36% code coverage
Hi Salesforce Expert,
We are getting only 36% code coverage in our apex class. my Apex trigger please help me apex code is here-
And test class is

code which is not covering is below here--
We are getting only 36% code coverage in our apex class. my Apex trigger please help me apex code is here-
trigger EmailSendToOwnerUpdate on Case (before update) { // this is a list for storing email for settoAddress List<String>toAddress =new List<String>(); // this is a list for storing email for setccAddress List<String>CCAddress =new List<String>(); List<contact> lstcont=[select id ,Email,name,phone from contact]; Map<String,String> mapContact=new Map<String,String>(); list<String> ccAddresslst=new List<String>(); map<string,String> mapuser=new map<string,string>(); String ownerName=''; String baseUrl = URL.getSalesforceBaseUrl().toExternalForm(); //for getting user email and store in map where user id is key and email is value for(User u:[select id, name, Email from user]) { mapuser.put(u.id,u.email); } //for getting Contact name and store in map where id is key and name is value for(contact con:lstcont) { mapContact.put(con.id,con.name); } for(case cs:trigger.new) { if(cs.total_attachments__c>=1) { case oldcase=Trigger.oldMap.get(cs.id); if(oldcase.OwnerId!=cs.OwnerId) { if(string.valueOf(cs.OwnerId).startsWith('005')) { toAddress.add(mapuser.get(cs.OwnerId)); ownerName=Getqueuemember.getOwnername(cs.OwnerId); } else{ CCAddress=Getqueuemember.getEmailfromuser(cs.OwnerId); ownerName=Getqueuemember.getqueueOwnername(cs.OwnerId); } List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>(); List<Attachment> attlist=[select Name, Body, BodyLength from Attachment where ParentId = :cs.id ]; for (Attachment a :attlist){ Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment(); efa.setFileName(a.Name); efa.setBody(a.Body); fileAttachments.add(efa); } String Subject='Apex template Ticket #'+cs.CaseNumber+' has been assigned to '+' '+ownerName; try{ EmailTemplate et = [SELECT Id,Subject, Body FROM EmailTemplate WHERE DeveloperName ='Sebastian_page']; Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage(); semail.setSubject(Subject); semail.setSenderDisplayName('Salesforce Support'); semail.setTargetObjectId(cs.ContactId); //semail.setTemplateId(et.id); semail.setCcAddresses(CCAddress); semail.setToAddresses(toAddress); semail.setHtmlBody('***THIS IS A SYSTEM GENERATED MESSAGE! DO NOT REPLY!***<br/><br/>Please be advised that the following SalesForce ticket has been assigned to '+ownerName+'<br/><br/> Ticket #:'+cs.CaseNumber+ '<br/>Subject:'+cs.subject+'<br/><br/>Contact Name: '+mapContact.get(cs.Contactid)+'<br/>Contact Phone (primary): '+Getqueuemember.r(cs.ContactPhone)+'<br/>Contact Mobile (primary): '+Getqueuemember.r(cs.ContactMobile)+ '<br/> Contact Email:'+Getqueuemember.r(cs.ContactEmail)+'<br/><br/>Category: '+Getqueuemember.r(cs.Ticket_Category__c)+Getqueuemember.r(cs.Service_Category__c)+Getqueuemember.r(cs.Ticket_Category2__c)+ '<br/>Type: '+Getqueuemember.r(cs.Ticket_Type__c)+Getqueuemember.r(cs.Service_Type__c)+Getqueuemember.r(cs.Ticket_Type2__c)+ '<br/>Item: '+Getqueuemember.r(cs.Ticket_Item__c)+Getqueuemember.r(cs.Service_Item__c)+Getqueuemember.r(cs.Ticket_Item2__c)+ '<br/>Ticket Source: '+Getqueuemember.r(cs.Case_Source__c)+'<br/><br/><br/>Building: '+Getqueuemember.r(cs.building__c)+'<br/>Room: '+Getqueuemember.r(cs.Room_Number__c)+'<br/>Area Description: '+Getqueuemember.r(cs.Area_description__c)+ '<br/><br/><br/>Click the appropriate link below to view the ticket directly:<br/><br/>Partner Portal Users: <a href='+baseUrl+'/'+cs.id+'>'+baseUrl+'/'+cs.id+'</a><br/>BlackbeltHelp (Community Users): <a href='+Getqueuemember.getcommunityurl(cs.AccountId)+cs.id+ '>'+Getqueuemember.getcommunityurl(cs.AccountId)+cs.id+'</a><br/><br/>Description:'+cs.Description+'<br/><br/>Internal Notes: '+Getqueuemember.r(cs.Internal_Notes__c)); semail.setFileAttachments(fileAttachments); semail.setWhatId(cs.id); Messaging.sendEmail(new Messaging.SingleEmailMessage[] {semail}); } catch(exception e) {} } } } }
And test class is
@isTest public class TestEmailSendToOwner { @isTest public Static void emailTest() { // create test account Account acc = TestUtilityController.getAccount('MassBay'); insert acc; // create test contact Contact con = TestUtilityController.getContact('Test Last Name','testing@testing.com',acc.Id); insert con; // fill utility UtilityController.fillUtilities(); UtilityController.isUtility = false; // Create test cases List<Case> listCase = new List<Case>(); if(UtilityController.caseRecordType.containsKey('MassBayCC_Ticket')){ String recordTypeId = UtilityController.caseRecordType.get('MassBayCC_Ticket').Id; Case csanew = TestUtilityController.getCase(recordTypeId,con.Id,acc.Id); csanew.status = 'Action - Automated Escalation'; csanew.total_attachments__c=2; csanew.Ticket_Category2__c = 'Blackboard Learn - LMS'; csanew.Ticket_Type2__c = 'Faculty/Staff'; csanew.Ticket_Item2__c='Tutorial'; listCase.add(csanew); insert listCase; Getqueuemember.getcommunityurl(acc.id); Getqueuemember.getEmailfromuser(listCase[0].ownerId); Getqueuemember.getOwnername(listCase[0].ownerId); Getqueuemember.r(csanew.Ticket_Category2__c); } } }
code which is not covering is below here--
if(string.valueOf(cs.OwnerId).startsWith('005')) { toAddress.add(mapuser.get(cs.OwnerId)); ownerName=Getqueuemember.getOwnername(cs.OwnerId); } else{ CCAddress=Getqueuemember.getEmailfromuser(cs.OwnerId); ownerName=Getqueuemember.getqueueOwnername(cs.OwnerId); } List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>(); List<Attachment> attlist=[select Name, Body, BodyLength from Attachment where ParentId = :cs.id ]; for (Attachment a :attlist){ Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment(); efa.setFileName(a.Name); efa.setBody(a.Body); fileAttachments.add(efa); } String Subject='Apex template Ticket #'+cs.CaseNumber+' has been assigned to '+' '+ownerName; try{ EmailTemplate et = [SELECT Id,Subject, Body FROM EmailTemplate WHERE DeveloperName ='Sebastian_page']; Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage(); semail.setSubject(Subject); semail.setSenderDisplayName('Salesforce Support'); semail.setTargetObjectId(cs.ContactId); //semail.setTemplateId(et.id); semail.setCcAddresses(CCAddress); semail.setToAddresses(toAddress); semail.setHtmlBody('***THIS IS A SYSTEM GENERATED MESSAGE! DO NOT REPLY!***<br/><br/>Please be advised that the following SalesForce ticket has been assigned to '+ownerName+'<br/><br/> Ticket #:'+cs.CaseNumber+ '<br/>Subject:'+cs.subject+'<br/><br/>Contact Name: '+mapContact.get(cs.Contactid)+'<br/>Contact Phone (primary): '+Getqueuemember.r(cs.ContactPhone)+'<br/>Contact Mobile (primary): '+Getqueuemember.r(cs.ContactMobile)+ '<br/> Contact Email:'+Getqueuemember.r(cs.ContactEmail)+'<br/><br/>Category: '+Getqueuemember.r(cs.Ticket_Category__c)+Getqueuemember.r(cs.Service_Category__c)+Getqueuemember.r(cs.Ticket_Category2__c)+ '<br/>Type: '+Getqueuemember.r(cs.Ticket_Type__c)+Getqueuemember.r(cs.Service_Type__c)+Getqueuemember.r(cs.Ticket_Type2__c)+ '<br/>Item: '+Getqueuemember.r(cs.Ticket_Item__c)+Getqueuemember.r(cs.Service_Item__c)+Getqueuemember.r(cs.Ticket_Item2__c)+ '<br/>Ticket Source: '+Getqueuemember.r(cs.Case_Source__c)+'<br/><br/><br/>Building: '+Getqueuemember.r(cs.building__c)+'<br/>Room: '+Getqueuemember.r(cs.Room_Number__c)+'<br/>Area Description: '+Getqueuemember.r(cs.Area_description__c)+ '<br/><br/><br/>Click the appropriate link below to view the ticket directly:<br/><br/>Partner Portal Users: <a href='+baseUrl+'/'+cs.id+'>'+baseUrl+'/'+cs.id+'</a><br/>BlackbeltHelp (Community Users): <a href='+Getqueuemember.getcommunityurl(cs.AccountId)+cs.id+ '>'+Getqueuemember.getcommunityurl(cs.AccountId)+cs.id+'</a><br/><br/>Description:'+cs.Description+'<br/><br/>Internal Notes: '+Getqueuemember.r(cs.Internal_Notes__c)); semail.setFileAttachments(fileAttachments); semail.setWhatId(cs.id); Messaging.sendEmail(new Messaging.SingleEmailMessage[] {semail}); } catch(exception e) {} } } } }
Apex Class:
Apex Trigger:
Apex Test Class:
Thanks for help but we haven't got sucess in code coverage . Still we got 36% code coverage after changing apex code acodring you.
In the additional test code I gave yesterday, I noticed that there were some errors. For example, I mentioned Owner instead of OwnerID which I fixed in the code below. I also noticed some other discrepancies which I fixed below. Another thing I also noticed is we didn't create any test attachments for the test cases we created. I know it was stated that the total attachments equals 2 but when your code run the SOQL: List<Attachment> attlist=[select Name, Body, BodyLength from Attachment where ParentId = :cs.id ]; I believe it will return a null result. Definitely something we have to test. With that being said, with the fixes to the code I made below, can you tell me if the changes I made increased the test code percentage? It may not be 100% yet but I just want to ensure that we are in the right direction with these changes since there a lot of moving parts to it. Sorry again for the error and typos earlier.
Apex Test Class (Corrected):
After some changes in your code we are got 86% code coverage . Thanks a lot Christan