Events / Calculator
Compose Enterprise is going through a design update. The illustrations on this page might be of the old design, but the information is up to date. Thank you for your patience during this update!
Content
Event options
Action – Execute script
Action – Call CNG service
Action – Call Generic service
One of the most practical features of Compose is the ability to trigger actions. In the Builder section of the Form Builder, in the Properties panel at the bottom of the Builder tab for question elements and the Button element, you’ll find a green button titled Events. By clicking it, you’ll enter the Events Configuration menu where you can configure actions triggered by events, aka. trigger actions based on the answer given to a form question. Events can for instance populate form answers from external sources, transform answers given or use answers to populate other questions.
Event options
In the Event column in the Events Configuration menu, for most elements you may choose from two events. One is whether the action occurs after the answer is completed (in the question element), aka. in the event After answer is completed, or the action occurs When filling out answer.
Once the Event is picked, you may choose from three actions; Execute script, Call CNG Service and Call Generic Service.
Action – Execute script
In this example we will demonstrate the action Execute Script. We will use the script as a calculator to calculate the answer of one input field based on the answers provided in two other input fields. Check out the Map - Event script with coordinates user guide for another user case with executing scripts. Start by adding three input fields to the canvas. The questions in this example have the question IDs Q1, Q2 and Q3.
As we will do calculations with integers, set the validation to Integer for all three elements.
We will be multiplying the answers from the two first questions and displaying the calculated value in the last input field. We want to run this calculation every time a respondent user updates any of the two first fields. To be able to do this we need to add an event to both of these questions. To add the Event, select the first question and press the Events button in the Builder tab of Properties panel.
In the Events Configuration menu, select New event.
From the dropdown lists, choose the action Execute script and the event After answer is completed as we have no interest in seeing the calculation before the answers are fully provided.
Then enter the script for the calculation in the script window on the right hand side. This script translates to setting the Cost of labor to 0 until both Labor hours and Hourly rate are answered. Then the script will multiply the two and populate Cost of labor with the answer.
Q3.answer = 0
if (Q1.answer != null && Q2.answer != null) {
Q3.answer = (Q1.answer * Q2.answer)
}
Click OK. This script will now be executed after the respondent users has entered an answer in the input field and moved the focus away from the field. Since we want the script to run also after the respondent user enters an answer in the next input field, we need to add the same event to this question element.
Select Hourly rate and press the Events button.
Select After answer is completed and Execute script, and enter the same Script as before.
When the events with the scripts have been added to the form, remember to Save Draft and then you can preview to see the executed calculation. To use the form in a workflow, remember to version the form with the saved changes.
Respondent view with only one entered answer
Respondent view with both entered answers
Available functionalities
The following functionalities are some of the available methods you can use for form population scripts. These functionalities enables the user to modify answer values for questions.
Value input questions - String validation
Value input questions are question elements with text-based input fields like input field elements, text area elements, date elements and password elements.
Statement | Description |
Q1.answer = "populated input value" | Sets the answer to the given value. |
Q1.answer will always return either a string or null.
Value input questions - Integer/decimal validation
In this table, question Q2 is integer validated and Q3 is decimal validated.
Statement | Description |
Q2.answer = 101 | Sets the answer to the given value. |
Q2.answer = "101" | Sets the answer and converting the value to BigInteger. Exception if conversion fails. |
Q3.answer = 201.34 | Sets the answer to the given value. |
Q3.answer = "201.34" | Sets the answer and converting the value to BigDecimal. Exception if conversion fails. |
Q2.answer will always return either a BigInteger or null. Q3.answer will always return either a BigDecimal or null.
Value input questions - Date validation
Statement | Description |
Q4.answer = "2014-11-23" | Sets the answer to the given value. Converts the string value to a Date Object. Exception if conversion fails. Format should be YYYY-MM-DD. |
Q4.answer = Util.currentDate() | Sets the answer to the current date. |
Q4.answer = Util.newDate('2014-11-23', 'Y-MM-DD') | Sets the answer to the given value. Format can be specified in the second argument. Exception if conversion fails. |
Q4.answer = Util.newDate('2014-11-23') | Sets the answer to the given value. Uses YYYY-MM-DD format. |
Q4.answer will always return either null or an Object representing the date.
Single-select questions
Single-select questions are question elements with predefined answer options whereas you can only select one answer, like radio button elements and dropdown elements.
Statement | Description |
Q5.setAsAnswerValue('answer-value') | Sets the answer to the given option with the specified answer-value (not to be confused with the option label). |
Q5.answer = Q6.answer | Valid only if both Q5 and Q6 are single-select questions. |
In all passed cases the value will be converted with the correct validation. Exception if conversion fails.
Multi-select questions
Multi-select questions are question elements with predefined answer options whereas you can select multiple answers, like check box elements.
Statement | Description |
Q7.setAsAnswerValues('answer-value1','answer-value2') | Sets the answers to the given options with the specified answer-values (not to be confused with the option labels). |
Q7.setAsAnswerValues(internalAnswerValueList) | internalAnswerValueList is a variable of type java.util.List. Sets the answers to these values. |
Q7.answers = Q8.answers | Valid only if both Q7 and Q8 are multi-select questions. |
In all passed cases the value will be converted with the correct validation. Exception if conversion fails.
Action – Call CNG Service
This action is currently not much in use as the Call Generic Service action provides all the same functionality and more than CNG services.
Action – Call Generic Service
Learn how to connect generic services to a form with an event in the Call Generic Service section in the Generic services (form and workflow population) user guide.