You need to sign in to do that
Don't have an account?

How to write batch apex if there is no count 0, send email
Hi,
I need to make a process,when there is 0 count record send email.
I need to make a process,when there is 0 count record send email.
Please go through below link
https://webkul.com/blog/batch-apex/
Thanks,
Foram Rana
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) {
if(scope.size()==0){
//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) {
}
}