Hot answers tagged

36 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 ...
user avatar
  • 434k
33 votes
Accepted

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

Yes, Queueable can be called from Database.Batchable classes. You must go through enqueueJob for this to work: public class BatchClass implements Database.Batchable<SObject> { public ...
user avatar
  • 434k
25 votes

Is there any way of confirming that an @future method has been called/queued in a test class?

Yes! I love this question, as it's one of my pet peeves. People don't test @future calling methods because they don't know it's possible. Enter the magic of two key methods: Test.StartTest(); Test....
user avatar
  • 6,978
22 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 {
user avatar
  • 434k
21 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 ...
user avatar
  • 48.4k
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 ...
user avatar
  • 434k
19 votes

Future Method calling Another Future Method

While a @future method cannot call another, it can execute a Queueable. Here is some documentation. They might even be more useful than @future methods for both operations, as they can be chained.
user avatar
  • 143k
18 votes

Is there any way of confirming that an @future method has been called/queued in a test class?

The method looks like this: static public Boolean isFutureRunning(System.Type type, String methodName) { String namespacePrefix; String name; if (type.getName().contains('.')) { /...
user avatar
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, ...
user avatar
  • 434k
17 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....
user avatar
  • 434k
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 ...
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 ...
user avatar
13 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. ...
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 ...
user avatar
  • 434k
12 votes

Is there any way of confirming that an @future method has been called/queued in a test class?

With the newly available Queueable interface this problem becomes more approachable. replace your @Future annotated method with a Queueable inner class lean on Limits.getQueueableJobs() to count the ...
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,...
user avatar
  • 5,547
12 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<...
user avatar
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 ...
user avatar
  • 12.8k
10 votes

You have uncommitted work pending. Please commit or rollback before calling out

This is a very common issue that one encounters The issue happens you are doing a DML before you do a Callout .To replicate this issue one can try things like Account acc=new Account(); acc.Name=...
user avatar
10 votes
Accepted

Saleforce Too many future calls: 11

The governor limit is that no more than 10 future calls may be made in a single request so your trigger is broken as soon as more than 10 Case objects are updated at once that match you if condition. ...
user avatar
  • 130k
10 votes
Accepted

Future vs batch

I would take a look at Dan Appleman's DF13 Presentation here - he talks about these types of scenarios and how you can create Batch/Schedule Managers to achieve the type of continuous processing you ...
user avatar
10 votes
Accepted

Future method cannot be called from a future or batch method, Case Trigger with Salesforce for Social

Do a check in your trigger for system.isBatch() || system.isFuture() and if true then exit trigger
user avatar
  • 53.1k
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....
user avatar
10 votes
Accepted

Prevent trigger from running in single test

The only way to do this would be to code the functionality into your trigger. You could make use of static variables to prevent the trigger from running. Just create some class public class ...
user avatar
10 votes
Accepted

Future Method calling Another Future Method

You cannot call another future method from a future method. As per Salesforce documentation, You cannot call a method annotated with future from a method that also has the future annotation. Nor ...
user avatar
  • 3,491
10 votes
Accepted

Debug or view status of a @future method

Future methods can be debugged in two ways. First, you can open the developer console and perform the action that causes the future method to be called. Second, you can enable debug logs in Setup / ...
user avatar
  • 434k
10 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 ...
user avatar
  • 12.8k
10 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 ...
user avatar
  • 7,553
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 ...
user avatar
  • 434k
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 ...
user avatar
  • 143k

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