How To Call A Form From Another Form

Share this:

There are two common ways to call a form from another form within Dynamics 365 for Finance and Operations. You can use a menu item button. Or you can use use X++ code to call another form. In this article I will show you how to use an Menu Item Button. And how the system can pass the currently selected record into the calling form.

Understanding how to call a form in from another form will allow you to create more interactive forms. Forms that provide a better user experience.

Call A Form Using A Menu Item Button

The easiest way to call a form from another form is to use a menu item button. For example, let’s pretend you have a form with a grid. And you want the user to be able to select a record from the grid, then click a button, and open a related form. This can be done very easily using a menu item button.

For instance, consider this very common need for a Menu Item Button. I have a form named Model. The form lets users enter combinations of make and model of a car. For example: Honda Accord, or Toyota Camry, or Ford Explorer. These are all examples of make and model combinations.

On my form, the user needs to enter the Make and Model into columns on a grid. The Model is a value that the user will enter in as a string. However, the ‘Make’ is a lookup, that shows values stored in the ‘Make’ table. Values in the ‘Make’ table are entered using the ‘Make’ form.

It would be very helpful to the end user to provide a Menu Item Button that allows them to open the ‘Make’ form, right from this form. That way, they don’t have to try to find the name of this form by looking through all of the menu items in the system.

Add A Menu Item Button

To recap, we need to call a form from another form. Specifically, we need to call to the Make form from the Model form.

To do this, first create a Finance and Operations solution in Visual Studio. Set the model on the project to be the model you wish to work in. Then, either add the Model form to your project, or create an extension of the form, and add it to your project.

Next, open the Model form by right clicking on the form node and selecting ‘Open’. Or by double clicking on the form node.

The form designer will show in the right pane. Locate the ‘Action Pane’ control. Create an Action Pane control if one does not already exist on your form.

In order to create a Menu Item Button, we first need to create a Button Group. Right click on the Action Pane control and select New>Button Group.

Next, right click on the Button Group that was created, and select New>Menu Item Button.

This will create a Menu Item Button.

Set The Menu Item Properties

In order for us to call a form from another form using the button item button, we need set the properties on the menu item button. Otherwise it will not do anything.

First, right click on the Menu Item Button, and select ‘Properties’ to open the Properties Window.

In the Properties windows, there are several properties that we need to set in most conditions.

First, locate the Name properties, and change the default name to something that better describes your Menu Item Button. I renamed my button to FormMenuIFunctionButtonMake.

Secondly, locate the Menu Item Type and Menu Item Name properties. In this example, we want to call the Make form. In order to do that, we need to first know the Menu Item that opens that form. The Menu Item is named ‘rsmMake’ and it is a Display menu item type. By convention, any menu item that opens a form should be of type ‘Display’. Set the Menu Item Type to ‘Display’. Set the Menu Item Name to rsmMake.

Thirdly, locate the Text property. The value of this property will display on the button that is shown on the form. Set this to the text ‘Make’.

Fourthly, it is a good idea to set the Help Text property. Set this to “Pushing this button will open the make form.” Or whatever you would like.

Pushing this new button on the Model form will now open the ‘Make’ button form. This allows the user to add new values to the ‘Make’ form. Then close out of the form. And continue entering in records on the ‘Model’ form.

Passing In A Record To The Calling Form

Even though the Menu Item Button that we added in the previous section works, there is still one thing we can add to improve the functionality. Specifically, Menu Item Buttons allow for developers to send the currently selected record on the current form, into the calling form.

Right now, when a user clicks the ‘Make’ button, the system will open the Make form. And the user will see all of the records in that form. That is fine. However, it would provide an even better user experience if the calling form were to already be filtered to the record that contains the ‘Make’ that the user was selecting on the previous form. This way, a user could immediately make any edits to the record, without having to locate it first.

In order to make this functionality work, a developer needs to set the ‘Data Source’ property on the Menu Item. You must choose from one of the Data Sources that is on the current form. In our example, set the ‘Data Source’ property on the Menu Item Button to ‘rsmModel’. This is the only Data Source on the form.

This will cause the currently selected ‘rsmModel’ record to be sent to the calling form.

Filtering The Calling Form

If the calling form contains the exact same Data Source, then there is nothing more that you need to do. The calling form will automatically recognize the passed in record, and filter the Data Source on the form to only show the passed in record.

However, in our example, that is not the case. We are passing in a record of type rsmModel. But the record on the calling form uses the Data Source rsmMake. That said, there still exists a relationship between the rsmModel table and the rsmMake table. And because there is a relationship, the system is able to figure this out and filter to the right record.

Most of the time, if you have a relationship between the two tables, the system will do the work for you. If this was not the case, this functionality can still be achieved by overriding the Init method.

Overriding the Init Method

Open the rsmMake form in Visual Studio. In the form designer, locate and expand the ‘Methods’ node under the main form node. If there already exists an Init method you can double click on it to open. If there is not, follow the next steps to override the Init method.

Right click on the ‘Methods’ node and select Override>init

Add the following code to the Init Method.

public void init()
    {
        rsmModel rsmModel;

        super();

        if (element.args())
        {
            if (element.args().record()
                && element.args().record().TableID == tableNum(rsmModel))
            {
                rsmModel = element.args().record();
            }
        }
    }

Explaining The Code

The code validates that the form has Args() set. The Args object is automatically set by the system when you set the Data Source on the Menu Item Button.

After validating that Args exists, the code validates that a record was passed in the Args. Next, the code checks to see what type of table records is passed in, by comparing the tableID on the record to the tableID that gets returned from the global function ‘tableNum’. Finally we can store the generic table record that was passed in, into a table buffer of the correct type. This then allows us to use intellisense within Visual Studio.

Now that we have the passed in record, we can use this information. We could override the executeQuery method on the Data Source and filter based on the information passed in. Or we could do other things, such as hide or disable controls.

Conclusion

In this article, I showed you how to easily use a Menu Item Button to call a form from another form. I also showed you how to pass the currently selected record into the calling form. And how the system can automatically filter the calling form. I also showed you how to can access the passed in record in x++ code. And use the information to control the form.

Learning how to call a form from another form, allows you to quickly and easily connect the user with the information that they need. Then come back to a form and continue. I am hope you learned something new today.

Peter Ramer
Peter Ramer is a part of the Managed Application Services team at RSM working on Microsoft Dynamics 365. He focuses on the Retail and Commerce industries. When he is not solving problems and finding ways to accelerate his clients' business, he enjoys time with his three kids and amazing wife.

Share this:

2 thoughts on “How To Call A Form From Another Form

Add yours

Leave a Reply

Your email address will not be published. Required fields are marked *

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑