MSBA-341 · Lesson 2 of 12 · 19 min

Table and form extensions

▶ Video coming soon A video version of this lesson is on the way.

The first piece of Marisol's request was the simplest to describe: buyers needed two new pieces of information on every purchase line for castings. One was a yes or no flag saying whether a foundry inspection certificate was required. The other was the heat number, the identifier a foundry gives to one melt of metal, which traces a casting back to its chemistry report. Desmond told Prudence that this was the most common kind of extension in any implementation, and that doing it well meant thinking about the data type, the label, where the field shows and how it is found, not just adding a column.

Creating a table extension

In Visual Studio, a table extension is created by finding the standard table in Application Explorer, right-clicking it and choosing to create an extension in the current project. The new element appears in the project as PurchLine.CRCustom, the name Visual Studio suggests from the model. It opens in the same designer as a normal table, with the standard fields shown but greyed out because they belong to the base table. New fields, field groups, indexes, relations and delete actions are added to the extension exactly as they would be on a new table.

Every new field should be based on an extended data type rather than a primitive type. Prudence created two EDTs in the model: CRInspectionRequired based on the standard NoYesId type, and CRHeatNumber based on a string of 20 characters with the label Heat number and help text explaining what a heat is. The fields on the table extension then used those EDTs, which gave them the right labels, lengths and help automatically. If another table later needed a heat number, such as the inspection certificate table in lesson 6, it would use the same EDT.

On the left, a blue panel labelled PurchLine Application Suite lists standard fields: ItemId, PurchQty, PurchPrice, DeliveryDate, LineAmount and about 300 more fields. On the right, a green panel labelled PurchLine.CRCustom extension lists new field CRInspectionRequired, new field CRHeatNumber, new field group CRInspection, new index CRHeatIdx, and in gold, changed property label on one field group. An arrow from the extension to the base table is labelled merged at compile.
The PurchLine table extension adds fields, a field group and an index; the base table is unchanged.
Additions to PurchLine for the inspection requirement.
Element added in PurchLine.CRCustomPurpose
Field CRInspectionRequired (EDT CRInspectionRequired)Yes when the line needs a foundry certificate before receipt
Field CRHeatNumber (EDT CRHeatNumber)The foundry heat that the casting came from
Field group CRInspectionGroups the two fields so forms and entities can show them together
Index CRHeatIdx on CRHeatNumberSupports lookups of all lines from one heat when a defect is found
Relation to CRInspectionCertTable (lesson 6)Links a line to its certificate record

What table extensions can and cannot change

A table extension can add almost anything a new table can have, but it can change only a small set of properties on existing elements. Microsoft documents which properties are changeable through extension; common ones include the label of a field group, the list of fields in a field group, and certain properties of fields such as whether they are mandatory, depending on the case. It cannot change a standard field's data type, remove a standard index or alter a standard relation. If the requirement seems to need one of those changes, it is usually a sign that the design should be different.

Indexes deserve care. A new index on a large standard table such as PurchLine is created during database synchronization, which for a big production table can take time and is part of the deployment window. Desmond added CRHeatIdx because quality engineers would search by heat number when a defect was found, but he warned Prudence never to add indexes speculatively. Each index slows every insert and update on the table a little, and on transaction tables that cost adds up.

Extending enums and extended data types

Base enums can be extended only if Microsoft has marked them as extensible. An extensible enum can gain new values from other models, and those values are stored as integers assigned when the enum is synchronized, so code must never depend on the integer value of an extensible enum. Many standard enums are extensible; some, where the integer values are used in calculations or stored in ways that assume a fixed list, are not. Prudence checked the purchase status enum and found it was not extensible, which is why the inspection status became a new enum, CRInspectionStatus, rather than new values on a standard one.

Extended data types can be extended to change some properties. The most useful is lengthening a string EDT: if a customer's vendor account numbers are longer than the standard length, an EDT extension can increase it, and every field based on the EDT grows at the next synchronization. An extension cannot shorten an EDT or change its base type. Labels and help text can also be changed through an extension, which is a light way to adjust terminology across the whole application.

Always include a default branch when switching over an enum that is, or may become, extensible.
// Code that is safe with an extensible enum
switch (purchLine.CRInspectionStatus)
{
    case CRInspectionStatus::NotRequired:
        return true;
    case CRInspectionStatus::Received:
        return true;
    case CRInspectionStatus::Waived:
        return true;
    default:
        // Other models may add values; treat unknown values as not ready
        return false;
}

Form extensions

Showing the fields to buyers needs a form extension. Prudence created PurchTable.CRCustom, an extension of the purchase order form. The designer shows the full standard form tree, again greyed out, and lets the developer add controls into existing groups, tabs and grids. She added the CRInspection field group to the line details tab, so the two fields appeared together, and added the heat number as a column in the lines grid so buyers could see it without opening the details.

Form extensions can add controls, add new data sources, add fields to existing data sources, and change some control properties, such as visibility, label and position within a parent. They cannot delete standard controls or change a control's type. Adding a new data source is how a form shows data from a new table; Prudence would later add CRInspectionCertTable as a data source joined to the purchase lines so the certificate number could be displayed.

Form extensions in practice.
Form extension canForm extension cannot
Add controls into existing containersDelete standard controls
Add data sources and data source fieldsChange a control's type
Change permitted properties such as Visible or LabelChange the form pattern of the standard form
Wrap form methods, data source methods and control methods (lesson 3)Replace standard methods entirely
Subscribe to form and control events (lesson 4)Reorder standard controls freely in every container

Form patterns still apply. When a control is added to a standard form, the pattern check runs on the combined form, and adding a control in the wrong place can produce a pattern warning. The usual fix is to put new fields inside an existing group or tab page that already follows the pattern, rather than creating a new top-level container. Prudence's first attempt added a new tab directly under the design, which triggered a warning; moving the group into the existing line details tab cleared it.

Extending queries, views and data entities

Queries, views and data entities can be extended within limits. A query extension can add data sources and ranges. A data entity extension can add fields that come from the entity's existing data sources, or from data sources added in the extension, which is how a new field on PurchLine becomes available to Excel, OData and data management. Prudence added CRInspectionRequired and CRHeatNumber to an extension of the purchase order lines entity so that buyers editing lines in Excel would see them, and so the migration team could load open purchase lines with heat numbers already set. The first lesson of the next course, MSBA-342, goes deeper into entities.

Synchronization and deployment

After a table extension is built, the database must be synchronized so the new columns and index exist. In the unified developer experience, deploying the model from Visual Studio offers a synchronize option, and Prudence's environment ran it as part of the deployment. In sandbox and production environments, synchronization runs as part of applying the deployable package. Desmond pointed out that adding a column to a very large table is quick in modern SQL, but adding an index or changing a column can take longer, and the release notes for each package should say which tables change.

Field groups, labels and help

Field groups matter more than they first appear to. Forms, data entities and lookups often show fields by group rather than one at a time, so a new field group lets the two inspection fields travel together into any form that shows it. Standard tables also have special groups. AutoReport decides which fields appear when a user prints a quick report from a grid, and AutoLookup decides which fields appear in a lookup when no custom lookup is defined. Prudence added CRHeatNumber to the AutoReport group through the extension so that buyers printing a list of lines would see it, and left AutoLookup alone because nobody looks up purchase lines by heat.

Every new field, EDT, enum value and field group needs a label from the CRCustom label file, with entries in each language the model supports: English for Canada and the United States, French for Canada and Spanish for Mexico. Help text deserves the same care. The help text on CRHeatNumber explained in one sentence what a heat number is and where to find it on a foundry certificate, which saved the receiving clerks a call to quality the first time they saw the field.

Relations, delete actions and lookups

A table extension can add relations from the standard table to other tables. The relation from PurchLine to CRInspectionCertTable did three jobs: it gave the heat number field an automatic lookup of recorded certificates, it let the form join to the certificate table, and it documented the link for anyone reading the metadata. Delete actions can be added too. Desmond did not add one from PurchLine to the certificate table, because certificates had to survive the deletion of a purchase line for audit reasons. Instead the certificate table kept its own record of the purchase order and line number.

Testing table and form extensions

Table extensions are easy to test by hand and easy to forget in automated tests. Prudence's test list for this lesson's work was short: create a purchase line for a casting and set both fields; check that the heat number shows in the grid and in the line details; open the purchase order lines entity in Excel and confirm both columns appear; run the database synchronization in a fresh environment and confirm the index is created; and switch the user's language to French and Spanish to check every label. She also checked that the fields stayed empty and caused no errors for purchase lines that had nothing to do with castings, because most of Cobalt Ridge's purchase lines were for indirect items and services.

Prudence's last question of the session was about naming. Why PurchLine.CRCustom and not PurchLine.CRInspection? Desmond explained that Visual Studio names an extension after the base element and the model by default, and that a model can have only one extension of a given element. All of CobaltRidgeCustom's changes to PurchLine therefore lived in one extension element, and the CR prefix on every new field, group and index showed which changes belonged to Cobalt Ridge. Feature names went into the field and class names instead, so CRInspectionRequired and CRHeatNumber said what they were for.

Worked problem

Adding the heat number to receipts

Quality engineers want the heat number to appear on the product receipt line as well, so that inventory can be traced by heat after the purchase order is closed. Where should the field go and how does it get there?

Show solution
  1. Identify the receipt line table: product receipt lines are stored in VendPackingSlipTrans.
  2. Create a table extension VendPackingSlipTrans.CRCustom with a CRHeatNumber field based on the same EDT.
  3. Copy the value from the purchase line when the receipt line is created. That is logic, not metadata, so it needs a Chain of Command wrapper or an event handler at the point where receipt lines are initialized from purchase lines (lesson 3).
  4. Add the field to the receipt journal form through a form extension so it can be seen.

Answer: A table extension stores the field, a form extension shows it, and a small piece of extension logic copies it when receipts are posted. Reusing the same EDT keeps label and length consistent.

Common mistakes

  • MistakeBasing new fields directly on primitive types.

    InsteadCreate or reuse an EDT so labels, lengths and help text are consistent.

  • MistakeAdding new values to an enum that is not marked extensible.

    InsteadCheck the enum's IsExtensible property; if it is not extensible, create a new enum.

  • MistakeWriting code that compares an extensible enum to an integer.

    InsteadCompare to named values and include a default branch.

  • MistakeAdding a new top-level tab to a standard form and ignoring pattern warnings.

    InsteadPlace new controls in existing containers that follow the form pattern.

Summary and key terms

  • A table extension adds fields, field groups, indexes, relations and delete actions to a standard table and can change only permitted properties.
  • New fields should use prefixed EDTs so labels and lengths are consistent wherever the data appears.
  • Only enums marked extensible can gain values; code must not depend on their integer values.
  • Form extensions add controls and data sources and change permitted properties; pattern rules still apply.
  • Data entity extensions expose new fields to Excel, OData and data management.
Table extension
An element that adds to a standard table without modifying it.
Form extension
An element that adds controls, data sources or permitted property changes to a standard form.
Extensible enum
An enum that other models can add values to; its integer values are assigned at synchronization.
Heat number
A foundry's identifier for one melt of metal, used to trace castings to a chemistry report.
Database synchronization
The step that updates the database schema to match the table metadata.

Practice problems

Work each problem on paper first, then reveal the answer to check.

  1. Why did Prudence create a new CRInspectionStatus enum instead of extending the purchase status?

    Show answer

    The purchase status enum is not extensible.

  2. How can a new PurchLine field appear in Excel?

    Show answer

    Add it to an extension of the purchase order lines data entity.

  3. Can an EDT extension shorten a string?

    Show answer

    No. It can lengthen a string EDT and change labels and help, but not shorten it or change its base type.

Lesson quiz · 5 questions

Score 60% or more to complete this lesson.

Take the quiz