• Timothy Wiech
  • NEWBIE
  • 10 Points
  • Member since 2015


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
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.
 
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);
    }
}

 
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.
 
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);
    }
}