As we know, many different types of keys exist in AX 2012, such as Surrogate key, Alternate key, Primary key, foreign key, etc.
Since many of them are briefly explained on MSDN.
But there are certain table keys which are difficult to understand just by the definition, something like “Replacement key”. So let me then showcase the practical scenario, like how you can use it and the benefit of having that property in your table.
What is a replacement key??
A replacement key is an alternate key that the system can display on forms instead of a meaningless numeric primary key value. Each table can have a maximum of one replacement key.
The replacement key is chosen by setting the ReplacementKey property on the table. The drop-down list offers every alternate key as an available value.
The drop-down list contains every index that has its AlternateKey property set to Yes.
You might change the default blank value to an index whose field values within each record provide a name or other moniker that is meaningful to people. If a ReplacementKey is chosen, its fields can appear on forms to helpfully identify each record.
The ReplacementKey should be a set of fields that represent the natural key.
Now let’s look at it in AX 2012, how we can create this key and what it looks.
Firstly am going to create a simple table called “EmployeeTable” which contains 3 fields emplId, Name and DOB (dateofbirth).
And now going to create an index on this table where “AllowDuplicate” property should be set as ‘NO’ and alternate key property as ‘YES’.
As shown below:
Once the creation of index is done then we can easily assign
the replacement key to this table as shown here in the below image.
Now creating another table called “EmployeeTransactions” which contains 2 fields TotalAmountCharged and TotalHourWorked.
In order to give relation with EmployeeTable, creating foreign key relations and as soon as we do that, automatically new field (EmployeeTable) is automatically added in our “EmployeeTransactions” table. As shown in the image below:
Now, I can say that a relation exists between the two tables andthe relation is the RecId field of employeetable to that of the newly created employeeTable field (Int64 – datatype).
NOTE: You can also achieve these relations using EDT.
Now let’s create some lines in these tables and check how and what it stores into employeetable field in ‘employeetransaction’ table.
As we have seen from the backend, the table stores the INT64 field i.e., recId of the related ‘employeetble’ record.
So basically, the header and line part in the form represent “employeetable”&” employeetransaction”. Here in the header part, details can be filled up & emplid can be easily accessed, and if you look at the line level, there is a field called reference which I dragged from transaction datasource > employee table field. For reference, just look at the form image above.
Now here comes the magic J as soon as you create & save the record in the header then line level reference gets filled up with related emplid field. But if you look at the backend in tables it would be stored as recid.
So I can say that the Alternate key allows the system to display a significant value on forms instead of a meaningless numeric primary key value. Each table can have a maximum of one replacement key.
Thus with this we are done with the Replacement key functionality in AX 2012 R2. Please input your email-id, if you are looking for XPO of it.