Menu

Passing Parameters to a Button on a Dialog

Avantiico Support Team

The Microsoft Dynamics AX/D365 Support Team at Avantiico is focused on solving our client’s problems, from daily issues to large and more complex problems. Cindy is a true AX veteran and hold 20 years dedicated Senior Consulting experiences for Microsoft Dynamics AX Software.

See All Posts

Scenario​

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.

Solution

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.

Resolution

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.

code

Likewise, “GetFromDialog” is pretty straight forward, we’re just setting some local variables from the fields on the dialog.

code

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:

    • It must be public. This should be self-explanatory, but the target object will be using this method, so we need to be sure that it is public.
    • The dialog on our caller class is taking a parameter which includes the file path, file name, and extension. So, we’ll need to break that apart and return only the file path for our particular code example to work.
code

Now we can take a look at the main method on the target class.

code

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.

code

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:

code

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!

Share on facebook
Facebook
Share on google
Google+
Share on twitter
Twitter
Share on linkedin
LinkedIn

More to explorer