Dynamics 365 Business Central: The following field must be included into the table’s primary key: Field: xxxx Table: xxxx (Validation Results)

Dynamics 365 Business Central

Hi Readers.
Today, I would like to share a development error handling about TableRelation Property.

Validation Results
The following field must be included into the table’s primary key: Field: Name Table: Vendor

I was asked this question recently. This is a very basic question.

As you might know, using TableRelation Property is very convenient when you want to set up a lookup into another table.

TableRelation Property: Sets up a lookup into another table.

For example, on the Item card you can select a vendor from who you usually purchase an item. This is done through a table relationship.

Why does this problem occur when adding the TableRelation property in the extension?
Let’s continue to see.

The following syntax is valid for the TableRelation property:

TableRelation = <TableName>[.<FieldName>] [WHERE(<TableFilters>)] |
[IF(<Conditions>) <TableName>[.<FieldName>] [WHERE(<TableFilters>)] ELSE <TableRelation>]
<Conditions> ::= <TableFilters>
<TableFilters>::= <TableFilter> {,<TableFilter>}
<TableFilter>::= <DestinationFieldName>=CONST(<FieldConst>) | FIELD(<SourceFieldName>)

You can define to specific field in TableRelation property .

For example: TableRelation = Vendor.”No.”;

TableRelation = Vendor.Name;

And then “TableRelation = Vendor.Name” will cause this problem. Becuase the “Name” field is not included into the primary key of Vendor table. In other words, a table relation cannot be made to a field which is not part of the primary key in a table.

So what should we do if we still want to display the name, not the number?
There are several ways.

1. Using ValidateTableRelation Property: Sets whether to validate a table relationship.

More details: How to enter any value in a TableRelation field (Without validating the table relationship)

ValidateTableRelation = false;

Test Video:

PS: You can find a reference in User Setup page.

2. Using OnLookup (Field) Trigger and Page.RunModal() Method

This method is somewhat different from TableRelation in user operation, requiring you to select the data you need in a new pop-up page.

Test video:

3. Add both Number field and Name field, and update the Name field when Number is validated.

Test Video:

PS: Similar to the above, you can also define the name as FlowField.

END

Hope this will help.

Thanks for reading.

ZHU

コメント

タイトルとURLをコピーしました