We will first create the required new EDTs, which is done by taking the following steps:
- Create the ConVMSVehRegNum string EDT with the following properties:
Property |
Value |
Name |
ConVMSVehRegNum |
Size |
10 |
Label |
Registration—add a comment that this is a vehicle registration number |
Help Text |
The vehicle registration number |
- We now need the date acquired EDT, so create a date EDT named ConVMSAcquiredDate with the following properties:
Property |
Value |
Name |
ConVMSAcquiredDate |
Extends |
TransDate |
Label |
Date acquired |
Help Text |
The date that the vehicle was acquired |
Although we created this EDT as a date, this is mainly for the way it appears. It is created in the database as a date time, and compiles to a Common Language Runtime (CLR) date time type.
When creating labels, create the help text label with the same name as the main label, but suffixed with HT. You can use copy on the main label (putting, for example, @ConVMS:DateAcquired in the paste buffer) and paste it in the Label property as usual, but we can simply paste our label into the Help property and add HT on the end by clicking on the value, pressing Ctrl + V, End, and typing HT. Note that label IDs are case-sensitive!
- Create a new table and name it ConVMSVehicleTable. The convention for main and worksheet header tables is that they starts with a prefix, followed by the entity name as a singular noun, and suffixed with Table. Remember tables are types and can't have the same name as other types, such as classes and data types.
- Drag the following EDTs on to the Fields node in this order:
- ConVMSVehicleId
- Name
- ConVMSVehicleGroupId
- ConVMSVehicleType
- ConVMSVehRegNum
- ConVMSAcquiredDate
The reason for the order is specifically for the ID, description, and group fields. These are usually placed as the first three fields, and the ID field is usually first.
- Remove the ConVMS prefix from the fields as they are on a table that is in our package. An efficient way is to use the following technique:
- Click on the field.
- Press F2.
- Left-click just after ConVMS.
- Press Shift + Home.
- Press Backspace.
- Click on the next field, and repeat from the F2 step (step 2) for each field.
- On the VehRegNum field, change the AliasFor property to VehicleId.
The AliasFor property allows the user to enter a registration number in the VehicleId field in foreign tables, causing SCM to look up a vehicle and replace the entry with VehicleId. This concept is common on most main tables.
- Make the VehicleGroupId field mandatory.
- Save the table, and open the ConVMSVehicleId EDT. Complete the Reference Table property as ConVMSVehicleTable, right-click on the Table References node, select New Table Reference, and complete the Related Fields property as VehicleId from the drop-down list. If the drop-down list does not show the field, we have either not entered the correct table in the Reference Table property or we forgot to save the table.
- Close the designer table for the EDT and navigate back to the table designer.
- Change the VehicleId field properties as an ID field like so:
Property |
Value |
AllowEdit |
No |
AllowEditOnCreate |
Yes |
Mandatory |
Yes |
The preceding properties only affect the way the field behaves on a form.
- A main table GroupId field usually has an impact on logic, and is usually mandatory. Even if it does not, we should still make the VehicleGroupId field mandatory.
Careful consideration must be taken when deciding on whether the field is mandatory or when it can be edited. In some cases, the decision on whether it can be changed is based on data in other fields or tables. This can be accomplished in the validateField event methods.
- Do not make the VehicleType field mandatory.
Enums start at zero and increment by one each time. SCM validates this using the integer value, which would make the first option invalid. Since enums always default to the first option, the only way to force a selection from the list would be to make the first element, called NotSet, for example, with a blank label. Note that extensible enums cannot be used this way as we can't be certain what the numeric value of the first element is.
- Create a unique index called VehicleIdx with the VehicleId field.
- Group fields are often used for aggregation or search queries; create an index called VehicleGroupIdx and add the VehicleGroupId field to it. The index must not be unique, which is the default setting for this property.
- Complete the table's properties as follows:
Property |
Value |
Label |
The vehicles label ID should be VehicleTable |
Title Field 1 |
VehicleId |
Title Field 2 |
Name |
Cache lookup |
Found |
Clustered Index |
VehicleIdx |
Primary Index |
VehicleIdx |
Table Group |
Main |
Created By
Created Date Time
Modified By
Modified Date Time |
Yes |
Developer documentation |
ConVMSVehicleTable contains vehicle records. If there is anything special about this table, it should be added here. |
Form Ref |
Leave this blank until we have created the form. |
- Create a field group named Overview, labeled appropriately (for example, @SYS9039), and drag in the fields you wish to show on the main list grid on the form: for example, VehicleId, Name, VehicleGroup, and VehicleType. This is to give the user enough information to select a vehicle before choosing to view the details of it; if we add too many fields, it becomes confusing as there is too much information to easily digest.
- Create a field group, Details, and find an appropriate label. Drag in the fields that should show on the header of the form when viewing the details of the vehicle. This should repeat the information from the overview group, as these field groups are not visible to the user at the same point; Overview is for the list of records, and Details is placed at the top of the details form, where the user would want to review the full details of a vehicle.
- Main tables are usually referenced in worksheet tables, and SCM will create a lookup for us based on the relation on the foreign table. To control the fields in the automatic lookup, drag the fields you wish to see into the AutoLookup field group, and ensure that VehicleId is first.
- Create a foreign key relation for the VehicleGroupId field using the following properties:
Parameter |
Value |
Name |
ConVMSVehicleGroup |
Related Table |
ConVMSVehicleGroup |
Cardinality |
OneMore: The field is mandatory |
Related Table Cardinality |
ZeroOne |
Relationship Type |
Association |
On Delete |
Restricted |
- Add a normal field relation to the relation, connecting the VehicleGroupId fields.
- It is common to initialize main tables from defaults, held in parameters. The initValue method is called when the user creates a new record. Right-click on the Methods node and select Override | initValue.
- In the code editor, adjust the code so that it reads as follows:
public void initValue()
{
super();
ConVMSParameters parm = ConVMSParameters::Find();
this.VehicleGroupId = parm.DefaultVehicleGroupId;
}
There is another method, using the defaultField method, which is shown in the There's more... section.
- Next, add the Find and Exist methods using the table's primary key field as usual.
- Finally, we will add a field validation method to ensure that the acquisition date is not before today. Override the validateField method and add the following code between the ret = super(); line and return ret;:
switch (_fieldToCheck)
{
case fieldNum(ConVMSVehicleTable, AcquiredDate):
Timezone clientTimeZone =
DateTimeUtil::getClientMachineTimeZone();
TransDate today =
DateTimeUtil::getSystemDate(clientTimeZone);
if(this.AcquiredDate < today)
{
// The acquisition date must be today or later
ret = checkFailed("@ConVMS:AcqDateMustBeTodayOrLater");
}
break;
}
- Create a label for the error message returned by checkFailed and replace the literal with the label ID.
- Once complete, save and close the table code editor and designer tab pages.
- Should we try to build, we may get the following error:
A reference to 'Dynamics.AX.Directory, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is required to compile this module.
- The error might also read similar to The identifier Name does not represent a known type. This means that our package does not reference the Directory package. Use Dynamics 365 | Model Management | Update model parameters. Select our package, and then add the missing package on the next page. Then choose Refresh models from Dynamics 365 | Model Management.