By using database services, a PE feature, you can use data stored in any table of a relational database, for instance to dynamically populate a dropdown, or to pre-populate fields based on a value entered by users.
A SQL query, when successful, returns an XML document with the following format:
- A root element which is always
- It contains one nested
<row>element per row.
- Each such element in turn contains one element per selected column, derived from the column name:
- The name is converted to lowercase.
- Underscores (
_) replaced by dashes (
For example, values for the
dept_nocolumn end up in
See below for concrete examples.
In what follows, we'll see how you can populate a Department dropdown in your form using values stored in an
departmentstable of your relational database.
You start, in Form Builder, by creating a new database service, clicking Add in the left sidebar under Database Services. This opens the Database Service Editor.
Under Datasource you type the name of a datasource you setup in your application server. This is the the JNDI name of the datasource, without the
jdbc/part. If you type
employees, Orbeon Forms will look for
If you're using Tomcat, the simplest way of setting up a datasource is to edit Tomcat's
server.xml, there add a
<Context>for Orbeon Forms if you don't have one already, and inside it add a
<Resource>pointing to your database. On Tomcat, you also need to put the database JDBC driver in Tomcat's
Connect to the database
Still in the Database Service Editor, you write the SQL query to run in the database. When that query runs, Orbeon Forms creates an XML document with the returned data, and you'll be referring to parts of that document when linking the database service to a specific dropdown.
The user interface in Form Builder looks like this:
Database Service Editor
This diagram summarizes the mapping:
Mapping of columns to XML elements
To "link" the service to the dropdown, you create a new action, set it to run on Form Load, call the service you earlier named
list-departments. Here is how the user interface looks like:
Action general settings
With the result of the service you want to set the list of possible values of an a Department dropdown you have in the form. This is where you extract data from the XML document seen earlier, and you do this with 3 XPath expressions. The first points to the "rows", and will almost always be
/response/row. Next, you need to tell Orbeon Forms where it can find, inside the
<row>, the label (the text shown to users) and the value (what is stored in data when users make a selection).
Here is how the user interface looks like:
This diagram summarizes the mapping:
Link to the dropdown
Finally, when your form runs and users make a selection in the dropdown, the value, here the content of the
<dept-no>element, is used to populate the element corresponding to the field in the form data.
Say that when users enter a value in employee number, you want to lookup the corresponding employee in your database and populate other fields, First name and Last name, based on the information you find about that employee. We've already know how to establish a connection with the database, so let's start by seeing how we can use the value of a field in a SQL query.
Your SQL query can contain parameters. Those look like:
<sql:param type="xs:string" select=""/>. The
typeattribute corresponds to the SQL type to use (e.g.
xs:decimal, …). The
selectattribute must be left blank; it is filled-out by the Actions Editor when the service is called.
When the query runs, the value of each parameter is set to the current value of a form field, and you define the mapping between parameter in the SQL query and form field when you create an action. That mapping is done by position; e.g. in the above query, you'll want to set parameter 1 to the control containing an employee id.
Set service parameter
Finally, you use the Set Response Control Values section of the Actions Editor dialog to extract the information you're interested in from the XML produced based on the result from the query, and populate fields in the form.
Set control values
Say you will be passing a request parameter
employee-id=100to your form, and want to load data from a relational database about the employee with specified id, this to pre-populate some fields, such their first name, last name, hire data, and department, as shown in the screenshot below.
You can do this as follows, in Form Builder:
- 1.Create a field to hold the employee id passed through the request parameter. You might want to put this field in another section, for instance named "(Internal)", that you hide from end users by setting in its Section Settings dialog, the Visibility to No. For the field, you can use a Hidden Field, or a Calculated Value if you'd like to see its value at runtime when debugging the form.
- 2.In the Control Settings for that field, name it
employee-id, and in the Formulas tab, set its Initial Value to
- 3.Create a database service that retrieves the information about the employee, with a statement like the one below.
- 4.Create an action that, on form load, runs the database service, passing the value of the
employee-idfield, and setting the value of the first name, last name, hire data, and department fields to values returned by the SQL query.
WHERE employee_id = <sql:param type="xs:string" select=""/>
You can get the source of the form described above, and run it for yourself on the instance of Orbeon Forms deployed on demo.orbeon.orbeon, which has the
employeestable used by the above query. After you publish the form, try opening its
/newpage, passing the request parameter