GeezerVison is showing github.com — [apex] New Rule: Avoid Future Annotation · Issue #6203 · pmd/pmd
[apex] New Rule: Avoid Future Annotation #6203DescriptionProposed Rule Name: AvoidFutureAnnotation
Proposed Category: Best Practices
Description:
The @Future annotation is a legacy way to execute asynchronous Apex code in Salesforce. While it still works, it has several limitations that make it less suitable for modern Apex development:
- Methods must be static
- Only primitive data types, primitive arrays, or collections of primitive data types can be parameters
- No way to monitor job status or handle failures
- No support for chaining jobs
The Queueable interface provides a more modern and flexible alternative that addresses these limitations:
- Better error handling and monitoring capabilities (via Transaction Finalizers)
- Support for more complex data types (including custom classes and collections of sObjects)
- Ability to chain jobs together
This rule flags usage of the @Future annotation and suggests implementing the Queueable interface instead.
Code Sample::
// Not recommended: Using @Future annotation
public class FutureExample {
@Future
public static void futureMethod(String accountId) {
// Process account asynchronously
Account acc = [SELECT Id, Name FROM Account WHERE Id = :accountId];
// ... process account
}
}
// Recommended: Using Queueable interface
public class QueueableExample implements Queueable {
private String accountId;
public QueueableExample(String accountId) {
this.accountId = accountId;
}
public void execute(QueueableContext context) {
Account acc = [SELECT Id, Name FROM Account WHERE Id = :accountId];
// ... process account
}
}
// Usage:
// System.enqueueJob(new QueueableExample(accountId));
Possible Properties:
- Should this rule be customizable via properties?
No properties are needed at this time. Reacted by Daniel Ballinger, Piotr Kożuchowski and Szymon Halik ActivityFishOfPrey commented on Nov 4, 2025 Queueable jobs also offer more controls for deduplication of workloads and delayed execution.
We have consistent guidance in the Architecture Center for Tools for Asynchronous Processing with Salesforce for using Queueables in preference to future methods. changed the title [-][apex] Avoid Future Annotation[/-] [+][apex] New Rule: Avoid Future Annotation[/+] on Nov 5, 2025Bukama commented on Nov 17, 2025 Please be aware there are several annotations which are called @Future, for example Jakarta EE's @Future which has nothing to do with asynchronous execution, but with validation. mitchspano commented on Nov 17, 2025 ContributorAuthor More actions @Bukama this rule is for Apex, not Java. Reacted by Matthias Bünger jtferns commented on Jan 8, 2026 🙋 @mitchspano we just stumbled into this on recent code-analyzer runs, and have been wondering what's the best way to address Mixed DML scenarios in Apex test setups; the docs still suggests using @future; should it suggest Queueable instead?
(I scoured a few other SF repos to figure out where to ask/drop an issue, but couldn't find an obvious one; this is definitely not an issue with pmd, and is more relevant for Apex/testing; happy to be redirected elsewhere) mitchspano commented on Jan 14, 2026 ContributorAuthor More actions @jtferns Yes, Queueable should be used instead of @Future.
Thanks for sharing the reference to the docs, I will share with our document team and get that updated.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|