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
  • 480k
20 votes
Accepted

Chaining Queueables: Clarification & Practical Usage

There's two modes of operation in Salesforce: synchronous and asynchronous. The rule is that if you're synchronous, you get 50 jobs for that transaction. Once you go asynchronous, you get only one ...
sfdcfox's user avatar
  • 480k
17 votes
Accepted

Uncommitted work pending in unit test with trigger and queueable callout

You'll need to test the method directly, and test the Queueable separately. The easiest way to do so is a static flag. public class MyQueueable implements Queueable @TestVisible static Boolean ...
Adrian Larson's user avatar
  • 149k
14 votes
Accepted

Infinite queueable chaining, what is the drawback?

The primary limitation you will face is throttling. Once you surpass a chain depth of 4, Salesforce will slow you down to 15 seconds between executions. If you need to process a high volume of jobs, ...
Adrian Larson's user avatar
  • 149k
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
13 votes
Accepted

Are queueable jobs actually queueable?

No, I don't believe the order of execution for Queueable Apex is defined. They "will be processed when system resources become available" (Source). The AsyncApexJob records that represent the queued ...
Daniel Ballinger's user avatar
12 votes
Accepted

How can I share state between 'Queueable' executions?

The standard approach for this is to pass state via the constructor class MyQueueable { Object state; public void execute(QueueableContext qc) { // do work using state up to the point where ...
cropredy's user avatar
  • 69.8k
11 votes

How can I share state between 'Queueable' executions?

A class calling itself can enqueue itself. No need to construct a new object. public class MyQueueable implements Queueable { Integer counter = 0; public void execute(QueueableContext context) { ...
sfdcfox's user avatar
  • 480k
9 votes

Queuable Apex logic in Constructor or Execute Method

The most important difference would be that constructor is executed in current execution context, and execute method is executed in new one. In practice it mean that if you call your queueable in ...
kurunve's user avatar
  • 4,259
9 votes
Accepted

Queueable apex and trigger execution context

Yes, this is guaranteed (though not explicitly documented). The reason why is that, until the transaction fully commits, the Queueable job call may be rolled back, either because of governor limits, ...
sfdcfox's user avatar
  • 480k
8 votes
Accepted

Access Data on Queueable class

Couple things. You need the List<String> to be an instance property, not a static one. I would also move the instantiation to reside solely in the constructor. public class SampleQueueble ...
Adrian Larson's user avatar
  • 149k
8 votes
Accepted

Queueable Apex in a Before Trigger

In a before insert trigger, there are no record Ids assigned yet. If you're testing in the UI, try creating a new record, it should have the same error (but it won't be visible in the UI since it is ...
sfdcfox's user avatar
  • 480k
8 votes

Why queuable apex accepts sobjects where as future methods doesn't?

Batchable, future, and queueable were basically three versions of the same feature request from customers. Initially, customers wanted a way to process bulk data asynchronously. We were given ...
sfdcfox's user avatar
  • 480k
8 votes

Queuable Apex logic in Constructor or Execute Method

Tl;dr: I don't recommend using a constructor like this. I think this would violate the principle of least astonishment in a few ways: Constructor has logic in it (when they're normally used to ...
Derek F's user avatar
  • 59.2k
7 votes
Accepted

Is it possible to queue a queueable apex job from a scheduled apex job?

You can do essentially what you're asking, but there's a couple of precautions you'll want to take. First, you'll want to create an "on/off" switch that allows you to shut down your queueables ...
crmprogdev's user avatar
  • 40.9k
7 votes

Is it possible to queue a queueable apex job from a scheduled apex job?

I can confirm that this works - I have just created these classes and executed them and they successfully ran. For reference, here is the exact code: Schedulable: global class ...
Caspar Harmer's user avatar
7 votes
Accepted

Limit of Queueable Jobs Added to the Queue?

See Queueable Apex (emphasis mine): Chaining Jobs If you need to run a job after some other processing is done first by another job, you can chain queueable jobs. To chain a job to another job, ...
Adrian Larson's user avatar
  • 149k
7 votes
Accepted

Queuable Apex logic in Constructor or Execute Method

I would say, the constructor is best used just as "prep". The work you want to enqueue should be done in the execute method. You always have the option, when constructing a Queueable, to run the work ...
Charles T's user avatar
  • 10.9k
7 votes

Queueable design patterns to avoid "Too many queueable jobs added"?

The "too many queueable jobs" error is a per transaction error. You can have an "unlimited" number of queueable and future items in the queue of jobs to execute, unlike scheduled ...
sfdcfox's user avatar
  • 480k
7 votes
Accepted

How to avoid System.AsyncException: Maximum stack depth has been reached

This limitation is specifically only enforced in Sandboxes and Developer Edition orgs. In production orgs, you are allowed to chain indefinitely (but, be aware of the backoff mechanism as described in ...
sfdcfox's user avatar
  • 480k
6 votes
Accepted

Test execution order issues with Queueable and @future method

What you describe is an inherent limitation of testing code that runs asynchronously in a future context. When you enqueue your Queueable class to run, you're scheduling it to run at "some time in the ...
crmprogdev's user avatar
  • 40.9k
6 votes

Callout Limits for future methods and queueable apex

tl;dr: The limit of 100 http callouts is the same for all transactions (be they synchronous or async). The answer here resides in another piece of documentation: https://developer.salesforce.com/docs/...
Derek F's user avatar
  • 59.2k
6 votes
Accepted

Queueable Apex consideratoins

Having built a "rate-limited multiplexing API engine" before, I can tell you that a robust solution can quickly get very large. We had a complicated system that had the following features: Throttled ...
sfdcfox's user avatar
  • 480k
6 votes

Automated testing of chained Queueable jobs in Salesforce

When you need more than one execution, but you want to test the asynchronous code independently, you can cheat by rolling them back. Here's an example in a unit test: Test.startTest(); SavePoint sp = ...
sfdcfox's user avatar
  • 480k
6 votes
Accepted

Queueable Apex vs process builder and Flow

Apex is superior in all three categories for maintainability, scalability, and efficiency. Flows typically take longer to edit than code, do not run efficiently, and may cause problems on bulk updates....
sfdcfox's user avatar
  • 480k
6 votes
Accepted

Apex more than 50 Queueble jobs

Why don't you use Queueable to process a 'Queue' of Order's? As per the docs: No limit is enforced on the depth of chained jobs, which means that you can chain one job to another job and repeat ...
Phil Hawthorn's user avatar
6 votes

Mutation of Finalizer state

Fundamentally this is letting the developer know that if they have a class that implements both Queueable and Finalizer, where the Queueable "attaches" itself as its own finalizer, any state ...
Phil W's user avatar
  • 34.1k
6 votes
Accepted

Why is queueable not picking updated state?

Executions for Queueable, Batchable, Schedulable and Platform Events are handled by persisting the state of the (async) object at the point the relevant "execution request" method is called. ...
Phil W's user avatar
  • 34.1k
5 votes

Queueable Apex consideratoins

Queueable won't fully solve your problem. Queueable can ensure that the asyc stuff starts in order, but doesn't take into account how the order may change as the external systems communicate over the ...
Aidan's user avatar
  • 13.5k
5 votes
Accepted

Issue with Invocable Method to Call Queueable

Your oppList shouldn't be static. It should be a member variable. Try this: public List<Id> oppList; Note how you are creating a new Queueable in this line: System.enqueueJob(new ...
Caspar Harmer's user avatar

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