Option source - Auto-populate via service

Option source - Auto-populate via service

Norsk

If you want to see how to populate an option source with a video tutorial, check out the Option Sources 3 Ways video on the Compose YouTube channel. It's in Norwegian, but English subtitles are available. Skip to 1:54 to see the option source example.

Option sources can be created in the Option Source Builder and connected to single- and multiselect question elements, such as radio buttons, check boxes and dropdowns in the Form Builder. Instead of creating and maintaining the options manually, you can populate and update an option source automatically with scheduled generic service calls towards external data sources. Ergo, when the external register is changed, your local option sources and services are automatically updated without you having to lift a finger!

Create a generic service with an option source variable

Before you create the option source you will have to create a generic service which will retrieve the options you wish to use. Navigate to the Organizer and create a new Generic Service. Double-click it to open the Generic Service Builder.

In the Properties panel, enter the URL from which you wish to extract the options.

The API is a data model with information about Norwegian parking providers. For this API we wish to send a call to get a list of options. We therefore select the method GET.

The URL does not depend on any URL parameters, so we do not need any request variables. We do wish to receive the list of options as a response variable from the service call, so Create a Response variable. Also, remember to specify the data format of the data model to the API. In our case the format is JSON.

The type of response variables will by default be set as string based validation and we need to change the type to an Option Source Variable. Double-click on the variable or click Modify to display its properties.

Change the Type to Option Source variable and make sure the validation corresponds with the data you will receive. For this example the data is expected to be in the String format. When you have edited the variable properties, click OK.

Since GET APIs do not allow a request body (and we don’t have any request variables anyway), navigate directly to the Response section. For this API we actually don’t need an XPath expression either since we can work with the full response directly. To transform the response into an option source, we need to provide a transformation script. Click the Transformation button to open the scripting window.

The JSON data model we’re basing the list on is displayed to the right of the script.

def orgnumbers = value.read('$[*].organisasjonsnummer') def names = value.read('$[*].navn') def result = [:] for (int i = 0; i < orgnumbers.size(); i++) { result[orgnumbers[i]] = names[i] } return result

 

The response from the service call is referenced as value in the script with Groovy code. In the two first lines, the script reads and extracts two lists – organization numbers, orgnumbers, which we will use as the answer value for the options, and names, which will be the text associated with the option. The rest of the script defines the final two-column list result we get in return. Click OK when you're done.

Save your draft and create a version, and your generic service is now ready to populate an option source!

Populate an option source with a generic service

Navigate to the Organizer and create a new Option Source. Double-click it to open the Option Source Builder.

Check the Auto-populate via service check box.

Browse to select the generic service we created in the guide above.

The name of the selected service will be displayed in the Generic Service field. You can customize a Schedule for the generic service call. For this example we will keep the default settings, so the generic service call will happen every hour on the hour and update the option source. If an update fails, the result from the last call will be used as the option source.

The generic service has now been connected to the option source. Click Save Draft and the Populate and Publish buttons will be activated. Click Publish to run the service call and populate the list with options.

The list is shown in the Published section and is now possible to connect to forms and workflows. We see the populated list with the organization numbers as answer values and the names as the text representation.

Connect the option source to a single- or multiselect element in a form, by choosing Select in the Properties panel of the element. Browse in the workspace to find the option source we just created.

We see the name of the option source in the field above the Browse button.

Remember to save the draft and you’ll be able to preview the service populated option source in action.