34 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 ...
  • 445k
24 votes

Dramatic increase in AsyncApexJob latency

When you say "schedule a task", I'm going to assume you're talking about a batch class that performs work. If that's the case, I believe your issue is related to how asynchronous processing works on ...
  • 40.5k
23 votes
Accepted

How can I cause side effects outside an execution context?

Update, Summer '17: Platform Events are now GA. Note the following from the docs: Platform Events and Transactions Unlike custom objects, platform events aren’t processed within database ...
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, ...
  • 445k
17 votes
Accepted

Do Platform Event Triggers process synchronously? Do they get a fresh set of limits?

Are the subscription triggers processed synchronously or asynchronously? Aynchronously Do the subscription triggers get separate limits from the publishing context? Yes, separate limits from the ...
16 votes
Accepted

How do I test asynchronous Apex?

Asynchronous Apex includes all of the methods for executing code on the Salesforce platform outside a synchronous transaction, including: @future methods. Batch Apex Queueable Apex Schedulable Apex ...
15 votes

How can I cause side effects outside an execution context?

I wrote a bit on my blog last week about how I was using the Tooling api to effectively create an "eval()" method. You could replace the transaction mechanism with a similar eval mechanism. The "eval'...
  • 7,008
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<...
11 votes
Accepted

How to run an asynchronous job in developer console?

I suspect you won't be able to get true asynchronous anonymous code working. The problem is that your anonymous Apex only exists for the transaction it was created in. By the time the asynchronous ...
11 votes
Accepted

Governor Limits on System.enqueueJob in Asynchronous Apex – Documentation Wrong?

Yes, the documentation in question is incorrect. You can prove that the Limits class does not conform to what is written therein fairly easily: public class Demo implements Queueable { public void ...
  • 144k
11 votes

Error "Too many queueable jobs added to the queue: 2"

Salesforce says you can add 50 jobs to the queue per transaction. But what Salesforce doesn't mention clearly is that it is not applicable for batch, you can only enqueue one job. Even if you enqueue ...
  • 5,518
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 ...
  • 13k
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....
10 votes
Accepted

How can my code tell if it's inside a Queueable context?

Spring '16 brings us a new method: System.isQueueable() which takes care of this requirement. releasenotes.salesforce.com/en-us/spring16/release-notes/rn_apex_new_classes_methods.htm As of Spring '...
9 votes

How to test @future methods with setup objects and avoid MIXED_DML_OPERATION

A longstanding trick that I've seen used time and time again to work around MIXED_DML_OPERATION when doing test data setup is to wrap the setup DML operation in a System.runas(new User(Id = UserInfo....
  • 22.3k
9 votes
Accepted

Testing multiple asynchronous processes

Test.startTest does not wait for any asynchronous code to run. Test.stopTest can test multiple asynchronous code calls within reason (e.g. a scheduled class that calls a batch class), but there are ...
  • 445k
9 votes
Accepted

System.AsyncException: Maximum callout depth has been reached

There is an Idea out there called Allow callouts from chained queueable Apex jobs. Here's the description, with some minor reformatting: Allow callouts from chained queueable Apex jobs Currently (...
  • 144k
9 votes
Accepted

Error "Too many queueable jobs added to the queue: 2"

You have two things going on here: You can only execute one job from a job. Triggers fire in chunks of 200 records. So if you update 201 records, you will have one Queueable kicked off with 200 ...
  • 144k
9 votes
Accepted

PB + Queueable System.FinalException: Record is read-only

I suspect that you've unwittingly copied the "isReadOnly" flag on the sObject records. This flag, which can't be accessed or modified directly, is responsible for causing the FinalException that you'...
  • 445k
9 votes
Accepted

In an unit test for an LWC, how can I prove an async chain is setting a value in a lightning-formatted-number?

Figured out a way to flush the promises by applying answer from https://stackoverflow.com/questions/44741102/how-to-make-jest-wait-for-all-asynchronous-code-to-finish-execution-before-expec ...
8 votes
Accepted

Testing scheduled apex containing @future method?

Sorry for not answering this back when I figured it out. I don't remember exactly, but I believe it was due to a built in limit in testing only one 'level' of asynchronous call. The test environment ...
  • 3,532
8 votes
Accepted

Do outbound messages block trigger?

Outbound messages are executed asynchronously. When a workflow rule causes an outbound message to fire, it's simply placed into a queue for later execution. A future method would fire before an ...
  • 445k
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 ...
  • 131k
8 votes
Accepted

What's the difference between AsyncApexJob and CronTrigger?

CronTrigger is the parent job that will be run; it can have many runs attached to it. AsyncApexJob is a single job, including batchable, queueable, and future methods. So, if you want to know what's ...
  • 445k
8 votes
Accepted

Why do developers set 'this' to a variable (e.g. 'self' or 'that')?

this is a fickle creature when you're talking about normal functions in JavaScript, because it changes based on context. For example: let value = { src: "hello", output: function() { ...
  • 445k
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 ...
  • 445k
7 votes

How can I cause side effects outside an execution context?

The way we have resolved (or worked around) this in our package is basically by taking transaction control away from the platform. First we set a savepoint, then we do the processing, and if an ...
  • 999
7 votes
Accepted

Using @future tag on Apex Webservice but code not executing asynchronously

From the comment thread, separating the future and WebService annotations may help so they each are being used as documented: WebService static void MathStuffAmort(String ID,String str1) { ...
  • 131k
7 votes

How can I cause side effects outside an execution context?

I've been blasting away at this, trying to best apply some of the contributions, notably: JeremyNottingham with the exemplary Database.Stateful pattern, MLucci's technique to 'empty out' the ...
7 votes

How can I cause side effects outside an execution context?

Interesting question with some interesting answers. I think/hope I've understood what you're asking. In some respects, this feels a bit like fighting the platform here, and I guess it'll depend on how ...

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