Execution contexts
In this chapter, we are focusing on understanding the execution contexts of code written in Apex. In later chapters, we will review how Salesforce Heroku and Salesforce Functions can be used to run other computational workloads written in Java and Node.js.
An Apex execution context on the platform always has a beginning and an end; it starts with a user or system action, for example, a button click from a Lightning Web Component (LWC), an “Apex-scheduled” background job, or responding to a Platform Event. In all cases, execution is typically short-lived, with seconds or minutes instead of hours before it ends – Apex does not run continuously. This behavior is especially important in multitenant architecture because each context receives its own set of limits around queries, database operations, logs, and the duration of the execution.
In the case of background jobs (Batch Apex), instead of having one execution context for the whole job...