What is an Apex Transaction

Author: Bruce Tollefson Published: May 27, 2022; Modified: May 27, 2022
lightning_bolts

In Salesforce an Apex transaction refers to the entirety of operations that are executed within a transaction boundary. The transaction boundary refers to everything that occurs within that transaction. In other words the Apex transactions is all the related code that fires due to something happening within the org.

As an example let’s say you have a record trigger flow on opportunities, a trigger on accounts, and a record trigger flow on contacts. The opportunity record trigger flow fires when the stage is changed to won and populates a date field on the account of the last opportunity won for the account. The account trigger has a before trigger that updates another field on the account and an after trigger that queries the opportunity finds that primary contact on the opportunity for the account and populates a field on that contact. The Contact trigger has a before trigger that updates another field on the contact record, like so:

Apex Transaction Sequence

Every time an opportunity is won this whole sequence of events fires. This is all included in an apex transaction.

As another example you create a Lightning Web Component that does a callout to an external system to pull back some information. Then once the user views that data they then click another button to update the opportunity to the won stage previously described. This would results in 2 apex transactions 1 for the LWC callout which is the full transaction and another for the opportunity won update. This is 2 transactions because the LWC makes the callout and then is done. If the LWC automatically set the stage and did the external callout then that would only be 1 apex transaction.

This is important to know as the governor limits are per transaction and the boundaries of that transaction. If you have a lot of cascading operations this could have potential to hit those limits if the code written is not bulkified.

Leave a Reply

Your email address will not be published. Required fields are marked *