Customizing a Data Dictionary for the Order Detail Table
In this section, you will customize the cOrderDetailDataDictionary that was automatically created when you created the table. You will add some business rules and other settings to the Data Dictionary. The reason for each Data Dictionary setting you apply will be explained.
The exceptions are business rules already explained when you created the previous Data Dictionaries in the Customizing the Data Dictionaries book. Most explanations can be read on the Customizing a Data Dictionary for the Customer Table page, since this is the first Data Dictionary created.
Steps to Customize the Data Dictionary
-
In Table Explorer, expand the
OrderDetailtable to display its Columns, Indexes, Relationships, and Data Dictionaries. You will seecOrderDetailDataDictionarylisted. -
Double-click on the
cOrderDetailDataDictionaryclass. The class will now open in the Studio's Data Dictionary Designer. -
On the Data Dictionary Designer's Columns tab page:
- Select the
Order_Numbercolumn and set the No Put option in the Data Entry Option group toTrue. - Select the
Detail_Numbercolumn and set the No Put option toTrue.
These options are set because the order and detail numbers are set automatically, so we don't want users able to change them manually. OrderDetail.Order_Number relates to OrderHeader.Order_Number and will be assigned as part of the save. OrderDetail.Detail_Number is assigned by the Auto Increment included in this Data Dictionary.
- Select the
Extended_Pricecolumn and set the Display Only option toTrue.
This option is set because the extended price is calculated automatically, so we do not want users to be able to change it manually. This value is calculated when the detail line is saved.
- Select the
Detail_Numbercolumn, then select the Auto Increment option in the Other group. - Click on the Prompt (...) button in the Auto Increment option form. This will open the Select Auto Increment Table and Column dialog.
- Expand the
OrderHeadertable and select theLast_Detail_Numbercolumn, then click Ok.
This satisfies Business Rule #11, which states that each line item in the invoice's table (or grid) must be numbered automatically. Notice that the source auto-increment table and column is not a system table; it is the parent order-header record. In this sample, each order-detail item number is assigned a consecutive value from the parent order record.
- Set the following Mask Type options in the Appearance group:
- Select the
Qty_Orderedcolumn and set the Mask Type option toMask_Numeric_Window. - Select the
Pricecolumn and set the Mask Type option toMask_Currency_Window. -
Select the
Extended_Pricecolumn and set the Mask Type option toMask_Currency_Window. -
Set the following label options in the Appearance group:
- Select the
Qty_Orderedcolumn:- Set the Long Label option to
Quantity Ordered. - Set the Short Label option to
Quantity. - Set the Status Help option to
Number of items ordered.
- Set the Long Label option to
- Select the
Pricecolumn:- Set the Long Label option to
Price per Unit. - Set the Short Label option to
Price. - Set the Status Help option to
Price per Unit.
- Set the Long Label option to
-
Select the
Extended_Pricecolumn:- Set the Long Label option to
Extended Price. - Set the Short Label option to
Total. - Set the Status Help option to
Total extended price.
- Set the Long Label option to
-
Press
F7to switch to the code editor. Add a method to thecOrderDetailDataDictionaryclass by adding the following code:
Procedure Adjust_Balances Number Qty Number Amt
Subtract Qty from Inventory.On_Hand
Add Amt to OrderHeader.Order_Total
End_Procedure
- Press
Ctrl+F7or selectcOrderDetailDataDictionaryin Code Explorer. In the Properties window, click on the Events tab. -
Double-click on Backout and Update. When you have completed this process, the corresponding events will be listed in the code editor (and the Studio has switched from the Data Dictionary Designer to the code editor).
-
Modify the Backout and Update methods to look like the code below:
Procedure Update
Forward Send Update
Move (OrderDetail.Price * OrderDetail.Qty_Ordered) to OrderDetail.Extended_Price
Send Adjust_Balances OrderDetail.Qty_Ordered OrderDetail.Extended_Price
End_Procedure
Procedure Backout
Forward Send Backout
Send Adjust_Balances (-OrderDetail.Qty_Ordered) (-OrderDetail.Extended_Price)
End_Procedure
Whenever a detail line is saved, edited, or deleted, a number of other balances must also be adjusted.
- Business Rule #5 states that when an item is ordered, you must decrement the quantity ordered from the quantity on hand. This will maintain the amount on hand for each item.
- Business Rule #6 states that the order's Total column is the sum of all the extended prices.
- Business Rule #9 states that to get the extended price for each item, you must multiply the quantity ordered by the unit price.
The Update method is called when a record is being saved or edited, and the Backout method is called when a record is being deleted or edited. The calculations performed in these methods are opposites. They both call the method Adjust_Balances with the appropriate parameters to adjust the data as required.
The value of OrderDetail.Extended_Price is calculated when the detail record is saved. Other methods are used (and described below) which will keep this column current during data entry (before the save). These other methods (Adjust_Display_Total and Entering_Price) are used only to supply the user with useful information during data entry. The calculation that occurs during the save is the calculation (in procedure Update) that ensures that the extended price is correct.
-
Add another method to the
cOrderDetailDataDictionaryclass by adding the following code:Procedure Adjust_Display_Total Integer iField Number nAmnt Integer iQty Get Field_Current_Value Field OrderDetail.Qty_Ordered to iQty Get Field_Current_Value Field OrderDetail.Price to nAmnt Set Field_Current_Value Field OrderDetail.Extended_Price to (nAmnt * iQty) // note we set value, but not changed state! End_ProcedureDuring data entry, we always want the extended price to be up-to-date. Anytime the item price (
OrderDetail.Price) or the quantity ordered (OrderDetail.Qty_Ordered) is changed, we want to adjust the extended price column (OrderDetail.Extended_Price). We will perform this update as part of the column-exiting process. When an item based onQty_OrderedorPriceis exited,Procedure Adjust_Display_Totalwill be called. This method will set the column value forExtended_Pricebased on the column values ofQty_OrderedandPrice.When the extended-price column is changed, all data entry object (DEO) items using this table and column will be updated. This keeps our display up-to-date.
It is instructive to see what actually happens to make this all work:
- The user exits an object (or item) in a DEO.
- As part of item exiting, the column-exit message for the Data Dictionary Object (DDO) is called.
- The column-exit message calls a procedure that changes the value of another column.
- When the column value is changed, all DEOs using that column are updated with the new value.
We use this only to display the extended total. When the record is saved, the extended total is calculated again and this new value is saved. We do this because you cannot be certain that a view will contain the
Qty_OrderedandPricecolumns and, if present, you cannot be certain that a user will enter and exit these columns before saving the record. You should never assume that a column will be entered or exited within a view. It is very important that you understand this. -
Add another method to the
cOrderDetailDataDictionaryclass by adding the following code:Procedure Entering_Price Integer Field# Number nAmnt Handle hoInventoryDD Boolean bHasRecord Get Data_Set Inventory.File_Number to hoInventoryDD If (hoInventoryDD) Begin Get HasRecord of hoInventoryDD to bHasRecord If (nAmnt=0 and bHasRecord) Begin Get File_Field_Current_Value File_Field Inventory.Unit_Price to nAmnt Set Field_Changed_Value Field# to nAmnt Send Adjust_Display_Total Field# nAmnt End End End_ProcedureA detail line is created by selecting an inventory item. The inventory item contains a unit price for the item. This is the suggested unit price. The actual unit price must be entered by the user. We would like to use this suggested price as our default price. We will do this by creating a column-entering message for
OrderDetail.Price. When entering the control associated with the column, the program will check to see if the current column value is zero. If it is, it will assume that a price has not yet been assigned and set it to the inventory item's unit price.Procedure Entering_Pricedemonstrates the following:- We only update the value if the current value is zero. The current value is passed to the procedure.
- We need to get a column value from a foreign table (i.e., the column is not part of the current Data Dictionary; it is part of a parent-table DDO). When we do this, we must
GettheFile_Field_Current_Valueand not theField_Current_Value. Also, we must use theFile_Fieldparameter and not the field parameter. When accessing values from a foreign DDO (or from within a DEO), you must remember to use theFile_Field_prefix. - After setting the new column value (
Field_Changed_Value), we will send the messageAdjust_Display_Total. This will adjust the extended-price column. - Note that all of the column-entry and exit messages perform actions that make data entry more convenient. They do not perform validation or integrity-checking operations. This is typical of column entering and exiting. Validations are performed by column-validation messages and by the
Validate_Savefunction.
-
Press
F7to switch back to the Data Dictionary Designer.- On the Columns tab, select the
Qty_Orderedcolumn and selectAdjust_Display_Totalfrom the combobox dropdown list in the Exit Message option in the Other group. - Select the
Pricecolumn and selectAdjust_Display_Totalfrom the combobox dropdown list in the Exit Message option. - Select the
Pricecolumn and selectEntering_Pricefrom the combobox dropdown list in the Entry Message option.
- On the Columns tab, select the
-
Click the Save File toolbar button (or press
Ctrl + S) to save the Data Dictionary.