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
SakthidasanSakthidasan 

Apex trigger consideration

I'm learing trigger concept throuth apex documentation.but I can't understand follwoing point.please explain 
​Triggers can also modify other records of the same type as the records that initially fired the trigger. For example, if a trigger fires after
an update of contact A, the trigger can also modify contacts B, C, and D. Because triggers can cause other records to change, and
because these changes can, in turn, fire more triggers, the Apex runtime engine considers all such operations a single unit of work and
sets limits on the number of operations that can be performed to prevent infinite recursion
Best Answer chosen by Sakthidasan
Mahesh DMahesh D
Hi Sakthidasan,

Pleas find the realtime scenario where there is a chance of recursive trigger occurrs.

We have a Location object with Active as a field which will maintain whether the Location is a Active or not.


Scenario 1:

If the user is inserting a new Location with Active flag is true, it will check all the existing active Location with the matching name and try to de-activate them.

Scenario 2:
If the user is update the existing Location with Active flag is true, it will check all the existing active Location with the matching name and try to de-activate them.

Here we have to write a Trigger on the Location object with after insert, after update as a events.

Possible issues:

If we are not adding the conditions properly then there is a chance of getting into recrussive calls. Hence we have to follow below steps:

(1) Make sure that all the possible conditions are added properly so that it will not recursively.

(2) At times we may need to use Static variable to control the recursive calls.


Note: This is one of the possible reasons and there may be multiple other reasons where the recursive trigger execution happens.

Here is another relatime scenario for you:

Whenever the user updates an Account, also update the corresponding Contacts. At the same time, if user is touching the Contacts, update the Accounts.


Here also highly possiblity of recurssive trigger execution happens.

Note: I could have copied lot of best practices but, explaining with the realtime scenarios will help you understand in a better way, hence providng all these points. But don't forget to follow all the possible best practices whenever you are writting Apex Code / Visualforce Page.


Please do let me know if it helps you.

Regards,
Mahesh

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
how to stop recursive trigger in salesforce

Problem :-  
1) Many Developers face recursive trigger , or recursive update trigger. For example in 'after update' trigger, Developer is performing update operation and this lead to recursive call.

2) You want to write a trigger that creates a new record ; however, that record may then cause another trigger to fire, which in turn causes another to fire, and so on. 
  
Solution :-
you can create a class with a static Boolean variable with default value true. In the trigger, before executing your code keep a check that the variable is true or not. Once you check make the variable false.
http://amitsalesforce.blogspot.in/2015/03/how-to-stop-recursive-trigger-in.html

............................................

Trigger Best Practices | Sample Trigger Example | Implementing Trigger Framework
http://amitsalesforce.blogspot.com/2015/06/trigger-best-practices-sample-trigger.html

1) One Trigger Per Object
A single Apex Trigger is all you need for one particular object. If you develop multiple Triggers for a single object, you have no way of controlling the order of execution if those Triggers can run in the same contexts

2) Logic-less Triggers
If you write methods in your Triggers, those can’t be exposed for test purposes. You also can’t expose logic to be re-used anywhere else in your org. 

3) Context-Specific Handler Methods
Create context-specific handler methods in Trigger handlers

4) Bulkify your Code
Bulkifying Apex code refers to the concept of making sure the code properly handles more than one record at a time.

5) Avoid SOQL Queries or DML statements inside FOR Loops
An individual Apex request gets a maximum of 100 SOQL queries before exceeding that governor limit. So if this trigger is invoked by a batch of more than 100 Account records, the governor limit will throw a runtime exception

6) Using Collections, Streamlining Queries, and Efficient For Loops
It is important to use Apex Collections to efficiently query data and store the data in memory. A combination of using collections and streamlining SOQL queries can substantially help writing efficient Apex code and avoid governor limits

7) Querying Large Data Sets
The total number of records that can be returned by SOQL queries in a request is 50,000. If returning a large set of queries causes you to exceed your heap limit, then a SOQL query for loop must be used instead. It can process multiple batches of records through the use of internal calls to query and queryMore

8) Use @future Appropriately
It is critical to write your Apex code to efficiently handle bulk or many records at a time. This is also true for asynchronous Apex methods (those annotated with the @future keyword). The differences between synchronous and asynchronous Apex can be found

9) Avoid Hardcoding IDs
When deploying Apex code between sandbox and production environments, or installing Force.com AppExchange packages, it is essential to avoid hardcoding IDs in the Apex code. By doing so, if the record IDs change between environments, the logic can dynamically identify the proper data to operate against and not fail

Let us know if this will help you

Thanks
Amit Chaudhary
Mahesh DMahesh D
Hi Sakthidasan,

Pleas find the realtime scenario where there is a chance of recursive trigger occurrs.

We have a Location object with Active as a field which will maintain whether the Location is a Active or not.


Scenario 1:

If the user is inserting a new Location with Active flag is true, it will check all the existing active Location with the matching name and try to de-activate them.

Scenario 2:
If the user is update the existing Location with Active flag is true, it will check all the existing active Location with the matching name and try to de-activate them.

Here we have to write a Trigger on the Location object with after insert, after update as a events.

Possible issues:

If we are not adding the conditions properly then there is a chance of getting into recrussive calls. Hence we have to follow below steps:

(1) Make sure that all the possible conditions are added properly so that it will not recursively.

(2) At times we may need to use Static variable to control the recursive calls.


Note: This is one of the possible reasons and there may be multiple other reasons where the recursive trigger execution happens.

Here is another relatime scenario for you:

Whenever the user updates an Account, also update the corresponding Contacts. At the same time, if user is touching the Contacts, update the Accounts.


Here also highly possiblity of recurssive trigger execution happens.

Note: I could have copied lot of best practices but, explaining with the realtime scenarios will help you understand in a better way, hence providng all these points. But don't forget to follow all the possible best practices whenever you are writting Apex Code / Visualforce Page.


Please do let me know if it helps you.

Regards,
Mahesh

 
This was selected as the best answer