+ Start a Discussion
Marc C.Marc C. 

Am I in a @future method?

We are experiencing difficulty re-using code which involves @future methods. If a method is not marked @future and does DML it cannot be called before a callout. If we mark it as @future it cannot be called from some other @future method. We end up duplicating methods and having pairs of doThis() and doThis_future() or with optional boolean useFuture parameters. Is there a better way?

 

Example:

@future

public static void method1() {

   // Do some future async DML

}

 

// method1() can be called before callouts

// method1() cannot be called from other @future methods

// nor anywhere in a chain starting with a @future method

sfdcfoxsfdcfox

I usually use dispatch functions in order to provide a seamless interface. For example:

 

public static void method1() {
  if(System.isFuture())
    method1_dml();
  else
    method1_future();
}

private static void method1_dml() {

}

@future
private static void method1_future() {

}

You could also choose to use a boolean parameter or some other logic. The functions are private so they can only be called through the dispatch function.

Shailesh DeshpandeShailesh Deshpande
I haven't implemented this, but I suggest you to try batch apex in this case. As per winter 13 release notes, you can call another batch class from the finish method of the current batch class.

Hope this helps.