0

What is the difference between using the Async Apex trigger and @future methods? Why use Async Apex Trigger when we have @future method to separate the thread from the main Trigger?

The only difference I can think of is that CDC could be utilised as a PUB/SUB event.


ChangeEvent Triggers (Async Apex Trigger)

trigger AccountCDCTrigger on AccountChangeEvent (after insert) {}

enter image description here

1 Answer 1

2

CDC and future methods address quite different concerns. Note, however, that CDC can be seen as a flavour of Platform Event, and if you bring that into the question you get a much more interesting set of constraints and approaches.

Up to 50 future method invocations can be initiated from a synchronous transaction, while only 1 can be generated from an async transaction, and zero from a future method.

You can perform DML on up to 10000 records in a given transaction, with a trigger called with up to 200 records at a time. You can also perform up to 150 DMLs in a transaction. And that transaction could itself already be async. If your trigger logic tries to use a future method, it will break in all sorts of scenarios because you will exceed the number you can enqueue (e.g. you update 201 records in an async transaction - this will call your trigger twice, which will try to initiate a future each time, exceeding the async limit of 1).

I suggest you read this article on why not to use future from a trigger, and how you can efficiently use platform events instead. The article has a link to sample code, but there is also this github repo with a slightly different approach.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .