Development questions
Can you call a batch class from an Apex trigger? If yes, what are the precautions?
Yes, via Database.executeBatch(), but with care:
- Limits: only 5 batch jobs can be queued or active at once (extra jobs go to the Flex Queue, up to 100 holding). A trigger firing per 200-record chunk can flood the queue.
- Context: you cannot call
executeBatchfrom a future method, and a batch can only start one batch, from itsfinish(). - Volume: one batch per trigger invocation, not per record. Collect Ids and launch once.
- Recursion: avoid the batch updating the same object and re-triggering infinite batches.
- Check
System.isBatch()/System.isFuture()before enqueuing.
Consider Queueable Apex for lighter work.
apex-triggertriggerasyncbatch
More Development questions
- What is an Apex Trigger in Salesforce and when should you use it over Flows?
- What is the difference between before triggers and after triggers?
- Explain all Trigger context variables (isInsert, isUpdate, isDelete, isUndelete, isBefore, isAfter, isExecuting, size, operationType).
- What is the difference between Trigger.new and Trigger.old?
- In which trigger events are Trigger.new and Trigger.newMap available?
- In which trigger events are Trigger.old and Trigger.oldMap available?