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

Cross object Apex trigger test class help
Hello everyone,
I have been pounding my head into my keyboard for the last day trying to figure out how to write a test class for this trigger that will get more than %66. I am very new to writing apex, but I have written a few triggers for single objects and this is my first for a cross object trigger and for some reason the test class is just not making sence to me. So that brings me here. I am wondering if anyone can help me write a test class that will allow deployment into production. Any an all help is appricated :) I wouldn't mind a little explanation at how you came to the conclusion as well. The more you know! :)
The code is pretty simple. It checks for a created task with a specific subject line, then changes the "Status" of the associated lead.
I have been pounding my head into my keyboard for the last day trying to figure out how to write a test class for this trigger that will get more than %66. I am very new to writing apex, but I have written a few triggers for single objects and this is my first for a cross object trigger and for some reason the test class is just not making sence to me. So that brings me here. I am wondering if anyone can help me write a test class that will allow deployment into production. Any an all help is appricated :) I wouldn't mind a little explanation at how you came to the conclusion as well. The more you know! :)
The code is pretty simple. It checks for a created task with a specific subject line, then changes the "Status" of the associated lead.
trigger changeLeadStatus_Disconnected on Task (before insert, before update) { String desiredNewLeadStatus = 'Wrong #/Disconnected'; List<Id> leadIds=new List<Id>(); for(Task t:trigger.new){ if(t.Subject=='Call with PhoneBurner - BAD PHONE'){ if(String.valueOf(t.whoId).startsWith('00Q')==TRUE){ leadIds.add(t.whoId); } } } List<Lead> leadsToUpdate=[SELECT Id, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE]; For (Lead l:leadsToUpdate){ l.Status=desiredNewLeadStatus; } try{ update leadsToUpdate; }catch(DMLException e){ system.debug('Leads were not properly updated. Error: '+e); } }
Please try as per the following code -
You should be able to get 100% code coverage.
Thanks,
Srinivas
- Please mark as solution if your problem is resolved.
All Answers
Please try as per the following code -
You should be able to get 100% code coverage.
Thanks,
Srinivas
- Please mark as solution if your problem is resolved.