Hello,
I have the following question:
I am using the following hooks:
- before save
- after save
- before relationship add
- after relationship add
- before relationship delete
- after relationship delete
What i want to confirm with you is the order of execution of these above hooks.
I understand that inside a hook it is used the sort order: the lower the number the sooner the hook it is fired. But i am not very clear what is the order of execution of the different hooks.
Regards,
Mihai
Your question does not have a single answer, it really depends on the case, and some cases are complex.
Some logic hooks save data and that causes other logic hooks to be run from inside the original hook.
Sometimes when you are saving data it saves both a bean and a relationship bean; other times it’s simpler.
So, instead of finding the generic answer, I recommend studying the specific case that you are facing. For example, create a couple of hooks and set breakpoints inside the hooks to see in your IDE when the breakpoints are getting hit.
I agree with @pgr it can be complex when you have multiple things going on. Trial and error is what I do most. However, as “general” rule in my exprience the “save” stuff comes first and the “relationship” stuff comes last.
I know I run into trouble trying to calculate something based on a new relationship that is not created yet. Even in workflow this is an issue. I’ll normally break it up into two transaction when this occurs, one that creates/saves the record on save, and one that runs in the scheduler that handles calculations and queries based on the relationship.
1 Like