Wednesday 7 March 2018

ReferenceGroupControl Creation

https://devmusings.blog/2018/02/09/reference-group-controls-in-dynamics-365-for-operations/

Microsoft introduced the reference group control in Microsoft Dynamics AX 2012 that is unchanged in Microsoft Dynamics 365 for Finance and Operations. The control is a hybrid between a regular form control and a form group. It acts as a control during form design. At runtime, it appears as a group of controls. There are several reasons to use a reference group control.
  • It simplifies adding a look up to a record identifier field (RefRecId).
  • It reduces the need for coding display methods.
  • Users can filter and sort on fields exposed by the control.
  • It provides additional lookup logic (filters data if the entry is ambiguous).
  • It is smart enough to resolve table inheritance.
With these advantages, there are a couple disadvantages. As the fields store record identifiers, it is more difficult to analyze data using the table browser or SQL. Additionally, these goodies require additional setup such as creating a special data type and setting properties.
For this example, we have a pair of tables: MK_Pirates and MK_Ninjas.
For both tables, we have a unique index using the PirateId and NinjaId fields. Both indexes have the Alternate Key property set and the ReplacementKey property is the respective index.
We need to create a table and form to record encounters between the pirates and ninjas. The table should record a pirate, a ninja, and a result. Our MK_PirateVsNinja table already has the fields for pirate and result. We will be adding the field for ninja.
The first step is to create a new data type for the ninja field. For this, we create a new Int64 data type that extends RefRecId. We name this data type MK_NinjaRecId. On this data type, we set the ReferenceTable property to our MK_Ninja table.
After creating the data type, we need to add it to our encounter table. We can drag the MK_NinjaRecId onto the MK_PirateVsNinja table to create the field. Visual Studio should ask if you want to create a relationship. Click Yes and rename the newly created field to something more readable.
In the editor for the form, drag the newly created Ninja field into the grid. This will create a reference group control. Compile the project and open the form in the browser. We should be able to select pirates and ninja using the drop-down buttons.
Even though the underlying table stores record identifiers, the form shows the identifier of the pirates and ninjas.
Unfortunately, seeing just the identifier is not helpful in this case. We need to show the pirate’s name and ship and ninja’s name and dojo on the form. There are a couple ways we can accomplish this. One way is to modify the AutoIdentification field group on the parent tables. This field group populates automatically based on the replacement key. We can modify it to show the fields we want.
Another way is to change the ReplacementFieldGroup property on the reference group control. This property, that defaults to AutoIdentification, determines which field group from the parent table to display.
After changing either the AutoIdentification field group or the ReplacementFieldGroup property, the form will change to display our intended fields from the parent tables.