Example: Attaching and Testing an Event Handler
The example below attaches and tests a sample event handler for reformatting
currency amounts in recordset fields. Before you begin, you should review the
basic steps for creating an event handler.
This example uses two Java files:
- CurrencyFormatter.java - performs the reformatting job of prepending
a dollar sign. This file is in the<vhi>\examples\EventHandlers\java\shared\src\shared\formatting
The CurrencyFormatter can be reused to reformat currencies in other model
elements, such as attributes (such as CurrencyAttributeFormatter.java, also
included in the examples folder).
- CurrencyFieldHandler.java - reformats currencies in recordset fields.
This file is in the <vhi>\examples\EventHandlers\java\FieldEvents\FieldFormatting\scripts\src\fieldformatting
Follow these steps to implement the currency formatting event handler:
- Open the CCSDemo model and press Return to connect.
- Create a folder named fieldformatting under <vhi>\models\CCSDemo\scripts\src\.
- Copy CurrencyFormatter.java and CurrencyFieldHandler.java to the folder you just created.
- On the Events menu in the Design Tool, click Rebuild. The Build Output window indicates when
the build is complete.
- In the Entity window, select the AcctTransactions entity.
- Click the Recordset tab, and then click the Fields subtab.
- Select the Amount Field, then click the Advanced
- In the Advanced Recordset Field Properties dialog box, select
from the Event Handler list.
This dialog box also has options for reviewing the
properties of the event handler or inspecting the code in your default
- Click Close to attach the event handler to the Amount Field. When you return
to the Fields tab, the Amount field now has a lightning
bolt displayed to the left, indicating that an event handler has been
- Click Apply to activate your changes.
- To test the event handler, select Recordset Test from the Debug menu. Confirm
that AcctTransData is listed as the Recordset and Fetch Records is listed
as the Action.
- Click Execute. After the test is complete, the returned values in the Amount
column have a dollar sign and negative amounts are surrounded by parentheses,
as they will be when returned to a client application.