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
Dev1234Dev1234 

Calling batch apex process from a controller

Hi,

I want to display a list of records processed out of a batch process on a Visuaforce page for user operation.

The Batch process returns a list of records but the list is not passed to the VF controller. The list shows empty when debugged in the Vf controller. I do  realise that the batch process runs asynchronously, but the requirement is really critical.

 

Please reply as soon as possible. Any pointers will be highly appreciated. Thanks in advance.

 

Regards,

Dev1234.

Best Answer chosen by Admin (Salesforce Developers) 
Imran MohammedImran Mohammed

Implement Database.Stateful in your Batch Apex class.

Create a Map or List variable which is Global and which will be used to store the records processed.

Also create a Global Boolean variable that you will use in the Finish method of Batch Apex and turn it true when all processes are finished.

 

In your VF page, you have to use window.setInterval function and invoke a controller method  at regular intervals that will check whether the Boolean variable in Batch Apex has turned true.

If its true then you can use the Map or list you defined in Batch Apex.

 

 

All Answers

Imran MohammedImran Mohammed

Implement Database.Stateful in your Batch Apex class.

Create a Map or List variable which is Global and which will be used to store the records processed.

Also create a Global Boolean variable that you will use in the Finish method of Batch Apex and turn it true when all processes are finished.

 

In your VF page, you have to use window.setInterval function and invoke a controller method  at regular intervals that will check whether the Boolean variable in Batch Apex has turned true.

If its true then you can use the Map or list you defined in Batch Apex.

 

 

This was selected as the best answer
andhra123andhra123

Hi Imran,

 

Can you please provide some sample code how to declare the variable in the batch apex. I have a requirement to get the count of records processed by batch apex.

 

Thanks

chubsubchubsub

andhra123, did you ever figure this out?  I'm creating the same thing....a Batch Apex (Stateful) in which I will need to store maps and Lists to hold values and then call the batch from a visualforce page.

andhra123andhra123

Hi chubsub,

 

See the message posted by Imran Mohammed. I applied same logic to get the count of records processed by apex job.

it would be helpful for you too.

 

All the best.

SFDC_DeveloperSFDC_Developer
Hi Dev1234 & Imran,

I'm facing the same problem,
I tried the solution suggested by Imran but it didn't work for me.
Can you please give me some more information.