Write a Batch Apex to Update all the Industry and Type Field of Account and re-call batch class from batch Using Database.executeBatch in finish method | create a batch to update account field in Salesforce

3,884 views


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

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

batch class to update account field 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 Controller

Step 1:- Create Apex Class : batch_class1.apxc

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

batch_class1.apxc [Apex Class Controller]

  1.   global class batch_class1 implements DATABASE.Batchable<sObject>{
  2.  
  3.     global batch_class1(){
  4.         List<Account> accList = [SELECT Id, Name, TYPE FROM Account WHERE TYPE != NULL];        
  5.         List<String> strList = NEW List<String>();
  6.         FOR(Account a1:accList){ 
  7.             strList.add(a1.Type);
  8.         }
  9.     }
  10.  
  11.     global DATABASE.QueryLocator START(DATABASE.BatchableContext BC){
  12.         string query = 'Select id,Name,Type,Description,Industry From Account';
  13.         RETURN DATABASE.getQueryLocator(query);
  14.     }
  15.  
  16.     global void EXECUTE(DATABASE.BatchableContext BC, List<Account> scope){
  17.         FOR(Account s:scope){
  18.           List<Account> acclist1 = NEW List<Account>();  
  19.             IF(s.Type != NULL){
  20.                  s.Name=s.Type;
  21.                  s.Industry = 'Compute Data Analytics';
  22.                 acclist1.add(s);
  23.             }
  24.  
  25.             UPDATE acclist1;
  26.         }
  27.     }
  28.  
  29.     global void finish(DATABASE.BatchableContext BC){
  30.  
  31.       batch_class1 b = NEW batch_class1();
  32.      DATABASE.executeBatch(b);
  33.  
  34.     }
  35.  
  36.  
  37. }

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

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
   
  1.     batch_class1 b1 = NEW batch_class1();
  2.     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]

  1.     global class batch_schedule implements Schedulable{
  2.     public static void EXECUTE(SchedulableContext sc){
  3.         ID batchInstanceID = DATABASE.executeBatch(NEW batch_class1(),10);        
  4.     }
  5. }

batch class to update account field in salesforce -- w3web.net

Further post that would you like to learn in Salesforce

 


 

FAQ (Frequently Asked Questions)

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

 
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