+ Start a Discussion
Ashu sharma 38Ashu sharma 38 

How to write batch apex if there is no count 0, send email


I need to make a process,when there is 0 count record send email.

Foram Rana RForam Rana R
Hi Ashu,

Please go through below link

Foram Rana
Christan G 4Christan G 4
Hi Ashu, can you elaborate more on this requirement. 0 cound record of what?
Ashu sharma 38Ashu sharma 38
Hi Christan G 4,

I just want to send mail on particulat time frame for If there is no lead captured from my custom field originating field.So I query in start method and in excecute method I just trying to send email,but its working Kindly look into it.

global class fiveToSalesforceLeadBatch implements  Database.Batchable<sObject>  {
    global Database.QueryLocator start(Database.BatchableContext BC) {
      DateTime currentTime = System.now();
        String query = 'select id,name ,Originating_System__c,createdDate from lead where Originating_System__c=\'five9\' and createdDate <:currentTime AND createdDate >:currentTime';
        system.debug('Query' +query);
        return Database.getQueryLocator(query);
    global void execute(Database.BatchableContext BC, List<Lead> scope) {
            //Sending Mail
            Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
            message.toAddresses = new String[] { 'test@tes.com', test@tes.com' };
                message.optOutPolicy = 'FILTER';
            message.subject = 'Alert: No Five9 Lead in last 30 mins';
            message.plainTextBody = 'Hello Test email';
            Messaging.SingleEmailMessage[] messages =   new List<Messaging.SingleEmailMessage> {message};
                Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
            if (results[0].success) {
                System.debug('The email was sent successfully.');
            } else {
                System.debug('The email failed to send: ' + results[0].errors[0].message);
    global void finish(Database.BatchableContext BC) {