This week I started to transform some OnAfter and OnBefore Hooks on tables to Events.
When performing these manuel steps I first though about Upgradeability. Every modification must be upgraded smoothly without any manual handling afterwards.
So what will the result be if I upgrade a customer with the Hook triggers? I tested with a simple example.
I have a Sales Line Hook codeunit with the following code:
First I did the following modifications to my Hook Codeunit to make it an “Event”:
- Make the function “Local”, so that it can only be called once.
- Then configure the Subscriber, with table no. “37” and event trigger “OnAfterInsert”
- I also need to replace the “SalesLine” with “Rec” in my code
And finally I add the following code as the first line to have the same behavior as the table trigger
- “IF NOT RunTrigger THEN EXIT;”
My result will look like this.
I also need to remove the reference in code on the table itselfs.
Now that everything is in place, I will do an upgrade.
After upgrading some customer my Delta will look like this. And as you can see there are a lot of modifications.
My first reaction was Ok, NAV has found the correct modification.
But when I saw the result, it wasn’t what I have expected.
All the code is marked as comment and must be fixed manually. Pfff, no good. Too much manual work.
So I need to make it more usefull and had a briljant idea. Instead of replace the “SalesLine” variable with “Rec” in my code, I can change the declaration of the parameters itselfs. And my Delta will look like this.
Instead of all lines are modified only one line is added. Nice.
And my result is also as I expected. Clean code with all my modifications and no inline comment that need to be fixed.
I can import this changes and compile everything. Until now this is what I wanted.
But my briljant idea isn’t that briljant I thought it would be. When I test my code my triggers wheren’t fired.
Strange, because everything compiles without an error.
When I check the “Event Subscriptions” I found the problem. There is a “ParameterMismatch” error with my subscription. Damn!
The only thing that helps is changing my “SalesLine” parameter back to “Rec”. Grrrr, how pitty is this.
Is this build in hardcoded?
I hope that I can be fixed by Microsoft in an CU update and therefore I have created a suggestion on Connect. So please vote on it, if you will have this fixed too.
For now I’m stuck with my briljant idea. The only quick fix I found was to add 2 extra lines but that will not totally result in the same result.
When creating a new Delta and applying it, my result is better then my first attem but still not that good as the second as you can see.
Let’s hope and vote for the best !!!!