You need to sign in to do that
Don't have an account?
Mayur Naidu 17
Can a database method invoke Apex Trigger?
Hello, I wanted to know if we use a Database.update() method in an Apex class for an sObject, does this update invoke 'before update' Apex trigger on that sObject?
Thanks for you help.
Thanks for you help.
If it help you please mark this answer as correct, it may help others.
yes, it can be done.
For your reference, you can check the below articles,
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_syntax.htm
https://mindmajix.com/salesforce/how-to-create-sample-apex-trigger-in-salesforce
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks!
I agree with the two before me. I just wanted to add that this is how we test triggers with our test classes and methods - we create test data and insert it (and update it if necessary) because we know it will fire the triggers.
From the 6th paragraph (actually it's the entire paragraph) from the first page of the Triggers section of the Apex Developer Handbook (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers.htm?search_text=triggers):
"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. See Execution Governors and Limits (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm)."
Also, you can check around the "interwebs" where folks have come up with ways to ensure that a trigger fires once and only once in certain circumstances/use cases.