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
Jeff SusichJeff Susich 

Create an Event when a Lead checkbox is checked

I want the standard Event creation page to be displayed when a Lead checkbox field is checked, and the record Saved. I need a couple of the new Event fields to be prepopulated from the Lead Record. 

I tried to do this in Flow, but Flow does not support Date/Time fields, which is needed for the Event Start Date/Time and End Date/Time fields. 

My Apex coder is telling me that the standard Event creation page cannot be displayed when the Lead checkbox is checked, using an Apex Trigger. 

This does not sound right to me. Any input appreciated. 
v varaprasadv varaprasad
Hi Jeff,

Please check following  sample code : 
trigger PopulateEventFieldOnLead on Lead (after insert) {
    set<id> leadIds = new set<id>();
    list<event> lstEvents = new list<event>();
    for(Lead l : trigger.new){
        if(l.CheckLead__c == True){   
            Datetime myDateTime = Datetime.now();
            Datetime endDate = myDateTime.addDays(2);
            Event te = new Event();
            te.StartDateTime = Datetime.now();
            te.EndDateTime = endDate;
            te.Subject = 'Email';         
            te.WhoId = l.id;
            lstEvents.add(te);
            leadIds.add(l.id);
        }
        
    } 
    insert lstEvents;
    
    list<lead> lstLeds = [select id,name,company,title,(select id,Subject from events limit 1) from lead];
    
    for(lead ls : lstLeds){
        string leadSubject = '';
        for(event e : ls.events){
            leadSubject = e.Subject;
        }
        ls.title = leadSubject;
    }
   
    update lstLeds;
    
}

Hope this helps you.


Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com