• Vikram Varma 2
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

Hi all, i'm a relatively new SF admin and wanted "Lead Status" to change to "Contacted" once some sort of activity is logged on the lead record. I tried using process builder, but that didnt work for obvious reasons, and was suggested to try an Apex Trigger. I have 0 coding experience and found the following trigger (only changed the names of the fields to match my org): 

trigger LeadStatus on Task (before insert, before update) {
    String desiredNewLeadStatus = 'Contacted';

    List<Id> leadIds=new List<Id>();
    for(Task t:trigger.new){
        if(t.Status=='Completed'){
            if(t.whoId != null && String.valueOf(t.whoId).startsWith('00Q')==TRUE){//check if the task is associated with a lead
                leadIds.add(t.whoId);
            }//if 2
        }//if 1
    }//for
    List<Lead> leadsToUpdate=[SELECT Id, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE];
    For (Lead l:leadsToUpdate){
        l.Status=desiredNewLeadStatus;
    }//for
    
    try{
        update leadsToUpdate;
    }catch(DMLException e){
        system.debug('Leads were not all properly updated.  Error: '+e);
    }
}//trigger

This worked in sandbox, but when trying to deploying to production, gave me the error of 0% code is covered. I learned that it requires an Apex Class (test class?). Since I have no experience writing code, i did some searching and found a similar test class and changed the field names, etc, and didn't work. I've just about given up. 

Does anyone see any errors with the trigger above, and can provide me a test class? It'll help me learn and solve an immediate problem. 

 

Thank you!