You need to sign in to do that
Don't have an account?
mwille64
Rerendering a visualforce page from apex controller implementing Database.Batchable
I have a Visualforce page that manages the generation, update and removal of records for annual processing of allocation processes. This is a once a year mannual process in which a user decides to delete older records (previous years), generate records for coming years or simply add records for new allocatable objects.
The controller is using the Database.batchable class to handle the rather large amount of records deleted or created in one go. This means that a simple Apex:ActionFunction to rerender the page isn't doing the trick, as the process triggered by the button is asyncronous and might complete only after several minutes.
I'm passing the controller object to the class that handles the batch calls, but I have not figured out a way to trigger via the page controller a rerendering of the page, once the batch processing is completed.
Any suggestions on how to achieve that?
The controller is using the Database.batchable class to handle the rather large amount of records deleted or created in one go. This means that a simple Apex:ActionFunction to rerender the page isn't doing the trick, as the process triggered by the button is asyncronous and might complete only after several minutes.
I'm passing the controller object to the class that handles the batch calls, but I have not figured out a way to trigger via the page controller a rerendering of the page, once the batch processing is completed.
Any suggestions on how to achieve that?
You can use a combination of JavaScript and Apex to rerender the page after the batch processing is completed. You can use an apex action function to call an apex method in your page controller, which will update a boolean flag indicating that the batch process has completed. In your JavaScript code, you can then use setInterval method to repeatedly check the value of this flag and rerender the page once the value changes to true. VF controller Batch
All Answers
You can use a combination of JavaScript and Apex to rerender the page after the batch processing is completed. You can use an apex action function to call an apex method in your page controller, which will update a boolean flag indicating that the batch process has completed. In your JavaScript code, you can then use setInterval method to repeatedly check the value of this flag and rerender the page once the value changes to true. VF controller Batch
- Where exactly in the Visualforce Page should the Javascript be placed? Currently I placed it in the PageBlock to be rendered.
- When exactly is the timer initiated?
So far so good. What I have not managed to implement is the following...
global void finish(Database.BatchableContext bc)
{
// Update the batchProcessCompleted flag in your page controller
}
I have tried to pass on the Controller object to the batch class on instantiation, but setting the flag has zero effect. The flag remains set to false...
public with sharing class ManageRoomOccupationsController
{
...
public void ResetRoomOccupations()
{
batchProcessCompleted = false;
Database.executeBatch(new resetRoomOccupationsBatch(this, Integer.valueOf(YearSelected),50000,false), 5000);
}
...
}
global class resetRoomOccupationsBatch implements Database.Batchable<sObject>, Database.Stateful
{
integer YearSelected;
integer maxBatchSize;
boolean isTestRun;
long deletecount;
ManageRoomOccupationsController parentctrl;
public resetRoomOccupationsBatch(ManageRoomOccupationsController pctrl, integer year, integer recnum, boolean istest)
{
YearSelected = year;
maxBatchSize = recnum;
isTestRun = istest;
parentctrl = pctrl;
deletecount = 0;
}
...
global void finish(Database.BatchableContext BC)
{
parentctrl.render_Processing = 'false';
parentctrl.batchProcessCompleted = true;
...
}
}