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
raj_sfdccraj_sfdcc 

Invoking Batch class from visulforce page using button click .

Hello Experts ,

Description :
i have a visualforcepage called 'generate_records' and 'schedulebatch' custom controller .From Controller i am calling batch class(MyBatchclass) and which process the large number of records .

Help Needed :
When user clicks on button that bacth starts running in the backgroud .so till the process is completed user should wait .Can you please help me how to show user something like"Loading plese wait" till the process is completed and then redirecing to some other page.
Mandalapu BrahmanaiduMandalapu Brahmanaidu
Hi
 Please find the code below.

Visualforce page
----------------
<apex:page controller="DemoBatchApexController" showHeader="false">
    <apex:slds />
    <apex:form id="formId">
    <apex:outputpanel id="pgBlckId">
            <apex:commandButton action="{!invokeBatch}" value="invokeBatch" reRender="pgBlckId" status="actStatusId"/>
            <apex:actionStatus id="actStatusId" >
                <apex:facet name="start" >
                <b>Loading......</b>           
                </apex:facet>
            </apex:actionStatus>
   
    </apex:outputpanel>
    </apex:form>
</apex:page>
DemoBatchApexController
------------------------
public class DemoBatchApexController {

    public PageReference invokeBatch() 
    {
        //BatchDemo is the batch class name
        BatchDemo batchClass=new BatchDemo();
        //executing the batch apex
        Database.executeBatch(batchClass);
        return null;
    }

}

Batch class
-----------
global  class BatchDemo implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext bc)
    {
       System.debug('Batch apex processing started');
       String query='select Id,Name,Site from schema.Account';
       return Database.getQueryLocator(query);
    
        
    }
    global void execute(Database.BatchableContext bc,List<schema.Account> accList)
    {
        for(schema.Account acc:accList)
        {
            acc.Site='Approved';
        }
          try
        {
            update accList;
        }
        catch(Exception e)
        {
            System.debug(e.getMessage());
        }
         
    }    
    global void finish(Database.BatchableContext bc)
    {
        
    }

}

If you find it is helpful to you then please mark it as best answer

Thanks&Regards
Brahmanaidu
https://sfdcscenarios.blogspot.com