# Dynamically update available user field values during a profile fill in

**URL:** https://meta.discourse.org/t/dynamically-update-available-user-field-values-during-a-profile-fill-in/261944
**Category:** Development
**Created:** [April 17, 2023, 11:31am UTC](https://meta.discourse.org/t/dynamically-update-available-user-field-values-during-a-profile-fill-in/261944 "2023-04-17T11:31:57Z")
**Posts on this page:** 1
**Showing post:** 6

<div class="post-metadata">

### Author: ![philipp96](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/philipp96/32/305474_2.png) [@philipp96](https://meta.discourse.org/u/philipp96)
#### Post date: [April 22, 2023, 1:37pm UTC](https://meta.discourse.org/t/dynamically-update-available-user-field-values-during-a-profile-fill-in/261944/6 "2023-04-22T13:37:32Z")

</div>

I have found a solution to automatically update this specific case. No API update possibility for customfield options , I have only found a server-side solution here through a cron job. I didn’t use the json file, i used the database entries for automation.

#### **The use case**

The use case for this automation is to allow new users to select existing options when registering, which can help the system to evolve as more users make their preferences known. If a new user cannot find an appropriate option, they can enter their own text in the custom text field, which can then be added to the dropdown list for the next user.

# This should be used with caution, as changes to the databases for Discourse can crash, recommended only for experienced developers!

Requirements:

- [Discourse Data Explorer](https://meta.discourse.org/t/discourse-data-explorer/32566)
- Discourse installed via Docker
- Experience in database management

## Steps 1:

Create two custom fields in /admin/customize/user\_fields, one with a dropdown and the other with just text (in my example: dropdown with user\_field\_5 and text with user\_field\_8).

Note: The ID can be retrieved from /admin/customize/user\_fields.json.

## Step 2:

It is best to use Discourse [Data Explorer](https://meta.discourse.org/t/32566?silent=true) to check your inputs: [Discourse Data Explorer](https://meta.discourse.org/t/discourse-data-explorer/32566)

### Create a new query under /admin/plugins/explorer:

E.g.: User Field Options

```plaintext
SELECT * FROM user_field_options WHERE user_field_id='5'

```

Change user\_field\_id=‘5’ to your dropdown user\_field\_id=‘x’ Then the values that are displayed for your dropdown options will be output.

### Create a second query:

E.g.: User Custom Fields

```plaintext
SELECT * FROM user_custom_fields WHERE name = 'user_field_8'

```

Change ‘user\_field\_8’ to your text custom field ‘user\_field\_x’ Then the values that your users have entered for text will be output.

## Step 3: This is the basic foundation for your cron job to update the user\_field\_options database, where all options for custom fields are listed, which have the associated value 5 for me because of user\_field\_5. I want to add options there.

First, we create a bash script in the directory for Discourse

```plaintext
cd /var/discourse
nano update_dropdown.sh

```

Add the following content if Discourse is run as Docker:

```plaintext
#!/bin/bash
cd /var/discourse
docker exec -i app su postgres -c "psql discourse -c \"UPDATE user_custom_fields SET value = (SELECT value FROM user_field_options WHERE user_field_id = 5 AND user_field_options.value = user_custom_fields.value) WHERE name = 'user_field_8' AND value IS NOT NULL AND value NOT IN (SELECT value FROM user_field_options);\""

```

Make sure you save the script and make it executable

```plaintext
chmod +x update_dropdown.sh

```

Then you can start and test the script

```plaintext
cd /var/discourse
./update_dropdown.sh 

```

## Step 4: To automate this, we need to add the bash script as a cron job:

```plaintext
crontab -e

```

This opens the cron job editor, where you add the code in the next line:

```plaintext
* * * * * /var/discourse/update_dropdown.sh

```

Then the command will be executed every minute. This can of course be adjusted, and the path must be adjusted depending on where you have stored update\_dropdown.sh on the server.

---

_[View the full topic](https://meta.discourse.org/t/dynamically-update-available-user-field-values-during-a-profile-fill-in/261944)._
