Writing a Custom Schedulable Batch Apex Class and Schedule job Weekly at Specific Date and Time in Salesforce | how to schedule batch class in salesforce

1,242 views

Hey guys, today in this post we are going to learn about how to Write a Custom Schedulable Batch Apex Class and schedule job Weekly at Specific Date and Time in Salesforce

Real time scenarios:- Custom Schedulable Batch Apex Class and Schedule job Weekly at Specific Date and Time in Salesforce.

Files we used in this post example

schedule_scoreCardUpdate.apxc Apex Class Controller It is used for Schedule Batch Apex Class and Schedule job Weekly at Specific Date and Time

Live Demo

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!

 

Schedulable Batch Apex in Salesforce -- w3web.net

 

You can download file directly from github by Click Here.

 

 

Other related post that would you like to learn in Salesforce

 

Create Apex Class Trigger Controller

Step 1:- Create Apex Class : schedule_scoreCardUpdate.apxc

From Developer Console >> File >> New >> Apex Class

schedule_scoreCardUpdate.apxc [Apex Class Controller]

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  1.    global class schedule_scoreCardUpdate Implements Schedulable {
  2.     public static void EXECUTE(SchedulableContext Sc){
  3.         ID batchInstanceID = DATABASE.executeBatch(NEW batch_scoreCardEmailUpdate(),10);
  4.  
  5.     }
  6.   }

Create Apex Class Controller

Step 2:- Create Apex Class : batch_scoreCardEmailUpdate.apxc

From Developer Console >> File >> New >> Apex Class

batch_scoreCardEmailUpdate.apxc [Apex Class Controller]

  1.    global class batch_scoreCardEmailUpdate Implements DATABASE.Batchable<sobject> {
  2.     global DATABASE.QueryLocator START(DATABASE.BatchableContext BC){
  3.         string query = 'Select Id, Name, Email__c, City__c, Phone_No__c From scoreCard__c Where Name !=null';
  4.         RETURN DATABASE.getQueryLocator(query);        
  5.     }
  6.     global void EXECUTE(DATABASE.BatchableContext BC, List<scoreCard__c> scope){
  7.          List<scoreCard__c> scoreUpate = NEW List<scoreCard__c>(); 
  8.         FOR(scoreCard__c scoreObj: scope){
  9.             scoreObj.Email__c = 'w3webmart@gmail.com';
  10.             scoreObj.City__c = 'Delhi';
  11.             scoreUpate.add(scoreObj);
  12.         } 
  13.         system.debug('scoreUpate@@ ' + scoreUpate);
  14.         UPDATE scoreUpate;        
  15.     }
  16.  
  17.     global void finish(DATABASE.BatchableContext BC){
  18.        AsyncApexJob a = [SELECT Id, STATUS, NumberOfErrors, JobItemsProcessed,
  19.           TotalJobItems, CreatedBy.Email
  20.           FROM AsyncApexJob WHERE Id =
  21.           :BC.getJobId()];
  22.        Messaging.SingleEmailMessage mail = NEW Messaging.SingleEmailMessage();
  23.        String[] toAddresses = NEW String[] {a.CreatedBy.Email};
  24.        mail.setToAddresses(toAddresses);
  25.        mail.setSubject('Apex Sharing Recalculation schedule email update' + a.Status);
  26.        mail.setPlainTextBody
  27.        ('The batch Apex job processed scoreCard update ' + a.TotalJobItems +
  28.        ' batches with '+ a.NumberOfErrors + ' failures.');
  29.        Messaging.sendEmail(NEW Messaging.SingleEmailMessage[] { mail });    
  30.     }
  31.  
  32. }

Schedulable Batch Apex in Salesforce -- w3web.net

Further post that would you like to learn in Salesforce

 

 

 

FAQ (Frequently Asked Questions)

How do I make Apex batch Schedulable?

The Apex Scheduler lets you delay execution so that you can run Apex classes at a specified time. To take advantage of the scheduler, write an Apex class that implements the Schedulable interface, and then schedule it for execution on a specific schedule.

Can we call a Schedulable class from batch class?

ScheduleBatch method to schedule a batch job to run once at a specified time in the future. This method is available only for batch classes and doesn't require the implementation of the Schedulable interface. This makes it easy to schedule a batch job for one execution.

Can we call schedule Apex from trigger?

Yes it is possible, we can call a batch apex from trigger but we should always keep in mind that we should not call batch apex from trigger each time as this will exceeds the governor limit this is because of the reason that we can only have 5 apex jobs queued or executing at a time.

Related Topics | You May Also Like

 
Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  

Our Free Courses →

👉 Get Free Course →

📌 Salesforce Administrators

📌 Salesforce Lightning Flow Builder

📌 Salesforce Record Trigger Flow Builder

👉 Get Free Course →

📌 Aura Lightning Framework

📌 Lightning Web Component (LWC)

📌 Rest APIs Integration



Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment