+ Start a Discussion
Raj R.Raj R. 

How to initiate a future class from VF page on button click?

Hi,

We have a VF page that has a button which is supposed to trigger some action. How would we create a button on the VF page that will call a future class once clicked?

The objective is to click the button, then initiate a future class to perform some action that will run in the background while the user is able to proceed. 
Best Answer chosen by Raj R.
GarryPGarryP
You can create a method that is future in your controller and call it on action attribute for commandbutton. here is the example -
///Apex
public with sharing class ExampleController {
    Public static string futureResult {get;set;}

    /*Add the logic here or call any future method as needed */
    @future
    public static void callFutureMethod(){
        futureResult='Future has been Called';
    }
}

Vf
<apex:page controller="ExampleController">
<apex:form >
Future result: {!futureResult}<br/>
<apex:commandButton action="{!callFutureMethod}" reRender="" title="Click to Call Future" value="FutureCall"/>
</apex:form></apex:page>


This is working example and should work in your case as well.
You might have to use ActionPoller to check if the processng has been completed and if user should be notified about the background activity.

Please mark the answerif thsi solves your problem