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
Suresh suri kSuresh suri k 

How to show the records in visual force page using bacth apex?

NagendraNagendra (Salesforce Developers) 
Hi Suresh,

Batch Apex does not return a list of records. Database.execute(batch, scope) only returns the ID of the Batch Job that was placed in the Queue.
 
Furthermore, as you note, it is Asynchronous. There is no guarantee of when it will execute in the queue, or when it will finish. Creating a VF Page to view the output of a Batch Apex process is not going to work at all as you expect.
 
You can look at performing this functionality in this type of pattern.
 
1. User requests batch processing by clicking your VF Page Button, which invokes the Batch Process. This gives them a screen, "Processing Batch...... The email will be sent when finished".
2. Upon Batch Finish method, send an email to the user with a Link to your VF Page which will display the processed records.
3. In the Controller for the VF Page, you need to Query to get the records processed (NOTE: You will be limited to 10,000 records as this is Controller).
4. You would need to establish some sort of criteria to establish what records were processed in the Batch. You could create a custom field which holds the BatchID of the processing batch on the records, then query for all records processed by the Batch.

Thanks,
Nagendra
Ajay K DubediAjay K Dubedi
Hi Suresh,
You can execute your batch class with in command button action method in controller. so whenever you click on command button batch class will be executed. Please see below sample code for your reference:
//Vf Page
apex:commandButton value="create account" Action="{!callBatch}"
//Controller
public void callBatch()
{
 CreateAccountRecordsBatch c = new CreateAccountRecordsBatch();
 Database.executeBatch(c);
}
//CreateAccountRecordsBatch is your batch class fetching account records via SOQL query. To know more on how to fetch records follow: https://developer.salesforce.com/blogs/engineering/2013/02/force-com-batch-apex-and-large-data-volumes.html

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi