10

Is there any way/workaround at all to call a future method immediately from another future event?

If not what's the fastest way to call my future method indirectly?

I basically have a 3rd party package that updates a field from a future event. When this field is updated I want to fire off another future event.

I have to use a future method as I am doing a callout.

  • 2
    I highly recommend Dan Appleman's design patterns for async apex. It will solve your future issues. This link m.youtube.com/watch?v=ZqqiIg_mynY is well worth the 35 minutes. – cropredy Jul 2 '14 at 6:35
  • @crop1645 that is a great link, thanks for sharing!! – Nathan Williams Jul 10 '15 at 21:56
  • 2
    @NathanWilliams - Appleman's book 'Advanced Apex Second Edition' enhances the YouTube video with more features and corresponding sample code. See advancedapex.com/blog - well worth the $$ – cropredy Jul 10 '15 at 23:36
  • I actually just purchased that one a few days ago, thanks! – Nathan Williams Jul 13 '15 at 16:12
8

In Winter15 there's a new apex interface, Queueable, that acts very similar to @future but allows chaining of invocations: http://docs.releasenotes.salesforce.com/he-il/winter15/release-notes/rn_apex_queueing_jobs.htm

The release notes specifically call out chaining queueable jobs as a supported use case, so if you can wait until winter15 is live it's your best bet.

1

Set a "gatekeeper" method to determine whether it should run in future or not, based on whether it's already in future mode.

public static void runMethod()
{
    if (System.isFuture() || System.isBatch())
        runMethodNow();
    else
        runMethodFuture();
}

@Future
static void runMethodFuture()
{
    runMethodNow();
}

static void runMethodNow()
{
    //do what you need to do here
}
0

One option:

  1. Create a checkbox to fire off your future method.
  2. Create a time based workflow that when the field the package is updating is modified, you kick off the time based workflow to check the checkbox you created.
  3. When the timebases workflow check the box you created, in the trigger uncheck it and then call your future method.

Another option is to have a batch running every so often to process the records. Make sure to set the batch size to 10 so you do not perform too many callouts.

  • What's the fastest this can be done? The time based workflow has a minimum of one hour. – Rowan Jul 2 '14 at 9:23
  • Try this link to set a workflow that fires in a fraction of an hour: sftipshack.wordpress.com/2013/10/21/… – Eric Jul 3 '14 at 15:20
-1

we can avoid Future Methods being called by Future method.There are two ways of achieving this:

  1. Add a new field to the object so the trigger can inspect the record to see if it is being called by the future method.

  2. Use a static variable to store the state of the trigger processing.

Please refer this link for getting more details of this approach:

http://blog.jeffdouglas.com/2009/10/02/preventing-recursive-future-method-calls-in-salesforce/

  • 3
    Or you could simply use system.isFuture() to see if it is in the future context. – Eric Jul 2 '14 at 6:02
  • I want to call the future method. That prevents the call – Rowan Jul 2 '14 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.