You have a class that extends RunBase using the standard dialog for entering parameters. On that standard dialog form you add a button calling a menu item. This works well for starting reports or triggering some static behavior, but what if you need to pass a parameter to the target of that button? Several other blogs have covered adding a button to a RunBase dialog, but in this post we’ll dig a bit deeper to show how a parameter can be passed and captured on the target object using accessor methods.
From a high level, this problem seems like it should be easy enough. However, there are a couple of tricks involved which we’re hoping to expose. Let’s start with the following example: We’ve developed a class for validating file permissions and programmed it such that it can run stand-alone using its own dialog. We’ve also made it capable of running silently if given some requisite parameters. Further, when running silently we can gather the needed parameters from the calling object if we can determine the calling class. First we’ll show how to add a button to a RunBase dialog, then we’ll show how to interrogate the parameter from the caller in the target class using an accessor method. Note that the code in this solution was taken from an AX 2009 implementation, the methodology may differ in Dynamics AX 2012.
The key to this solution is that instead of thinking of the caller “pushing” the parameter to the target, we need to implement a method of “pulling” the parameter from the caller.
In our RunBase class (I’m going to refer to this as the caller or calling class in this solution; it’s also named OurReport in the code) we have a pretty standard implementation of the “dialog” and “getFromDialog” methods. In the following snippet, you’ll see the addition of the menu item button highlighted.
Likewise, “GetFromDialog” is pretty straight forward, we’re just setting some local variables from the fields on the dialog.
Now, we need an accessor method for our target class to call and interrogate the caller for a file path. We implemented this as a “parm” method to follow AX naming convention. A couple of things to notice with this method:
Now we can take a look at the main method on the target class.
The “pull” accessor method parmFilePath is nothing new, but it’s being used here in a non-standard way. Typically parm methods are used on the target class, not on the source, but we need a way to pull the file path value on the dialog in the source class into the validation method on the target class.
Second point of interest is that this main method contains two distinct code paths – one in which no arguments are issued, and one with a caller. First is the path using the caller method on args. In order to ensure that this code path is only applied when the target class is called from our report, we needed to test whether the caller’s RunBase class is OurReport.
In AX 2012 we would simply use the “Is” and “As” operators to test the caller and downcast it appropriately (as explained on MSDN here: http://msdn.microsoft.com/en-us/library/gg843452.aspx). In AX 2009 and AX 4 the code looks like the following:
This code allows us to compare the argument’s caller instance with OurReport, and then assign a local object of OurReport to the caller instance. With a local copy of OurReport, we can use the parmFilePath accessor method to retrieve the file path from the caller’s dialog.
We hope this methodology and code sample will be useful for our fellow developers. Check back soon for more!
Modernize your D365 multi-entity accounting processes with Avantiico’s Multi-Company Solution for Microsoft Dynamics 365 Finance. Introduction Efficient financial multi-entity accounting is crucial
Discover the benefits and use cases of the Avantiico MCS Super Journal function for faster, simplified multi-company journal imports in Microsoft Dynamics
Optimize your financial management with D365 Payment Schedules. This blog explains how to simplify AP and AR processes, enhance cash flow tracking,
Discover how Avantiico helps you improve business processes, provide customers with a seamless experience and transform the way you do business.