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
Marcus StollMarcus Stoll 

Trigger to update lead status based off event created by specific user profile

Hi all - I'm trying to figure out the appropriate way to have a trigger to update a lead's status to "SDR Meeting Booked" if an event activity is created by someone whose user profile ID = 00eE0000000adW0.

Modifying some code I found elsewhere I have the first part down (changing status based off an activity being created), but I'm not sure how to modify this to account for the second piece.

Total developer noob here, so any help is appreciated!
 
trigger changeLeadStatus on Event (before insert, before update) {
    String desiredNewLeadStatus = 'SDR Meeting Booked';

    List<Id> leadIds=new List<Id>();
    for(Event t:trigger.new){
            if(String.valueOf(t.whoId).startsWith('00Q')==TRUE){//check if the task is associated with a lead
                leadIds.add(t.whoId);
            }
    }//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

 
Deepak BalurDeepak Balur
Can you be a bit more specific on '''....but I'm not sure how to modify this to account for the second piece."? Accont field, entitiy??
MithunPMithunP
Hi Marcus Stoll,

You have to use after context (after insert and after update) and update your trigger like below.

For now, i'm querying profile id of "System Administrator", you can replace it with your required profile.

Let me know if you face any issues.
 
trigger changeLeadStatus on Event (after insert, after update) {
    String desiredNewLeadStatus = 'SDR Meeting Booked';
    Profile PID = [Select Id From Profile where name = 'System Administrator' limit 1];
    List<Event>  eventList = [Select id,whoId,Createdby.ProfileId,CreatedbyId From Event Where id in: trigger.newmap.keyset()];
    List<Id> leadIds=new List<Id>();
    for(Event t: eventList){
            if(String.valueOf(t.whoId).startsWith('00Q') == TRUE && t.Createdby.ProfileId == PID.Id){//check if the task is associated with a lead
                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 all properly updated.  Error: '+e);
    }
}//trigger
Best Regards,
Mithun.
 
Marcus StollMarcus Stoll
Hi Mithun, I'm getting the following error when I try to use the code on an Apex Trigger:

"Error: Compile Error: Incorrect SObject type: Event should be Lead at line 1 column 1"
MithunPMithunP
Hi Marcus,

I think you created trigger on Lead, you shoud create trigger on Event object not on Lead.

Customize > Activities > Event Triggers > NEW

Best Regards,
Mithun.