+ Start a Discussion
RahulRahul 

Hi friends, can some one help me with this test class

I had 100% code coverage yesterday but today its 69% only. I dont know how this happened.
This is the code:-
trigger updatestatus on Task (after insert) {
set<id> setid = new set<id>();
list<contact> lstcon = new list<contact>();
for(task t :trigger.new){
setid.add(t.whatid);
setid.add(t.whoid);
}
for(contact cc :[select id,boolean__C,(select subject from tasks ) from contact where id=:setid]){
For(task t1 :trigger.new){
if(t1.subject=='Mass Email: Fourth Followup for Accounts New'){
Contact cc1 = new contact();
cc1.id=cc.id;
cc1.boolean__c='True';
lstcon.add(cc1);

}
update lstcon;
}

}

}

This is the Test class :-
@isTest 
private class SampleTestClass {
    static testMethod void validateContactsAndTasks() {
       Contact con = new Contact(LastName='Sample Last NAme', boolean__c=True);
       insert con;
        
        Task tsk = New Task();
        tsk.WhoId = con.Id;
        tsk.Subject = 'Mass Email';
        tsk.Status = 'In Progress';
        tsk.Priority = 'Low';
        insert tsk;
    }
}

Please help
Best Answer chosen by Rahul
Ryan GreeneRyan Greene
Have checked which lines are getting covered when the code runs? In the Developer Console, run the test, then when on the Trigger click the "Code Coverage:" button in the top left. This will show which lines are being covered. From your test code, it looks like this line tsk.Subject = 'Mass Email'; would not trigger the trigger as the subject must equal: if(t1.subject=='Mass Email: Fourth Followup for Accounts New'){

I would try updating your test tsk.Subject to equal "Mass Email: Fourth Followup for Accounts New"

All Answers

Ryan GreeneRyan Greene
Have checked which lines are getting covered when the code runs? In the Developer Console, run the test, then when on the Trigger click the "Code Coverage:" button in the top left. This will show which lines are being covered. From your test code, it looks like this line tsk.Subject = 'Mass Email'; would not trigger the trigger as the subject must equal: if(t1.subject=='Mass Email: Fourth Followup for Accounts New'){

I would try updating your test tsk.Subject to equal "Mass Email: Fourth Followup for Accounts New"
This was selected as the best answer
RahulRahul
Thank you so much for help Ryan.