You need to sign in to do that
Don't have an account?
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!
Try below test class. You owe me a beer if your production deployment goes through ;-)
All Answers
Try below test class. You owe me a beer if your production deployment goes through ;-)
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