function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Christian Schwabe (x)Christian Schwabe (x) 

Asynchronous Apex Triggers vs. @future-Annotation

Hi everybody

I read the article "Get #Buildspiration with Asynchronous Apex Triggers in Summer ‘19" (https://developer.salesforce.com/blogs/2019/06/get-buildspiration-with-asynchronous-apex-triggers-in-summer-19.html) a few days ago a had to think about the question "What is the difference between event-driven asynchronous apex trigger vs. @future-Annotation".

I can't think of an academically 100% correct answer right now, because both do the same: They outsource very limit-intensive (related to execution governors and limit) out of the current apex-transaction.

But, why and when should I use @future-Annotation and when event-driven asynchronous apex trigger?

I hope we can discuss this topic together and find a solution to understand vor everyone.


Best regards,
Christian

Tad Aalgaard 3Tad Aalgaard 3
Quoting from the article.
Asynchronous Apex triggers are change event triggers that run asynchronously after a database transaction is completed.
So, from what I am understanding is that an Asynchronous Apex triggers occur after the database transaction.  While the standard @future method is still part of the overall execution context and will not finish it's commits if an error occurs during the processing of the code that fired it throws and uncaught exception.
 
Alain CabonAlain Cabon
Hi Christian,

The former trigger was always synchronous itself but now you can have this primary trigger also asynchronous. 

The difference is subtle when you use few lines of code in the synchronous trigger before calling a future method (inside the synchronous trigger).

"Perform resource-intensive business logic asynchronously in the change event trigger and keep transaction-based logic in the Apex object trigger. By decoupling the processing of changes, change event triggers can help reduce transaction processing time." (Salesforce)

The synchronous Apex object trigger is still needed for the validation of a form for example (transaction-based logic). 

The asynchronous Apex event trigger (always after insert) is used to initiate an asynchronous treatment from the trigger itself even if you use @future finally again like  below.

React to Changes with Apex Triggers :
trigger LowOnStock on Products__ChangeEvent (after insert) {
    // Get the change event’s product ID for added and updated products
    Set<Id> productIds = new Set<Id>();
    for (Products__ChangeEvent event: Trigger.new) {
        if (event.ChangeEventHeader.getChangeType() != 'DELETE') {
            String productId = event.ChangeEventHeader.getRecordIds()[0];
            productIds.add(productId);
        }
    }
    if (productIds.size() > 0) {
        ProductNotifications.notifyOnLowOnStockProductUpdates(productIds);
    }
}

Future method:  ProductNotifications.notifyOnLowOnStockProductUpdates
public class ProductNotifications {    
 
    @future (callout=true)
    public static void notifyOnLowOnStockProductUpdates(Set<Id> productIds) {
        // Look up the current stock threshold to filter the products with
        Integer productStockThreshold = ...;
        List<Products__x> lowOnStockProducts =
            getProductsLowOnStock(productIds, productStockThreshold);
        // Notify subscribers
        notifySubscribers(lowOnStockProducts,
            ‘These items are running out of stock. Act soon!’);
    }
   ...
}

https://help.salesforce.com/articleView?id=external_object_change_tracking_example_create_triggers.htm&type=5 (https://help.salesforce.com/articleView?id=external_object_change_tracking_example_create_triggers.htm&type=5)

They are complementary.