Step 6 Complete Referential Integrity

Visual LANSA

Step 6. Complete Referential Integrity

REP007 - File Validation Rules/Triggers

In this step you will complete the referential integrity checks. You will add a rule that checks if any employees exist for a specific department before allowing the department to be deleted from the Department File. This rule will prevent an employee from losing its parent department.

1.  Open file iiiDepartments in the Editor. 

2.  Select the Rules and Triggers tab.

3.  Select the key field iiiDeptCode field in the list of rules.

4.  Select the Designer button on the ribbon, and expand the Add menu to select Add Lookup Check to add the rule to the iiiDeptCode field.

5.  In the Details tab, create the rule as follows:

Description

Cannot delete if Employee exists

Sequence

2

File Name

iiiEmpByDeptView

When Inserting

Never apply rule

When Updating

Never apply rule

When Deleting

Always apply rule (DLT)

 

   Use the ellipsis button for File Name to use the Find dialog  :

 

     Note that the rule is only enforced when deleting a record. Your Details tab should now look like this:

6.  In the Actions section, define the File Lookup Rule to set the field in error if the department exists in the Employee file:

If a "key match" is found in target file

Set field in error (ERROR)

If a "key match" is NOT found in target file

Evaluate next rule (NEXT)

Message file

(delete the entry in this field)

Message Number

(delete the entry in this field)

Message text

Cannot delete if used in Employee file

 

     Your Details tab should now look like this:

7.  Recompile the file iiiDepartments.

     Check the file compile completed successfully.

8.  Execute your Department Maintenance form iiiMaintDepartment.

a.  Try to delete department ADM. You will not be able to delete this record.

b.  Add a new department XYZ. Delete this department. The delete will be allowed because no employees have been defined for this department.

9.  Exit the application.

10. Close the file.