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.
- Don’t forget to check out:- How to write batch class to update a record’s field in Salesforce Click Here For More Information
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
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]
![]() |
global class schedule_scoreCardUpdate Implements Schedulable {
public static void EXECUTE(SchedulableContext Sc){
ID batchInstanceID = DATABASE.executeBatch(NEW batch_scoreCardEmailUpdate(),10);
}
}
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]
![]() |
global class batch_scoreCardEmailUpdate Implements DATABASE.Batchable<sobject> {
global DATABASE.QueryLocator START(DATABASE.BatchableContext BC){
string query = 'Select Id, Name, Email__c, City__c, Phone_No__c From scoreCard__c Where Name !=null';
RETURN DATABASE.getQueryLocator(query);
}
global void EXECUTE(DATABASE.BatchableContext BC, List<scoreCard__c> scope){
List<scoreCard__c> scoreUpate = NEW List<scoreCard__c>();
FOR(scoreCard__c scoreObj: scope){
scoreObj.Email__c = 'w3webmart@gmail.com';
scoreObj.City__c = 'Delhi';
scoreUpate.add(scoreObj);
}
system.debug('scoreUpate@@ ' + scoreUpate);
UPDATE scoreUpate;
}
global void finish(DATABASE.BatchableContext BC){
AsyncApexJob a = [SELECT Id, STATUS, NumberOfErrors, JobItemsProcessed,
TotalJobItems, CreatedBy.Email
FROM AsyncApexJob WHERE Id =
:BC.getJobId()];
Messaging.SingleEmailMessage mail = NEW Messaging.SingleEmailMessage();
String[] toAddresses = NEW String[] {a.CreatedBy.Email};
mail.setToAddresses(toAddresses);
mail.setSubject('Apex Sharing Recalculation schedule email update' + a.Status);
mail.setPlainTextBody
('The batch Apex job processed scoreCard update ' + a.TotalJobItems +
' batches with '+ a.NumberOfErrors + ' failures.');
Messaging.sendEmail(NEW Messaging.SingleEmailMessage[] { mail });
}
}
Further post that would you like to learn in Salesforce
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
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 |