Hot answers tagged

39 votes
Accepted

Difference between Queueable apex and future method

Your logic is slightly flawed. All async code can run in to stale data. The difference between Batchable, Queueable, and Future methods is how they're designed to behave. Batchable calls were designed ...
sfdcfox's user avatar
  • 481k
32 votes
Accepted

Queueable - Callout not allowed from this future method

You need to add Database.AllowsCallouts to your class, not @future(callout=true). The error is erroneous. public class getAccountUpdatesfromEndpoint implements queueable, Database.AllowsCallouts {
sfdcfox's user avatar
  • 481k
22 votes

Error: Future method cannot be called from a future or batch class

The error is straightforward. To work around it, you need to detect when you're already in an @future or batch context, and then simply not call your @future method(s). Dan Appleman has a pretty good ...
Derek F's user avatar
  • 59.3k
20 votes
Accepted

How to work around the 100 callout limitation?

Create a batchable class, and do as many callouts as you need. The 100 callout limit applies only to a single transaction, but Batchable classes can create millions of transactions. There's plenty of ...
sfdcfox's user avatar
  • 481k
19 votes
Accepted

Does queueing a future method count as DML?

Using System.enqueueJob, Database.executeBatch, System.scheduleBatch, System.enqueueJob, and @future methods all modify the state of the database, so for purposes of callouts, count as a DML operation....
sfdcfox's user avatar
  • 481k
18 votes
Accepted

@future runs in test without Test.stopTest() - Can that be?

It sounds like a bug, or at least it is not documented; I would not depend on this behavior. If you want to test asynchronous code, use Test.stopTest(). This applies for Schedulable, Queueable, ...
sfdcfox's user avatar
  • 481k
16 votes
Accepted

Error: Future method cannot be called from a future or batch method

First thing even though future methods are there for a while they are a thing of past. Going forward unless you have existing future code the suggestion is to move your code to Queueable apex. Think ...
Hemavantha Rajesh Varma Mudunu's user avatar
15 votes

Pass List<SObject> to @Future

According to the documentation here: Methods with the future annotation must be static methods, and can only return a void type. The specified parameters must be primitive data types, arrays of ...
Todd Sprinkel's user avatar
14 votes

How to work around the 100 callout limitation?

2999 is a lot of callouts to be making in one transaction. Is the user going to sit there and wait for them to finish? Brian's suggestion to use Batch Apex is a common way to deal will bulk callouts. ...
Daniel Ballinger's user avatar
13 votes
Accepted

Help on Invokable Apex Test class and @Future callout Apex test Class

Basically, you need to know about how to test HTTP callouts, and how to test future methods. Once you've gotten that far, you'll see that the unit test would look like: @isTest class MakeCalloutTest ...
sfdcfox's user avatar
  • 481k
13 votes
Accepted

Callout Limits for future methods and queueable apex

I quickly wrote a class with future method to test this behaviour. public class FutureClassLimitsTest { @future(callout=true) public static void docallouts(){ for(Integer i=0;i<...
Pranay Jaiswal's user avatar
12 votes
Accepted

Is it possible to handle Trigger.oldMap in @future method?

You can user JSON.serialize() in your trigger to serialize the Old and New Maps and JSON.deserialize() in your @future annotated method to get the actual object and compare the old and new values. So,...
Mahmood's user avatar
  • 5,924
11 votes

Wait for Future Method execution before doing DML

SF will just not let you do a synchronous callout from a trigger. Which makes sense because the external system might be slow, and SF has to lock the record being updated while your trigger runs. If ...
Aidan's user avatar
  • 13.5k
11 votes
Accepted

Diference between @future (callout= true / false)

If you want to call third party system or any http call outside of salesforce system you need to set callout=true in the future method. If you set it to false it means the method is not making any ...
RedDevil's user avatar
  • 7,765
11 votes
Accepted

Guarantee only a single asynchronous job runs at a time

I've implemented the following in numerous orgs and it works pretty well. It's similar to Keith's suggestion, but a bit more detailed and it does usually do near-realtime processing: Create a custom ...
Aidan's user avatar
  • 13.5k
10 votes

Can Queueable solve "Future method cannot be called from a future or batch method"?

Wasted about 2 months to get an answer from salesforce support why I get Too many queueable jobs added to the queue: 2 when documentation (https://developer.salesforce.com/docs/atlas.en-us....
Yury Bondarau's user avatar
10 votes
Accepted

Why is Map<Id, Set<Id>> an unsupported parameter type in future methods?

The list of primitives doesn't include Map, Set, or List. Therefore, what you have is a collection of non-primitives. They've also explicitly excluded Object, so you can't weasel your way around that ...
sfdcfox's user avatar
  • 481k
10 votes
Accepted

Future Method and Static Variables

Static variables in Apex only retain their value through the course of a single transaction. Future methods by nature execute in a separate transaction, which means that your static variables are ...
David Reed's user avatar
  • 92.1k
10 votes
Accepted

First error: Too many query rows: 50001

While it won't fix your issue, never use the global access modifier unless you know you need it. Take advantage of the QueryLocator pattern and iterate over your query itself: Instead of: global ...
Adrian Larson's user avatar
  • 149k
9 votes
Accepted

Why do debug logs not always work for a @future method?

The problem was -- I was executing anonymous code and checking the "Open Log" option. The call was actually generating two separate logs -- one for the anonymous code, and another for FutureHandler, ...
serverpunk's user avatar
9 votes
Accepted

Future Method to Trigger to Future Method

If your code is already asynchronous, you don't need to use a @future method to call out. So you can add checks for this sort of thing. public static void makeCallout(/*params*/) { if (!system....
Adrian Larson's user avatar
  • 149k
9 votes
Accepted

Confused by CalloutException

Some operations are "DML-ish", meaning they persist something to the database to be committed at the end of the transaction, just not a standard DML operation on an sObject. Enqueuing Batch Apex, for ...
David Reed's user avatar
  • 92.1k
8 votes
Accepted

unsupported parameter in future method

Future method parameters are limited to primitives and collections of primitives. You're attempting to pass in a collection of collections, which is not supported. You're correct that you can use ...
JCD's user avatar
  • 3,385
8 votes

First error: [UNKNOWN_EXCEPTION] ConcurrentPerOrgLongTxn Limit exceeded

I've done some more research to this and created a repository containing code to reproduce: https://github.com/koenfaro90/SFDC-ConcurrentPerOrgLongTxn-Reproduction It seems that these days the ...
Koen Faro's user avatar
  • 773
8 votes

Diference between @future (callout= true / false)

The document you linked and the annotation itself both seem quite clear: To allow callouts in a future method, specify (callout=true). The default is (callout=false), which prevents a method from ...
Adrian Larson's user avatar
  • 149k
8 votes

How to refactor Synchronous code with future method

There is no choice here but to move the "association code" to a method that you call from the end of the future method and the other classes: public class MyUtilClass { @future public static ...
Keith C's user avatar
  • 135k
8 votes
Accepted

How to do a dependent callout from a future callout

Instead of using @future, you should use Queueable Apex. Queueable Apex, lets you chain jobs. To allow callouts, you need to specify Database.AllowsCallouts public class AsyncExecutionExample ...
Daniel Hoechst's user avatar
8 votes
Accepted

Why would a future method not show up in debug logs?

Future methods, once called, are only temporary until a database commit occurs. If an operation causes a partial or total rollback, this will include the future method calls, as well as any emails ...
sfdcfox's user avatar
  • 481k
7 votes

how to test a future method which makes callouts?

Here is where Test.startTest() and Test.stopTest() come in to play. Structure your test to do all the data setup, then call startTest(). Execute your tested method and then call StopTest(). Among ...
Kevin P's user avatar
  • 7,188
7 votes
Accepted

Future method cannot be called from a future or batch method: Error

As the error states you are calling a future method from another future method . Your class A calls Class B but I see again you call Class A methods in Class B causing recursion . Hence would ...
Mohith Shrivastava's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible