Creating a Rule to Compute Age (Capture Claim Form)
It is best practice to listen on parameter update events and modify the parameter values as part of rules that propagate changes through bindings rather than directly updating the control values. This avoids the need to write the
form open scripts when parameter change events occur as part of form initialization.
The rule you create will:
Listen for changes in the value of
Birth Date (BirthDate) parameter.
Create a computation action that will compute age based on updates to the value of
BirthDate parameter and
Customer Age(CustAge) parameters. Since there is already a binding between
Customer Age(CustAge) parameter and
Age(CustAge) control, the
Age control is automatically updated with the computed value.
Procedure
Select the parameter
Customer Age (CustAge) from the Data node of the
Outline view on the
Capture Claim form.
Click the
Add Binding button.
The
Create Binding dialog opens.
Select the radio button
Update this property using Computation Action and click
Next.
In the
Rule: Edit Computation Action dialog, type following JavaScript code as part of an expression that computes the age based on the birth date specified:
var birthDate = context.newValue;
context.form.logger.info('compute_age: Birth date received='
+ birthDate);
if (birthDate != null) {
var now = new Date();
var age = now.getFullYear() - birthDate.getFullYear();
//compute age
if (now.getMonth() <= birthDate.getMonth() && now.getDate()
<= birthDate.getDate()) {
age = age--;
}
if (age <= 0) {
age = 0;
}
context.form.logger.info('compute_age: Returning the age value=' + age);
//populate age field with age computed.
age;
} else {
0;
}
Click
Next.
The
Rule: Pick Events dialog opens.
Click
(plus) button to select an event.
The
Select Event dialog opens.
Select
Update property of the parameter
Birth Date (BirthDate).
Click
OK.
Click
Finish.
Note: Test your script by clicking the
GWT Preview tab, specifying a value for the birth date, and pressing the Enter key. The calculated age will appear in the
Age field.