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
Amit Gaur 8Amit Gaur 8 

Mail Not Sent

I wrote the following code in which I will get mail with all Event in Tabular form, but after Execution I am not getting any Mail.

Kindly Help

My Apex Batch Class

global class EventBatchClass implements Database.Batchable<sObject>
{
    global Database.QueryLocator start (Database.BatchableContext bc)
    {
        String query='SELECT Owner.Name, Owner.Email, Subject, StartDateTime, EndDateTime, What.Name FROM Event';
        return Database.getQueryLocator(query);  
    }
    global void execute(database.BatchableContext bc, list<event> Scope)
    {   
        
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        messaging.SingleEmailMessage Email=new messaging.SingleEmailMessage();
        list<string> EmailId=new list<string>();
        String str = '' ;
        String finalStr= '' ;
        
        Map<Id,List<Event> > EventMap = new Map<Id,List<Event> > ();
        List<Event> EventList=new List<Event>();
        List<Event> EventList1=new List<Event>();
        
        for(Event e:EventList)
        {
            if(!EventMap.containsKey(e.OwnerId))
            {   
                EventMap.put(e.OwnerId, new List<Event>());
            }
            EventMap.get(e.OwnerId).add(e);
        }
        for(Id i:EventMap.KeySet())
        {
            EventList1=EventMap.get(i);
            EmailId.add(EventList1[0].owner.email);
            
            
        
         
            for(event e:EventList1)
            {  
                         
               str += '<tr><td>'+ e.Subject +'</td>'+'<td>'+ e.StartDateTime +'</td>'+'<td>'+ e.EndDateTime +'</td>'+'<td>'+'<a href="https://ap2.salesforce.com/'+ e.ID +'">Click Here</a>'+'</td></tr>' ;  
                 
               str = str.replace('null' , '') ;  
               finalStr = '' ; 
               finalStr = '<table border="1"> <td> Subject </td> <td> Start Date </td> <td>End Date </td> <td>Event Record </td> '+ str +'</table>' ;  
               
    
           }
           
           email.setSubject('Event Alert');
           email.setToAddresses(EmailId);
           email.setHTMLBody('Hi , Details of today\'s events are listed below:' +finalStr);
                
           mails.add(email);
           Messaging.sendEmail(mails);
         }  
         
    }
    global void finish(Database.BatchableContext BC)
    {
    }
}
Shaijan ThomasShaijan Thomas
please try the following
1. For testing purpose hardcode your email id.
2. Move the email part to finish method. You need to use Database.Stateful method
3. Try to send a simple message from Finish method

Thanks
Yury BondarauYury Bondarau
Could you please check if any exception were thrown during barch execution. You can check it in Jobs page of setup interface
User-added image