function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Bill Powell - CSS IncBill Powell - CSS Inc 

Lead Status Update On Completed Activity (email, logged call, etc)

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! 

Best Answer chosen by Bill Powell - CSS Inc
Ravi Dutt SharmaRavi Dutt Sharma
Hi Bill,

Try below test class. You owe me a beer if your production deployment goes through ;-)
 
@isTest
private class LeadStatusTest {
	
    static testMethod void testTaskCompletionEvent(){
        // Create a lead record
        Lead ld = new Lead(LastName='TestLead',Company='TestCompany',Status='Open - Not Contacted');
        insert ld;
        
        // Create a task record
        System.Test.startTest();
        Task tsk = new Task(OwnerId=UserInfo.getUserId(),Subject='Call',Priority='High',Status='Completed',WhoId=ld.Id);
        insert tsk;
        System.assertEquals('Contacted', [SELECT Status FROM Lead WHERE Id=:ld.Id LIMIT 1].Status);
        System.Test.stopTest();
    }
}

 

All Answers

Ravi Dutt SharmaRavi Dutt Sharma
Hi Bill,

Try below test class. You owe me a beer if your production deployment goes through ;-)
 
@isTest
private class LeadStatusTest {
	
    static testMethod void testTaskCompletionEvent(){
        // Create a lead record
        Lead ld = new Lead(LastName='TestLead',Company='TestCompany',Status='Open - Not Contacted');
        insert ld;
        
        // Create a task record
        System.Test.startTest();
        Task tsk = new Task(OwnerId=UserInfo.getUserId(),Subject='Call',Priority='High',Status='Completed',WhoId=ld.Id);
        insert tsk;
        System.assertEquals('Contacted', [SELECT Status FROM Lead WHERE Id=:ld.Id LIMIT 1].Status);
        System.Test.stopTest();
    }
}

 
This was selected as the best answer
Bill Powell - CSS IncBill Powell - CSS Inc
Thanks Ravi, i'll run it today. And i'd absolutely buy you a beer next week if you were going to the Salesforce World Tour in NYC! I'll be there, it'll be my first official Salesforce held event. 
Bill Powell - CSS IncBill Powell - CSS Inc
Ravi, it worked, you are the man. THANK YOU! 
Vikram Varma 2Vikram Varma 2
Hey Ravi,  Bill,
New to Salesforce developing as well, but Bill's code seemed to be exactly what I needed. I am having trouble deploying it though.  When I deployed Ravi's code, I had a few errors.  a) I had to change the initial status to Open from "Open - not contacted" as that was the default in our Salesforce.  Then I got another error "System.AssertException: Assertion Failed: Expected: Contacted, Actual: Open"  
So I changed the assert.Equals to "Open".  The test then passed and was deployed.  But now when I try to deploy the trigger, it says that the Apex test failure in the LeadStatusTest is  "System.AssertException: Assertion Failed: Expected: Open, Actual: Contacted "
Can either of you help please?
Vikram