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
AUCTUS AdministratorAUCTUS Administrator 

Apex Code is working but I dont get Code Coverage by testing

Hallo, 

since I am new to Apex and specially this forum please dont be so hard on me. 

I am trying to get this to work: 

(Short explenation of the porpuse: When sending out a Mail with a specific Subject, the contact field "Recruiting_Status__c" should be updated on the contact related to the task (WhoId). 

Note: I tried to do include the contact to my list and update on two different ways as you may notice (last one is different), but code coverage keeps telling me somethings wrong.. 

Trigger: 
trigger RecEmailTrigger on Task (after insert) {
    List<Contact> contactsToUpdate = new List<Contact>();
    
    for(Task t : Trigger.new) {
        if (t.WhoId != null){
        
        List<Contact> Con = [SELECT Id, Name, Recruiting_Status__c 
                             FROM Contact 
                             WHERE Id = :t.WhoID];
        
        if(t.subject.equals('Email: Anfrage auf vollständige Bewerbungsunterlagen')) {
        contactsToUpdate.add(new Contact(Id = t.WhoId, Recruiting_Status__c = 'Requested complete set of documents'));}     
        
        else if(t.subject.equals('Email: Das AUCTUS Recruiting Erlebnis: selbständige Terminkoordination für ein erstes Vorabgespräch')) {
        contactsToUpdate.add(new Contact(Id = t.WhoId, Recruiting_Status__c = 'In scheduling process for initial conversation'));}
        
        else if(t.subject.equals('Email: Das AUCTUS Recruiting Erlebnis: selbständige Terminkoordination für die CaseStudy@Home')) {
        contactsToUpdate.add(new Contact(Id = t.WhoId, Recruiting_Status__c = 'In scheduling process for case study'));} 
        
        else if((t.subject.equals('Email: Vielen Dank für Ihre Bewerbung') || t.subject.equals('Email: Vielen Dank für Ihre Kontaktaufnahme'))
        && (Con[0].Recruiting_Status__c.contains('preselected') || Con[0].Recruiting_Status__c.contains('Requested complete') )) {
        contactsToUpdate.add(new Contact(Id = t.WhoId, Recruiting_Status__c = 'Declined after preselection'));}  
        
        else if((t.subject.equals('Email: Vielen Dank für Ihre Bewerbung') || t.subject.equals('Email: Vielen Dank für Ihre Kontaktaufnahme'))
        && Con[0].Recruiting_Status__c.contains('initial conversation')) {
        contactsToUpdate.add(new Contact(Id = t.WhoId, Recruiting_Status__c = 'Declined after initial conversation'));}  
        
        else if((t.subject.equals('Email: Vielen Dank für Ihre Bewerbung') || t.subject.equals('Email: Vielen Dank für Ihre Kontaktaufnahme'))
        && Con[0].Recruiting_Status__c.contains('case study')) {
        contactsToUpdate.add(new Contact(Id = t.WhoId, Recruiting_Status__c = 'Declined after case study'));}
        
        else if((t.subject.equals('Email: Vielen Dank für Ihre Bewerbung') || t.subject.equals('Email: Vielen Dank für Ihre Kontaktaufnahme'))
        && Con[0].Recruiting_Status__c.contains('IM-interview')) {
        contactsToUpdate.add(new Contact(Id = t.WhoId, Recruiting_Status__c = 'Declined after IM-interview'));}
        
        else if((t.subject.equals('Email: Vielen Dank für Ihre Bewerbung') || t.subject.equals('Email: Vielen Dank für Ihre Kontaktaufnahme'))
        && Con[0].Recruiting_Status__c.contains('partner/MD interview')) {
        Con[0].Recruiting_Status__c = 'Declined after partner interview';
        contactsToUpdate.addAll(Con);}
        
    } 
    }
     update contactsToUpdate;
}
Test Class:
@IsTest
public class TestRecEmailTrigger {

        static testmethod void insertTask() {
        
        User us = new User();
        
        us.FirstName = 'Test';
        us.LastName = 'User';
        us.Alias = 'UserTest';
        us.Email = 'samuel.schoessel@gmx.de';
        us.Username = 'samuel.schoessel@gmx.de';
        us.CommunityNickname = 'Sammy';
        us.TimeZoneSidKey = 'Europe/Berlin';
        us.LocaleSidKey = 'de_DE';
        us.EmailEncodingKey = 'UTF-8';
        us.LanguageLocaleKey = 'en_US';
        us.ProfileId = '00e24000000Mca5';
        
        insert us; 
         
        Account acc = new Account(); 
        
        acc.Name = 'Test Account';
        acc.RecordTypeId = '012240000004qdf';
        acc.OwnerId = us.Id;
        
        insert acc;
        
        Contact con = new Contact();
        
        con.LastName = 'The Sam';
        con.FirstName = 'Sammy';
        con.OwnerId = us.Id;
        con.RecordTypeId = '012240000006HOl'; 
        con.Recruiting_Status__C = 'Scheduled for initial conversation';  
        
        insert con;  
         
        Task t = New Task();
        
        t.Subject = 'vollständige Bewerbungsunterlagen';
        t.Priority = 'Normal';
        t.Status = 'Geschlossen';
        t.OwnerId = us.Id;
        t.WhoId = con.Id;
                
        insert t;
        }
    }

Back to the Issue:
Apex Test Execution tells me that the Test passes the test but I only have 50% code coverage because it wont take all the lines with the contactsToUpdate List. 
As you can see in:
User-added image

Many many thanks in advance!! I really hope someone can help me with that!

Best

Sam
Best Answer chosen by AUCTUS Administrator
anto nirmalanto nirmal
Hi AUCTUS Administrator,

Please ensure all the else conditions are tested in the test class.
You test class covers only for the email subject condition: vollständige Bewerbungsunterlagen only repeat that for all the subjects.
You need to insert tasks for all the email subject contions in your apex class.

Add the following code to your test class and you could see your code coverage increasing.
Task t1 = New Task();
t1.Subject = 'Email: Das AUCTUS Recruiting Erlebnis: selbständige Terminkoordination für ein erstes Vorabgespräch';
t1.Priority = 'Normal';
t1.Status = 'Geschlossen';
t1.OwnerId = us.Id;
t1.WhoId = con.Id;
insert t1;
Please let me know if the helps.


As a common practice, if your question is answered, please choose 1 best answer.
Additionaly you can give every answer a thumb up if that answer is helpful to you.

Regards,
Anto Nirmal
 

All Answers

AUCTUS AdministratorAUCTUS Administrator
Additional comment: The Trigger works perfect in Sandbox!! When I send an Email out with the defined Subjects, the Status field of the related contact is updated by the trigger. 

But since its only getting 50% code coverage I am not sure if I should take it to production yet. Besides that, I am sure the code I wrote is total crap if it comes to efficiancy 
anto nirmalanto nirmal
Hi AUCTUS Administrator,

Please ensure all the else conditions are tested in the test class.
You test class covers only for the email subject condition: vollständige Bewerbungsunterlagen only repeat that for all the subjects.
You need to insert tasks for all the email subject contions in your apex class.

Add the following code to your test class and you could see your code coverage increasing.
Task t1 = New Task();
t1.Subject = 'Email: Das AUCTUS Recruiting Erlebnis: selbständige Terminkoordination für ein erstes Vorabgespräch';
t1.Priority = 'Normal';
t1.Status = 'Geschlossen';
t1.OwnerId = us.Id;
t1.WhoId = con.Id;
insert t1;
Please let me know if the helps.


As a common practice, if your question is answered, please choose 1 best answer.
Additionaly you can give every answer a thumb up if that answer is helpful to you.

Regards,
Anto Nirmal
 
This was selected as the best answer
ProlayProlay
You have to test all the else conditions.You have to test the conditions with System.AssertEquals. Please go through this link  ​http://th3silverlining.com/2010/09/07/salesforce-system-assert-vs-system-assertequals/
AUCTUS AdministratorAUCTUS Administrator
Thanks guys for the very fast responses!! This was a great experience! :) 

From now on I will be more active in this forum. 

PS: Thanks to adding every kind of option to my class got me 100% code coverage so I think it should be fine to take it to production!