A form can be re-used for several different purposes, yet still look very different. Developers can send parameters to forms in D365 F&O to control what data and controls are shown on the form. This creates a better experience for the user. Allowing them to focus on only what is needed for the specific purpose.
In this article I will show you how you can send a string value into a form. Then control the form based on that string value.
Ways To Send Parameters to Forms
There are a few different ways of sending in data to a form. However, all of them use a global class named Args. The Args class contains several properties that can be populated with information. This information can then be read from inside the form you are calling.
There are two main ways that the Args class object is populated.
- The Args class can be populated directly using x++ code.
- Or it can be set by the system by a developer setting properties on a Menu Item.
In the previous article, I showed you how to create a Menu Item, and add it to a Menu.
In this article we are going to cover the second way of populating the Args class object. Which is to use a Menu Item. I will then follow up with a second article that explains how to set the Args object through X++ code.
Key Properties On The Menu Item
Menu Items provide a very straight forward way for developers to send parameters to Forms in D365 F&O. There are several properties on the Menu Items whose values are automatically copied into an instance of the Args class object. Then sent into the calling form.
The two key properties that developers can set on a Menu Item are Parameters, and Enum Parameter. In this article I will focus on how to use ‘Parameters’.
Parameters
Parameters: This property on the Menu Item lets a developer enter in a hard-coded string. This string value is then read in the calling form. When Multiple Menu Items are created, each Parameters property is set to a different value. As a result, this allows the form to behave differently based on what Menu Item was used to call the form. This value is copied to the Args.Parm property.
Send Parameters To Form Example Example
In this article, I will show you an example of how to send parameters to forms in D365. For this example, we will use the Parm Menu Item property.
To begin, create a new solution and project in Visual Studio.
Then, either create a Menu Item or duplicate an existing one.
If you create a new one, set the Object Type to Form. Then set the Object to the name of the form you would like to call. In my case, I set the Object to ‘rsmVehicleServiceWorkbench’ to open a custom form I have.
For this example, duplicate this Menu Item, and rename it rsmVehicleServiceWorkbenchParameter.
Set the ‘Parameters‘ property to the text EnableReadOnly. This is some hard-coded text that I picked. It does not reference anything else. However we will need to remember this exact text so that we can use it in our calling form.
Override The Init Method On The Form
In almost every case where you send parameters to forms in D365, you should override the Init method on the calling form. The Init method is a method called by base system code when the form is first opened. It is responsible for initializing all of the form components. This is the logical and expected place for us to read in the passed in parameters. Then use those parameters to control the behaviour of the form.
The same steps I describe here will work on whatever form you use. I will just use the name of the form I am using in this example.
Add the form rsmVehicleServiceWorkbench to your project. Open the rsmVehicleServiceWorkbench form in the form designer in Visual Studio by double clicking on it. Expand the main form node, and select the ‘Methods‘ node underneath it.
Right click the Methods node, and select Override>Init.
If you or another developer has already overridden the ‘Init’ method it will not show up in this drop-down. In that case, expand the ‘Methods’ node and look for the ‘Init’ method. Next, right click on the form node and select ‘View Code’ to open the code editor for this form.
Add Code To The Init Method
After overriding the Init method, we need to add code to the Init method. In the code editor window, locate the Init method. If you just created it for the first time it will look like this.
public void init()
{
super();
}
The call to super() runs all of the base system code. This code creates all of the components of the form. Because we will want to interact with the components on the form, it is necessary that we put our code after the call to the super() method.
Add the following code to the init method, so that is looks like this.
public void init()
{
super();
if (element.args())
{
//Parameter Args
if (element.args().parm() == 'EnableReadOnly')
{
//disable data sources
rsmVehicle_ds.allowEdit(false);
rsmVehicle_ds.allowDelete(false);
rsmVehicle_ds.allowCreate(false);
//disable buttons in button group
FormButtonGroup.enabled(false);
}
}
}
Explaining The Code
Let’s discuss what each piece does. You can then make changes based on what is in your form.
if (element.args()) : First, we check to see if the form has an instantiated Args class. The way we do that is to check if the args() method on the element object, which refers to the form, returns a value. If it does, we can proceed. Without this check, if we try to call methods on the Args object, we will get a null reference exception at run time.
if (element.args().parm() == ‘EnableReadOnly’): Secondly, the value we entered into the Menu Item ‘Parameters’ property is copied by the system into the Args class object. We then can read that value out, by calling element.args().parm(). Then, we compare that value to a string, in this case ‘EnableReadyOnly‘, and then take action. If we had other Menu Items that passed in different values, we would want to add additional ‘if’ statements to account for each value.
Controlling The Form
Inside the ‘if’ statement we now can change the behavior of this form. You can do whatever you would like. In my example, I referred to the Data source that is on this form. The name of the Data Source is ‘rsmVehicle’.
In order to refer to a Data source in x++, you need to add ‘_ds’ on to the end of the name of the Data Source. <Data source name>_ds. In this demonstration, I used ‘rsmVehicle_ds’. Namely, I then called the methods, allowEdit, allowDelete, and allowCreate, and passed in false to each one of them. This effectively puts the form into a read-only mode.
Additionally, I have a button group on my form that contains all of the buttons. The name of the button group control is ‘FormButtonGroup’. I set the Auto-Declaration property on that button group to ‘Yes’ so that this control can be referenced x++ code.
Then, I called FormButtonGroup.enabled(false) to disable all of the buttons in this button group.
Again, you can take whatever action you would like. This is just what I did for this example.
The Args Record Property
There are a few additional properties on the Args class that are used to send parameters to forms in D365. However these are properties that do NOT exist on the Menu Item. These properties are set differently.
Record: The record property on the Args class stores an entire table buffer. This property is not set when calling a Menu Item from a Menu. However, if you have a Menu Item Button on a form, you can set the ‘DataSource’ property on this Menu Item Button. And then the system will automatically use the selected record from the current form, and put it into the Args.Record property, and send it to the calling form.
This is very useful. As often times you would like the form you are opening to understand the context from the calling form. And if the calling form uses the same datasource, the form will automatically filter itself to only show the passed in record. I will cover this in a separate article.
Conclusion
In conclusion, a developer can send parameters to forms in D365. One way they do this is by setting properties on a menu item. The data and elements on the form are controlled based on this input. This is very useful. Developers do not have to create two or more forms that are very similar. They can create a single form, and then control how the form works based on information sent into the form.