Hey guys, today in this post we are going to learn about How to Write a Batch Apex to Update all the Industry and Type Field of Account and Using Database.executeBatch in finish method to recall of batch class and Schedule this batch for every Monday at 12 PM
Real time scenarios:- Write a batch class on Account Object to update all the Account’s Fields Industry and Type through Anonymous Window in Salesforce. Whenever Type is not equal to null
Files we used in this post example
batch_class1.apxc | Apex Class Controller | It is used for Update all the Industry and Type Field of Account through Batch Class |
batch_schedule.apxc | Apex Class Controller | It is used for schedule this batch for 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 Controller
Step 1:- Create Apex Class : batch_class1.apxc
From Developer Console >> File >> New >> Apex Class
batch_class1.apxc [Apex Class Controller]
global class batch_class1 implements DATABASE.Batchable<sObject>{
global batch_class1(){
List<Account> accList = [SELECT Id, Name, TYPE FROM Account WHERE TYPE != NULL];
List<String> strList = NEW List<String>();
FOR(Account a1:accList){
strList.add(a1.Type);
}
}
global DATABASE.QueryLocator START(DATABASE.BatchableContext BC){
string query = 'Select id,Name,Type,Description,Industry From Account';
RETURN DATABASE.getQueryLocator(query);
}
global void EXECUTE(DATABASE.BatchableContext BC, List<Account> scope){
FOR(Account s:scope){
List<Account> acclist1 = NEW List<Account>();
IF(s.Type != NULL){
s.Name=s.Type;
s.Industry = 'Compute Data Analytics';
acclist1.add(s);
}
UPDATE acclist1;
}
}
global void finish(DATABASE.BatchableContext BC){
batch_class1 b = NEW batch_class1();
DATABASE.executeBatch(b);
}
}
Create a instance of batch class and execute Anonymous Window in Salesforce
Execute Apex Class in Anonymous Window
Step 2:- Open Apex Class >> batch_class1.apxc >> Debug >> execute batch class in Anonymous Window in developer console
batch_class1 b1 = NEW batch_class1();
DATABASE.executeBatch(b1,10);
Write a schedule for this batch class in Salesforce
Step 3:- Open Apex Class >> batch_schedule.apxc
batch_schedule.apxc [Apex Class]
global class batch_schedule implements Schedulable{
public static void EXECUTE(SchedulableContext sc){
ID batchInstanceID = DATABASE.executeBatch(NEW batch_class1(),10);
}
}
Further post that would you like to learn in Salesforce
What is batch Apex class in Salesforce?
Batch class in salesforce is used to run large jobs that would exceed normal processing limits. Using Batch Apex, you can process records asynchronously in batches to stay within platform limits.
Can we call batch class from Apex?
The code to run the batch Apex job is placed within another Apex class that implements the 'schedulable' interface. This class can be scheduled to run using the Apex Scheduler in Setup. However, there are other ways to invoke a batch Apex class.
How does batch Apex work?
Each time you invoke a batch class, the job is placed on the Apex job queue and is executed as a discrete transaction. This functionality has two awesome advantages: Every transaction starts with a new set of governor limits, making it easier to ensure that your code stays within the governor execution limits.
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 |