Using SQL Connector to Populate Drop Down List

Hi all!

I had a quick post I wanted to write up on a fairly common topic - using SQL connectors to dynamically populate a dropdown input widget in a Tulip app. In this example, I’m going to query a SQL table that is storing a list of tasks so that I can select which task I want to begin in my application.

To start, let me quickly cover why this even warrants a discussion, which distills down to data type matching. If you return multiple rows from your SQL connector function, the data structure is effectively a list of objects, where each output from the function maps to an object attribute:

[
    {
        “Task” : “Create Tulip Task app”
    },
    {
        “Task” : “Write blog post”
    },
    {
        “Task” : “Post blog on community”
    },
    ...
]

However, a dropdown input widget does not accept a list of objects as a valid data type, so in our case, we need to look at how we can transform the list of objects to a list of text values:

[
    "Create Tulip Task app",
    "Write blog post",
    “Post blog on community”
    ...
]

Getting into the example, I have the following SQL query that is just returning a single column of data back from a table:

Once I run this connector function in an app and story it to a new array (make sure to do this rather than just a variable), I can transform this data using the MAP_TO_TEXT_LIST() expression. Specifically, the expression I used was:

map_to_text_list(@Variable.Task List Return , 'Task')

This uses the Task key (corresponding to the output I defined) and extracts this to a list of values sourced from the connector function return. After this transformation, if you’ve set your dropdown input widget to point to the same variable as the expression, you should see the options listed!

Let me know if you have any questions!

2 Likes

FYI @twatkins this would have been an alternative approach to solving your custom widget problem earlier!

My struggle is getting the value back out of the drop down :slight_smile:
I’ve done this before One way to do Tulip dropdowns from tables (and passing an ID)
But I wish we could return the index from the selected item!

You can:


1 Like

ohhh, I’ll have to try that!

Many thanks, I wonder when ARRAY_INDEX_OF was introduced (I hope I didn’t miss it when I tried it 2 years back!)

Looks like it was added back in r232!