+ Start a Discussion

Trigger and workflow rule kept updating in a loop and reached SOQL limit

Trigger and workflow rule kept updating in a loop and reached SOQL limit. Introducing global static var but lead to test failure.



The problem is the workflow rule to update a field on the record is fired right after the execution of a update trigger. And this will result in another firing of the trigger.  And the initial value of the record in the second firing of trigger is the original value instead of the updated value after first firing.


We used to do one more database query to determine whether the update/insert in the trigger should take effect, but this will increase the SOQL queries. This will only prevent the record to be updated/inserted multiple times, but the trigger will still be fired more than twice, and the number of SOQL queries will be doubled even trippled.


Then  I have posted on the board,and thanks for Wilmer to give me the solution:



The global static variable does help, and it works fine without increasing SOQL queries. But the now the problem is, the test method can not pass. In the test we do multiple update, the insert/update database action in the trigger will only execute the first time, and the assertion for the following update all failed.


It seems the global static variable won't be refreshed after each DML.


Now I am really in a dilemma: how to decrease the number of SOQL while ensuring the test coverage.


Any suggestions? 


Thanks in advance.