To populate an item with values from another table:
A Post-Query trigger fires when the operator or the application executes a query. It fires once for each record fetched, which means that your code inside the trigger can read and set the values of values in each record.
To populate a control item named CUSTOMER_NAME in a data block named ORD_BLK, you could create the following Post-Query trigger attached to the ORD_BLK block.
Post-Query Trigger for ORD_BLK block:
SELECT name
INTO :ord_blk.customer_name
FROM s_customer
WHERE s_customer.id = :ord_blk.customer_id;
The Post-Query trigger takes care of retrieving the customer name when the operator queries orders in the block, but you also need to display the customer name when the operator is entering or updating the customer ID item during data entry. To handle this task, create a When-Validate-Item trigger attached to the customer ID item.
A When-Validate-Item trigger fires when the operator or the application navigates out of the item, and gives you the opportunity to issue a SELECT statement that populates the Customer_Name item.
When-Validate-Item Trigger for Customer ID item:
BEGIN
SELECT name INTO :ord_blk.customer_name FROM s_customer WHERE s_customer.id = :ord_blk.customer_id; EXCEPTION
WHEN NO_DATA_FOUND THEN Message('Invalid Customer ID. Please enter again.'); RAISE Form_Trigger_Failure; END;