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
anil 007anil 007 

how to schedule a batch apex class

hiii,

 

i have an bach apex class i want to write a schedule class for this batch apx class for evereday afternoon  at 1PM

 

Can anyone provide sample code for schedule apex class

 

 

PatcsPatcs

First write a new class which implements the schedulable interface

 

for example:

 

global class classname implements schedulable
{
    global void execute(SchedulableContext sc)
    {
    batchclass b = new batchclass(); //ur batch class
      database.executebatch(b);
    }
}

 

in the apex class page there is a button called "scheduled Apex" click that from there u can schedule the apex class

 

try it..................

Pradeep_NavatarPradeep_Navatar

We use Apex Scheduler to schedule a controller to execute it at a given time in future. For this make an Apex Scheduler Controller and to schedule this controller go to...

 

Administration Setup->Monitoring->Scheduled Jobs from there we select that Controller class and then provide some time and date to execute it in future.

 

            Below is a sample code of the Apex Scheduler Controller to send an email :

 

            global class ApexScheduledClass Implements Schedulable

            {

                        global void execute(SchedulableContext sc)

                        {

                                    sendmail();

                        }

 

                        public void sendmail()

                        {

                                    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

                                    string [] toaddress= New string[]{'vkumar.sri@gmail.com'};

                                    email.setSubject('Testing Apex Scheduler-Subject');

                                    email.setPlainTextBody('Testing Apex Scheduler-Body');

                                    email.setToAddresses(toaddress);

                                    Messaging.sendEmail(New Messaging.SingleEmailMessage[]{email});

                        }

            }

anil 007anil 007

hiii pradeep,

 

Thanks for reply it workes  fine

Shiva Nand 1Shiva Nand 1
Hi Pradeep Sir,
What is the code for debug/execute them and hot to write test class for that.
Titane PhamTitane Pham
global class SendReminderEmail implements Database.Batchable<sObject> {

    global String query;
    global String subject;
    global String body;

    global SendReminderEmail(String query, String subject, String body) {
        this.query = query;
        this.subject = subject;
        this.body = body;
    }
        
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext bc, List<Speaker__c> scope) {
        for (Speaker__c speaker : scope) {
            EmailManager.sendMail(speaker.Email__c, this.subject, this.body);
        }
    }
    
    global void finish(Database.BatchableContext bc) {

    }
    
}
Christina ZhankoChristina Zhanko
Here is good solution. It really helped me!
Schedule manager on AppExchange - https://appexchange.salesforce.com/listingDetail?listingId=a0N3A00000DqCmYUAV. Can execute your schedule periodically, like you want.
Hope, it will be useful for you!
Suraj Tripathi 47Suraj Tripathi 47
Hii Anil,
You can try the below code
global class testSchedular implements Schedulable {
    global void execute(SchedulableContext ctx) {
        SampleBatchClass sampleBatchObj=new SampleBatchClass();
        database.executebatch(sampleBatchObj);
    }
}
And for everyday 1 pm execution, you run it as
system.schedule('insert TrailHeadData1', '0 0 13/0 16/1  ? ',new testSchedular());
In case you find any other issue please mention. 
If you find your Solution than mark as this as a best answer. 

Thanks and Regards
Suraj Tripathi.