When working in forms and using display methods, sometimes they have a tendency to slow down the form to the degree that makes you wonder if it’s worth it at all. This is when cacheAddMethod comes in handy.
When using cacheAddMethod we place the method in memory, the effect of this is that the method only runs once when the form loads and then every time we switch between records. This instead of display methods normal behavior, which means it runs every time all the time.
For the cacheAddMethod to do what it’s supposed to we need it to run when the form starts up. If we have created the display method “OurTestMethod” on table SalesTable we want to place the call to cacheAddMethod in the init method on the form datasource SalesTable.
salesTable_ds.cacheAddMethod( tablemethodstr(SalesTable, OurTestMethod)); |
Now the form SalesTable runs much smoother, but it has some effects that we might not want. Let´s say that our method OurTestMethod returns a real and we want OurTestMethod to return 0.00 if a checkbox that is placed under the same tab as the field that uses our dislaymethod, has the value “No”.
This makes it a bit trickier, with the previous code nothing happens when the checkbox is unchecked. This is because the already calculated value is in memory and is not recalculated unless we switch between records so the data, and the our method, is reloaded. We need to trigger our cached method to be recalculated.
This is done like this:
1 2 3 4 5 6 7 | public void modified() { super(); SalesTable_ds.cacheCalculateMethod( tablemethodstr(SalesTable, OurTestMethod)); } |
We add a modified method on the checkbox that says that when someone clicks it, our display method OurTestMethod is recalculated but still in memory. This is done with the datasource method cacheCalculateMethod.
Last 5 posts in Development
- No valid document identified from the entity key - April 9th, 2010
- Using Regular Expressions in Dynamics AX - October 2nd, 2009
- Create class and methods in x++ - December 22nd, 2008
- Simple field lookup in form - October 13th, 2008
- Get your Intercompany CustAccount - September 29th, 2008
it´s good job
Great!! I liked this explanation!