# Action Syntax examples

## Overview

This document provides examples of the [Action Syntax](https://doc.orbeon.com/form-builder/advanced/services-and-actions/actions-syntax) used to define actions in Form Builder.

## Example 1: List of Nobel Prize winners

The Nobel Prize organization exposes a [REST API](https://www.nobelprize.org/about/developer-zone-2/). We would like to create a form that queries that API to return the 2023 Nobel Prize winners, and show these details in a table.

To do this, we start by creating a simple form with a nested repetition:

* a repeated section for the Nobel Prizes
* a nested repeated grid for the laureates

Here is how the form looks like in Form Builder:

![Nobel Prize form](https://4129616727-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LEkBiSDvlXs3VWiO0Zu%2Fuploads%2Fgit-blob-7ac4e9fb40b695a41cd74854b791c499f5b9d6ad%2Faction-syntax-nobel-form.png?alt=media)

We then also create an HTTP Service endpoint:

![Nobel Prize service](https://4129616727-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LEkBiSDvlXs3VWiO0Zu%2Fuploads%2Fgit-blob-4546994181a4c69377b0961e7146f4f038e55fff%2Faction-syntax-nobel-service.png?alt=media)

It points to the following API endpoint:

```
https://api.nobelprize.org/2.1/nobelPrizes?nobelPrizeYear=2023
```

This returns data in JSON format (with `...` to indicate omitted parts):

```json
{
  "nobelPrizes": [
    {
      "awardYear": "2023",
      "category": {
        "en": "Chemistry",
        ...
      },
      ...,
      "laureates": [
        {
          "id": "1029",
          "knownName": {
            "en": "Moungi Bawendi"
          },
          ...,
          "motivation": {
            "en": "in recognition of the extraordinary services he has rendered by the discovery of the laws of chemical dynamics and osmotic pressure in solutions",
            ...
          }
        },
        ...
      ],
      ...
    }
  ],
  "meta": ...,
  "links": ...
}
```

Form Runner operates on an XML view of JSON data so that you can use XPath formulas. You can see it directly in the "XML Response Body" of the HTTP Service Editor. Here is what that view looks like:

```xml
<json type="object">
    <nobelPrizes type="array">
        <_ type="object">
            <awardYear>2023</awardYear>
            <category type="object">
                <en>Chemistry</en>
                ...
            </category>
            ...
            <laureates type="array">
                <_ type="object">
                    <id>1029</id>
                    <knownName type="object">
                        <en>Moungi Bawendi</en>
                    </knownName>
                    ...
                    <motivation type="object">
                        <en>for the discovery and synthesis of quantum dots</en>
                       ...
                    </motivation>
                    ...
                </_>
                ...
            </laureates>
        </_>
    </nobelPrizes>
    ...
    <meta type="object">
        ...
    </meta>
    <links type="object">
        ...
    </links>
</json>
```

Finally, we write, using the Form Builder's Action Syntax, an action that:

* runs upon form load
* calls the `get-nobel-prizes` service
* clears the `prizes` repeat
* iterates over the Nobel Prizes (`/*/nobelPrizes/_`), and for each entry
  * adds iterations to the `prizes` repeat
  * sets values in the controls `year` and `category` from the `awardYear` and `category/en` fields in the data
  * clears the `laureates` repeat
  * iterates over the laureates (`laureates/_`), and for each entry
    * adds iterations to the `laureates` repeat
    * sets values in the controls `known-name` and `motivation` from the `knownName` and `motivation/en` fields in the data

There is a nested iteration due to the nested repeats. here is what the complete listener and action look like:

```xml
<fr:listener version="2018.2" events="form-load-after-controls" actions="my-action"/>

<fr:action name="my-action" version="2018.2">
    <fr:service-call service="get-nobel-prizes"/>
    <fr:repeat-clear repeat="prizes"/>
    <fr:data-iterate ref="/*/nobelPrizes/_">
        <fr:repeat-add-iteration repeat="prizes" at="end"/>
        <fr:control-setvalue value="awardYear" control="year" at="end"/>
        <fr:control-setvalue value="category/en" control="category" at="end"/>
        <fr:repeat-clear repeat="laureates"/>
        <fr:data-iterate ref="laureates/_">
            <fr:repeat-add-iteration repeat="laureates"/>
            <fr:control-setvalue value="knownName" control="known-name" at="end"/>
            <fr:control-setvalue value="motivation/en" control="motivation" at="end"/>
        </fr:data-iterate>
    </fr:data-iterate>
</fr:action>
```

When you test or run the deployed form, you see the Nobel Prize winners for 2023:

![Nobel Prize winners](https://4129616727-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LEkBiSDvlXs3VWiO0Zu%2Fuploads%2Fgit-blob-241951fd8daa83741019a7bb2cdf1c085555b2e3%2Faction-syntax-nobel-result.png?alt=media)

## See also

* Blog post: [Making sense of Form Runner Actions](https://www.orbeon.com/2024/09/making-sense-form-runner-actions)
* [Services and actions overview](https://doc.orbeon.com/form-builder/advanced/services-and-actions)
* [Action Syntax](https://doc.orbeon.com/form-builder/advanced/services-and-actions/actions-syntax)
* [Editing the source code of the form definition](https://doc.orbeon.com/form-builder/advanced/edit-source)
* [Synchronizing repeated content](https://doc.orbeon.com/form-builder/advanced/services-and-actions/synchronize-repeated-content)
* [Simple Actions](https://doc.orbeon.com/form-builder/advanced/services-and-actions/actions)
* [HTTP services](https://doc.orbeon.com/form-builder/advanced/services-and-actions/http-services)
* [Database services](https://doc.orbeon.com/form-builder/advanced/services-and-actions/database-services)
