37 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 ...
  • 451k
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 ...
  • 451k
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 ...
  • 145k
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<...
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 ...
13 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, ...
  • 145k
10 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 ...
  • 64.5k
10 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) { ...
  • 451k
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 ...
  • 4,189
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, ...
  • 451k
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 ...
  • 145k
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 ...
  • 451k
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 ...
  • 52k
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 ...
  • 40.6k
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 ...
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, ...
  • 145k
7 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 ...
  • 451k
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 ...
  • 10.7k
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 ...
  • 451k
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 ...
  • 40.6k
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 ...
  • 451k
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 = ...
  • 451k
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....
  • 451k
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 ...
  • 29.4k
5 votes
Accepted

Queueable Apex: Unable to log exception to a custom error object

Remember that System.enqueueJob runs the job asynchronously so any myErrorObj__c instances created may not appear for some period of time. But I assume the problem here is that by catching but then ...
  • 131k
5 votes
Accepted

Queueable DML capability

You're correct, performing your DML operations inside a Queueable execution is a separate transaction so you will have a fresh set of governor limits.
  • 4,207
5 votes
Accepted

How to set time limit between queued jobs?

This is from Dan Appleman's Advanced Apex Programming Ver 3. The code is available for download from his web site at http://www.AdvancedApex.com This pattern uses a dispatcher class that implements ...
  • 40.6k
5 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/...
  • 52k
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 ...
  • 13k
5 votes

Automated testing of chained Queueable jobs in Salesforce

A quick note to start off, the code you've provided right now doesn't display chaining of queueable classes. Chaining would be that you set up the execution of a queueable class from inside the ...
  • 52k

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