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
Raghu Sharma 6Raghu Sharma 6 

setting flag for batch run

I need to verify whether a batch (actually inbound email handler) has been run each day and if the batch is not run for a particular day, I need to send an email notification.

Is there any config way, I can set some flag before the anticipated time of batch run and reset the flag in batch.
After batch run anticipated time, I want to verify flag and if flag is not reset (which means batch did not run), I need to send an email
Is it possible to set the flag using workflow? Is it possible to verify flag and send email using workflow?
So, basically I'm trying to see if workflow can be run like a scheduled batch job for this simple task?
 
Damon ShawDamon Shaw
Hi Raghu,

Using workflow could possibly be used but it sounds really messy and unreliable.

How is the batch called? if it's scheduled I would use Database.Stateful to keep track if there are any records sent to the execute method and then send an email from the finish method based on that,
 
global class InboundEmailHandler implements Schedulable, Database.Batchable<sObject>, Database.Stateful {
    
    Boolean hasRecords = false;
    
    global InboundEmailHandler() {
        
    }
    
    global Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator('SELECT Id FROM Inbound_Email__c WHERE CreatedDate = today()');
    }

    global void execute(Database.BatchableContext BC, List<sObject> scope) {

        if(scope.size() > 0){
            hasRecords = true;
        }

        //do your batch stuff
    }
    
    global void finish(Database.BatchableContext BC) {
        
        String subject = '';
        String plainText = '';

        if(hasRecords){
            subject = 'InboundEmailHandler ran';
            plainText = 'there were some records in the batch';
        }
        else{
            subject = 'InboundEmailHandler didn\'t do anything';
            plainText = 'there were no records in the batch';
        }

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setPlainTextBody(plainText);
        mail.setToAddresses(new List<String>{UserInfo.getUserEmail()});
        mail.setSubject(subject);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
    }    
}

 
Raghu Sharma 6Raghu Sharma 6
Actually, its an inbound email handler which will be executed when ever it recieves the email. So, it is basically on demand and not really an batch
Damon ShawDamon Shaw
Hi Raghu,

When the inbound email handler is executed, does it create or update any records? if it updates a record can you add a date/time field to set when this class did it's update?

I would then look to create a scheduled class that looks for those records whos CreatedDate or update date/time falls within the required time frame, then send an email with the results, either it found some records or it didn't.

There are plenty of posts about creating a schedulable class, here's one to get you started,
https://salesforce.stackexchange.com/questions/80277/sample-code-to-schedule-an-apex-class

Once you've created the class to look up those records and send the email just go to Setup > Build > Develop > Apex Classes and click the 'Schedule Apex' button to schedule when your class should run