+ Start a Discussion
Muhammad Jawwad 16Muhammad Jawwad 16 

How to execute Batch Apex form Visualforce Page?

I have three batch classes, I want to execute all three batch classes through button click using visualforce page at the same time.
LoanofficerBatch lob = new LoanofficerBatch(); 
        database.executebatch(lob);
        
        RealtorBatch rb = new RealtorBatch(); 
        database.executebatch(rb);
        
        BuilderBatch bb = new BuilderBatch(); 
        database.executebatch(bb);

please help
Best Answer chosen by Muhammad Jawwad 16
Om PrakashOm Prakash
You need to write above code in a method of Apex class, then call that apex method from Command button of visualforce page.
 
public class BatchApexController(){
 
      public void callBatch(){
         // Your code here to execute the batch class/es
      } 
}
 
<apex:page controller="BatchApexController">
  <apex:form >
    <apex:commandButton action="{!callBatch}" value="Submit Batch" />
  </apex:form>
</apex:page>


This discussion will give you more details https://developer.salesforce.com/forums/?id=906F0000000MIOnIAO