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 ...
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 ...
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 ...
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, ...
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 ...
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 ...
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) {
...
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 ...
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, ...
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 ...
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 ...
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 ...
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 ...
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 ...
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, ...
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 ...
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 ...
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 ...
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 ...
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/...
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 ...
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 = ...
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....
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 ...
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 ...
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.
...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
queueable-apex × 157apex × 88
callout × 24
batch × 23
asynchronous × 19
queueable-interface × 19
future × 17
unit-test × 15
trigger × 13
governorlimits × 12
scheduled-apex × 9
asyncapexjob × 8
queueable × 8
transaction-finalizers × 8
visual-workflow × 6
rest-api × 4
api × 4
code-coverage × 4
httprequest × 4
queue × 4
soql × 3
schedulebatch × 3
invocable-method × 3
asynchronous-testing × 3
visualforce × 2