function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
srinu jsrinu j 

Can we call a batch apex from visualforce page?

Best Answer chosen by srinu j
Amit Chaudhary 8Amit Chaudhary 8

Write a method in the controller and call it in the VF page using a command button. Inside the method use the below code
public void callBatch()
{
 CleanUpRecords c = new CleanUpRecords(query);
 Database.executeBatch(c);
}

Please check below post for sample code.
http://salesforce.stackexchange.com/questions/83812/check-the-status-of-apex-class

You can also use action poller to see the status of batch job.

Action Poller:-
Supporse you need to execute one Apex batch job from Visaul force page and need to show the status of Apex batch job on visual force page.
In that case you can use ActionPoller to refresh the seaction to show Batch job updated status.
Main Use or Action Poller is to refersh the VF page section. A timer that sends an AJAX request to the server according to a time interval that you specify
This tag specifies a timer that sends an AJAX update request at specified interval. Minimum interval time to refresh is 5 sec.

https://www.salesforce.com/docs/developer/pages/Content/pages_compref_actionPoller.htm
http://www.salesforcetutorial.com/actionpoller-tag-salesforce/

Please let us know of this will help you.
 

All Answers

James LoghryJames Loghry
Yes.  Your Visualforce page will need either a custom controller or an extension class.  From that class you can use the Database.executeBatch method to kick off the asynchronous method.  
Amit Chaudhary 8Amit Chaudhary 8

Write a method in the controller and call it in the VF page using a command button. Inside the method use the below code
public void callBatch()
{
 CleanUpRecords c = new CleanUpRecords(query);
 Database.executeBatch(c);
}

Please check below post for sample code.
http://salesforce.stackexchange.com/questions/83812/check-the-status-of-apex-class

You can also use action poller to see the status of batch job.

Action Poller:-
Supporse you need to execute one Apex batch job from Visaul force page and need to show the status of Apex batch job on visual force page.
In that case you can use ActionPoller to refresh the seaction to show Batch job updated status.
Main Use or Action Poller is to refersh the VF page section. A timer that sends an AJAX request to the server according to a time interval that you specify
This tag specifies a timer that sends an AJAX update request at specified interval. Minimum interval time to refresh is 5 sec.

https://www.salesforce.com/docs/developer/pages/Content/pages_compref_actionPoller.htm
http://www.salesforcetutorial.com/actionpoller-tag-salesforce/

Please let us know of this will help you.
 
This was selected as the best answer