1 Answer 1

2

There are three things:-

  1. You want to invoke future method from vf page from constructor of controller class

You cannot not call this, You will get an error.

  1. You want to invoke future method from Visualforce page not from constructor but via action attribute

You can do this

public with sharing class sampleClass {
  @future
  public static void futureSampleMethod() {
    system.debug('futureSampleMethod ');
  }
}

and in Vf page

<apex:page controller="sampleClass" action="{!futureSampleMethod}">
 </apex:page>

Note:-

The action method invoked when this page is requested by the server. Use expression language to reference an action method. For example, action="{!doAction}" references the doAction() method in the controller. If an action isn’t specified, the page loads as usual. If the action method returns null, the page simply refreshes.

This method is called before the page is rendered, and allows you to optionally redirect the user to another page.

Important: This action should not be used for initialization or DML.

  1. You want to invoke future method from any other class which is not part of visualforce page

You can do this

public with sharing class sampleClass {
    public sampleClass() {
        futureSampleMethod();
    }
    @future
    public static void futureSampleMethod() {
        system.debug('futureSampleMethod ');
    }
}

and you can call it from any another class:-

new sampleClass();

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .