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
Rohit Vaidya 3Rohit Vaidya 3 

trigger from batch

Can we call a trigger from Batch class and batch class from trigger? if yes then what are the limitations.
Andrew GAndrew G
Technically, you don't call Triggers.  You cause a DML event which can fire a trigger.

As for Triggers calling Batches, you can but it is fraught with danger.  Biggest issue is ensuring that the Trigger does not cause the batch to exceed its' limits.

Reference (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm)
Use extreme caution if you’re planning to invoke a batch job from a trigger. You must be able to guarantee that the trigger doesn’t add more batch jobs than the limit. In particular, consider API bulk updates, import wizards, mass record changes through the user interface, and all cases where more than one record can be updated at a time.
 
Arun Kumar 1141Arun Kumar 1141
Hello Rohit,

Yes, it is possible to indirectly call a trigger from a batch class or vice versa in Salesforce, but it is generally not recommended due to design considerations and best practices.

Calling a trigger from a batch class can be achieved by updating records within the batch class that would normally trigger the trigger's execution. This approach indirectly triggers the logic defined in the trigger. However, it is important to carefully evaluate the impact and potential recursion that may occur when invoking triggers within a batch context. Recursion can lead to unexpected behavior and performance issues.

Similarly, invoking a batch class from a trigger is possible by leveraging features like the Queueable interface or future methods. In this scenario, the trigger would enqueue a job to execute the batch class asynchronously.

However, it is crucial to exercise caution and consider the following limitations and best practices:
  1. Recursion: Trigger recursion can occur when triggers call each other or when a batch class updates records that trigger additional triggers. It is essential to implement proper recursion control mechanisms to prevent infinite loops and ensure efficient execution.
  2. Governor Limits: Both triggers and batch classes are subject to Salesforce governor limits. Care must be taken to optimize code, handle large data volumes efficiently, and stay within the limits to prevent exceptions or failures.
  3. Complexity and Maintainability: Mixing the logic of triggers and batch classes can lead to complex code and make it harder to maintain and debug. It is advisable to separate concerns and follow a modular design approach for better code organization and readability.
  4. Bulk Data Considerations: Triggers and batch classes operate on different data volumes. Triggers typically handle individual records, while batch classes are designed for bulk data processing. Care should be taken to ensure that the logic within triggers and batch classes can handle both scenarios appropriately.
Overall, it is recommended to evaluate the specific use case and explore alternative design patterns, such as using helper classes or externalizing common functionality, to maintain code clarity and avoid unintended side effects.

Please mark the above answer as best answer if this is helpful for you.
Thanks.
SubratSubrat (Salesforce Developers) 
Hello Rohit ,

Yes it is possible, we can call a batch apex from trigger but we should always keep in mind that we should not call batch apex from trigger each time as this will exceeds the governor limit this is because of the reason that we can only have 5 apex jobs queued or executing at a time.

How to call Batch apex from trigger -> https://www.sfdc-lightning.com/2019/10/how-to-call-batch-apex-class-from-apex.html#:~:text=Yes%20it%20is%20possible%2C%20we,or%20executing%20at%20a%20time.

-------------------------------------------------------------------------------------------------

Regarding your second query , There is no specific place from where you call a trigger. Trigger code automatically executes when certain events such as insert, update, delete etc occur. The source for such events could be anything.

For eg - Button click on visualforce page or on detail page of a record causing an insert/update/delete
An apex code with DML statements(this could be a regular Apex class, batch class, or even some other trigger)
Workflow field updates...

A trigger is Apex code that executes before or after the following types of operations: insert update delete merge upsert undelete.

Triggers doc -> https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers.htm

If this helps , please mark this as best answer.
Thank you.