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
Marvin Castro 8Marvin Castro 8 

Trigger to update lead status when activity is logged

Hello.  I'm new to salesforce.  How do I create a trigger that will update the lead status to working when an activity is created (event, task, call)?  I saw this code while doing research, and it works for logging a call, but does not update the status if I schedule an event.  Any help will be appreciated.

trigger changeLeadStatus on Task (before insert, before update) {
    String desiredNewLeadStatus = 'Working';

    List<Id> leadIds=new List<Id>();
    for(Task t:trigger.new){
        if(t.Status=='Completed'){
            if(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
 
Best Answer chosen by Marvin Castro 8
UC InnovationUC Innovation
Hello Marvin,

You need to create the same trigger on the Event Object. Events and Tasks are both activities, but they are separate objects (They are special in that way). You already have the code, just copy it for Events and you should be good!

Hope this helps!

All Answers

UC InnovationUC Innovation
Hello Marvin,

You need to create the same trigger on the Event Object. Events and Tasks are both activities, but they are separate objects (They are special in that way). You already have the code, just copy it for Events and you should be good!

Hope this helps!
This was selected as the best answer
UC InnovationUC Innovation
Hi Marvin,

Just following up. Did you find the solution? If so, please mark this as solved and choose the best answer so others can find the answer quickly!
umesh kantumesh kant
I was checking how to create the trigger .
I have got the code and now i can do it.
Thanks a lot
umesh
I have a website development company  (https://www.wismad.com)