+ Start a Discussion
Sainath VenkatSainath Venkat 

Batch apex to run at 12:10 AM everyday

Can anyone helpme out in scheduling a batch apex which need torun at 12:10 AM Everyday.

My batch apex code:
global class BatchImpliedConsentLead implements Database.Batchable<sObject> {
    global integer recordsProcessed = 0;
    global Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator('SELECT Id, Implied_Consent__c,Implied_Consent_Date__c,HasOptedOutOfEmail,CASL_Opt_In__c,CASL_Opt_In_Status__c, Implied_Consent_Expiry__c from Lead WHERE Implied_Consent__c != null AND Implied_Consent_Expiry__c != null');
    }
    global void execute(Database.BatchableContext bc, List<Lead> scope){
        for(lead l: scope){
            //date d = l.Implied_Consent_Expiry__c.date();
            if((l.Implied_Consent__c == true) && (l.Implied_Consent_Expiry__c.date() <= system.today()) ){
                l.Implied_Consent_Date__c = null;
                l.Implied_Consent__c = false;
                l.HasOptedOutOfEmail = true;
                l.CASL_Opt_In__c = false;
                l.CASL_Opt_In_Status__c = 'Removed';
                
            }
            update scope;
        }
    }
        global void finish(Database.BatchableContext bc){
            system.debug(recordsProcessed +'records Processed');
            AsyncApexJob job = [select Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems,CreatedBy.Email from AsyncApexJob where Id =: bc.getJobId()];
    }

}

 
Best Answer chosen by Sainath Venkat
TechingCrewMattTechingCrewMatt
global class BatchImpliedConsentLead implements Database.Batchable<sObject>, Schedulable {
    global integer recordsProcessed = 0;
    
    public static void scheduleImpliedConsentLeadBatch(){
        System.schedule('Implied Consent Lead Job', '0 10 0 * * ?', new BatchImpliedConsentLead());
    }
    
    global void execute(SchedulableContext context){
        Database.executeBatch(new BatchImpliedConsentLead());
    }
    
    global Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator('SELECT Id, Implied_Consent__c,Implied_Consent_Date__c,HasOptedOutOfEmail,CASL_Opt_In__c,CASL_Opt_In_Status__c, Implied_Consent_Expiry__c from Lead WHERE Implied_Consent__c != null AND Implied_Consent_Expiry__c != null');
    }
    global void execute(Database.BatchableContext bc, List<Lead> scope){
        for(lead l: scope){
            date d = l.Implied_Consent_Expiry__c.date();
            if((l.Implied_Consent__c == true) && (l.Implied_Consent_Expiry__c.date() <= system.today()) ){
                l.Implied_Consent_Date__c = null;
                l.Implied_Consent__c = false;
                l.HasOptedOutOfEmail = true;
                l.CASL_Opt_In__c = false;
                l.CASL_Opt_In_Status__c = 'Removed';
                
            }
        }
        update scope;
    }
    
    global void finish(Database.BatchableContext bc){
            system.debug(recordsProcessed +'records Processed');
            AsyncApexJob job = [select Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems,CreatedBy.Email         from AsyncApexJob where Id =: bc.getJobId()];
    }

}
To schedule Apex for this use case you'll need to implement the Schedulable interface as shown above. The method call below will schedule the class which will execute the batch. I also moved your DML statement outside of the loop for efficiency.
 
BatchImpliedConsentLead.scheduleImpliedConsentLeadBatch();


 

All Answers

TechingCrewMattTechingCrewMatt
global class BatchImpliedConsentLead implements Database.Batchable<sObject>, Schedulable {
    global integer recordsProcessed = 0;
    
    public static void scheduleImpliedConsentLeadBatch(){
        System.schedule('Implied Consent Lead Job', '0 10 0 * * ?', new BatchImpliedConsentLead());
    }
    
    global void execute(SchedulableContext context){
        Database.executeBatch(new BatchImpliedConsentLead());
    }
    
    global Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator('SELECT Id, Implied_Consent__c,Implied_Consent_Date__c,HasOptedOutOfEmail,CASL_Opt_In__c,CASL_Opt_In_Status__c, Implied_Consent_Expiry__c from Lead WHERE Implied_Consent__c != null AND Implied_Consent_Expiry__c != null');
    }
    global void execute(Database.BatchableContext bc, List<Lead> scope){
        for(lead l: scope){
            date d = l.Implied_Consent_Expiry__c.date();
            if((l.Implied_Consent__c == true) && (l.Implied_Consent_Expiry__c.date() <= system.today()) ){
                l.Implied_Consent_Date__c = null;
                l.Implied_Consent__c = false;
                l.HasOptedOutOfEmail = true;
                l.CASL_Opt_In__c = false;
                l.CASL_Opt_In_Status__c = 'Removed';
                
            }
        }
        update scope;
    }
    
    global void finish(Database.BatchableContext bc){
            system.debug(recordsProcessed +'records Processed');
            AsyncApexJob job = [select Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems,CreatedBy.Email         from AsyncApexJob where Id =: bc.getJobId()];
    }

}
To schedule Apex for this use case you'll need to implement the Schedulable interface as shown above. The method call below will schedule the class which will execute the batch. I also moved your DML statement outside of the loop for efficiency.
 
BatchImpliedConsentLead.scheduleImpliedConsentLeadBatch();


 
This was selected as the best answer
sachinarorasfsachinarorasf
Hi Sainath,

I have gone through your problem.

Schedule class:
 
global class scheduleEvrery12AM implements Schedulable{
    global void execute (SchedulableContext SC){
        BatchImpliedConsentLead batchImpliedObj = new  BatchImpliedConsentLead();
        database.executeBatch(batchImpliedObj);
    }
    public static void scheduleJob()
    {
        string sch = '0 10 12 1/1 * ? *';
        system.schedule ('Batch', sch, new scheduleEvrery12AM());
    }
    
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora