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
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 ...
user avatar
21 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 ...
user avatar
  • 40.4k
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

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 ...
user avatar
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'...
user avatar
  • 6,978
14 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 ...
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

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 ...
user avatar
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 ...
user avatar
  • 143k
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
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 '...
user avatar
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

What does the Batch Apex "Holding" status mean and where is it documented?

Based on the Summer '14 Release Notes the holding status appears to be associated with the new Apex Flex Queue feature. The Apex Flex Queue pilot enables you to submit batch jobs beyond the allowed ...
user avatar
  • 13.3k
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....
user avatar
  • 22.2k
9 votes

Can a DML retry solve UNABLE_TO_LOCK_ROW race conditions?

If you use the FOR UPDATE keyword in your query, the database will wait up to about 5 seconds before declaring that it cannot acquire the lock (this should normally be plenty of time). This event can ...
user avatar
  • 434k
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 ...
user avatar
  • 434k
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 (...
user avatar
  • 143k
8 votes
Accepted

How can I get the details of last schedulable job and of which class? Is there any way?

Query for the AsyncApexJob Object. Something like this will get you started AsyncApexJob myJob = [SELECT Id, Status, ApexClass.Name, FROM AsyncApexJob ...
user avatar
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 ...
user avatar
  • 3,472
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 ...
user avatar
  • 434k
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 ...
user avatar
  • 130k
8 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 ...
user avatar
  • 5,478
8 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'...
user avatar
  • 434k
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 ...
user avatar
  • 434k
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() { ...
user avatar
  • 434k
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 ...
user avatar
  • 434k
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 ...
user avatar
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 ...
user avatar
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 ...
user avatar
  • 999

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