Trigger not updating field in Test Class
The error is in your Status
. In the trigger you've got:
if (l.Status =='Open- Not Contacted')
(note the n- N
), and in your test class you create it as:
ld.Status='Open -Not Contacted';
(note the n -N
). That misplaces whitespace is probably the reason why that line is not getting covered
Edit: Try to use constants for this kind of things. It will make your life easier.
Among the other answers here you will still need to move this line to be after you insert your task.
Lead Getleadupdated = [SELECT Status FROM Lead where ID = : createlead.id];
Otherwise the value of your Status field will be what it was before the trigger was run.
Also please note that your Trigger will run for ANY Task that is created, whether it is an email or not. If your Sales Team only uses Tasks for emails then you have no problem, but if they use Tasks for other things this code will run every time.
Note that this is not the correct answer. Leaving it here to notify others that you don't need to explicitly query the Id field, it will always be in the result set
you are inserting the Task without a Lead Id:
Lead Getlead = [SELECT Status FROM Lead where ID = : createlead.id];
Task tk = createTask(Getlead.id);
GetLead does not have an Id, because that was not queried in the SOQL query.
But you don't even need to requery the Lead. As soon as you insert the Lead, the createLead
will contain the Id.
So do this:
Task tk = createTask(createLead.id);
And the last part of your code will be covered as well.