What is the sample code to schedule class in salesforce

287    Asked by AndreaBailey in Salesforce , Asked on Sep 22, 2022

 I'll be working on creating a apex schedule class and I just need a pointer to start is that possible if you can have a working small sample where I can understand how the mechanics work and I have googled but not find what exactly I want and also I looked at the salesforce site https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm

Answered by Ankesh Kumar

To schedule class in salesforce, You can take help from the below example :


Batch class :

global class BatchCreateOpportunity implements Database.batchable{
    global Database.QueryLocator start(Database.BatchableContext bc){
        String query = 'SELECT id from Account limit 1';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List scope)
    {
        List oppList = new List();
         for(Account acc : scope )
     {
         for(integer i=0;i
    insert oppList;
}
 global void finish(Database.BatchableContext BC)
    {
    }
}
Scheduler class :
global class schduleBatchCreateOpportunity implements Schedulable {
   global void execute(SchedulableContext ctx) {
      BatchCreateOpportunity p = new BatchCreateOpportunity();
        database.executeBatch(p);
   }
}


Your Answer