Calculated Values in eForms
Calculated fields can be configured in eForms by adding JavaScript to the field's component. In this example, the difference in days between the values entered into two date / time fields is calculated and displayed in a number field.
-
Check the property names of the fields that should be used in your script. The property names can be found by opening the field's component for editing, going to the API tab and making a note of the value of the field labeled 'Property Name'.
-
Open the component of the field that should contain calculations for editing. In this example, a number component is being used. Enter the Data tab of the component.
-
Expand the Calculated Value option and then expand the JavaScript panel.
-
Using JavaScript, calculations can be made to output figures based on the information that has already been entered. In the sample script below, two fields are being referenced by their property name (dayOfLeave1 and dayOfReturn1). The script calculates the number of days between the dates entered in these two fields and displays it in the number field that is currently being configured.
Copyvar ONE_DAY = 1000 * 60 * 60 * 24;
// Convert both dates to milliseconds
var date1_ms = Date.parse(data.dayOfLeave1);
var date2_ms = Date.parse(data.dayOfReturn1);
// Calculate the difference in milliseconds
var difference_ms = Math.abs(date2_ms - date1_ms);
// Convert back to days and return
return Math.round(difference_ms/ONE_DAY);