Workflows: Trigger 'User Updated' Should Include 'Custom Fields'

AI-generated summary

gassim opened a feature request noting that the User Updated workflow trigger fails to include Custom Fields in its payload. Although the trigger fires when a custom field is modified, it returns an empty array for changes to these fields, making it difficult to determine which specific field was updated. Gassim initially attempted to use standard webhooks but found they also lacked field-level granularity.

As a workaround, gassim shared a SQL Data Explorer query that identifies the most recently updated custom field within the last few seconds. This allows the workflow to infer which field changed by querying the user_custom_fields table directly, bypassing the empty payload issue.

putty suggested alternative approaches, such as using Data Tables to store previous values for a “before and after” comparison or triggering workflows on user login to check for changes. However, gassim argued that checking on profile update is more efficient than checking on login. Gassim clarified their specific use case: syncing a “Subscribe to Newsletter” custom field with an external platform. Since this field is a simple boolean, they only need the current value, not the previous one, rendering complex data table comparisons unnecessary for this specific scenario.

putty pointed out that if custom field changes don’t reliably trigger the standard profile update event, a manual inspection of the webhook body is required. However, gassim clarified that the trigger does fire; it just doesn’t specify the field in the payload. Therefore, the proposed workflow structure is:

  1. Trigger on User Updated.
  2. Use a SQL Query to fetch the latest value of the specific custom field.
  3. Send that value to the external newsletter API.

The discussion highlights the limitation of current workflow triggers regarding custom field metadata and validates the SQL-based workaround for identifying changed fields.

Hi there!

I am trying to use the ‘User Updated’ trigger when a user updates a custom field; however, the fields are limited to the standard default Discourse fields. I think it would be helpful if we can select custom fields as well.

I thought about creating the traditional webhook that listens for ‘user updated’ and sending the request to the a workflow webhook; unfortunately, using a webhook doesn’t specify which field was changed so that didn’t work.

The trigger does work when the custom field is updated but it returns an empty array, so maybe the workaround is using a data explorer query to figure out what changed when the array is blank. :thinking: (Thanks @Frully for all the previous Data Explorer query suggestions, I think it might just be part of the solution).

Update:
Technically this should work:

  • Trigger ‘User Updated’
  • Condition: if change is empty array i.e. ‘custom field’
  • Use SQL Query to figure out what was the latest change:
-- [params]
-- user_id :target_user_id

WITH latest_save AS (
  SELECT MAX(updated_at) AS max_updated_at
  FROM user_custom_fields
  WHERE user_id = :target_user_id
    AND name LIKE 'user_field_%'
)
SELECT
  ucf.user_id,
  COALESCE(uf.name, ucf.name) AS field_name,
  ucf.name AS raw_field_key,
  ucf.value AS current_value,
  ucf.created_at,
  ucf.updated_at AS changed_at
FROM user_custom_fields ucf
CROSS JOIN latest_save ls
LEFT JOIN user_fields uf
  ON ucf.name = CONCAT('user_field_', uf.id)
WHERE ucf.user_id = :target_user_id
  AND ucf.name LIKE 'user_field_%'
  AND ucf.updated_at >= ls.max_updated_at - INTERVAL '5 seconds'
ORDER BY ucf.updated_at DESC, uf.position ASC

[AI Generated SQL that seems to work]


Thank you!

I hope that this trigger could be improved because it will be a great help.

1 Like

Agreed. In the meantime, would it be reasonable to store the user fields in a data table and then check them once the user logs in? If it’s different, execute your workflow. In addition, you’d want something to capture it initially.

1 Like

Nice, thanks for the suggestion! I have not experimented with the data tables yet and still wonder how they work so should be give it a try. I think instead of checking each time users log in, running the workflow when they make the profile update might be more efficient?

I added a new update on how I might approach this using the SQL query above which shows the latest change. I am a few clicks away from implementing it and giving it a try; however, I think I need to not limit it to the “last” change and just get all the changes during the same time as they might update more than one field at the same time. :slight_smile:

Yeah, that could work as long as custom field changes count toward that! My hunch is that you still need to know if the field changed, though—that’s where the data table comes into play.

Granted, I don’t know what you’re wanting to do when this happens. Able to share that?

1 Like

image

There’s an ‘updated_at’ value in the user_custom_fields :slight_smile: It’s true it doesn’t mention what was the previous value but for the use-case below, I guess it doesn’t matter? :slight_smile:

Sure! We have a custom field ‘subscribe to newsletter’ and when a user updates that I want to update the subscription. If it’s changed to false then ‘unsubscribe’ and if it’s changed to ‘true’ then ‘subscribe’. The newsletter platform is external so I eventually use an API request.

I have it covered when ‘user created’ but now I’m looking into when ‘user updates’ that value in their profile. :slight_smile:

Ah, but here we’re back to your original request: updating that field doesn’t trigger the “profile update” event. That’s why I was suggesting something more “manual” but based on user activity.

You could still use this as a trigger, but you’d need additional code/query nodes to inspect the fields, even if they’re not in the initial webhook body. You’ll need to fine-tune the threshold of “how recent a change gets relayed”. That’s where I still go back to a data table for a true before/after reference.

Unless you just want that custom field to send to your workflow regardless of if it’s different. I guess that’s probably the safe route? If any profile change, send that custom field to your newsletter platform.

1 Like

The more I think about it, the more I think this is what you should do:

  1. Create a webhook that triggers on profile updates
  2. Receive the webhook in Workflows
  3. Run a data explorer query to fetch your custom field
  4. Send it to your external service

If you want to limit API calls, you could send only those where the custom field changed recently, but I think that could introduce errors.

I sound like I’m making a commission on data tables, but once again, adding a data table will give you a good before/after reference—unless you want to make an API call to your external service to see if the current field is different.

1 Like

Oh, but it actually does! :grin: The thing is that it doesn’t specify which custom field changed:

It sounds like a nice feature to explore. Though for this use-case it’s a true/false field - ‘subscribe’/‘unsubscribe’ so all I need to know is the new value which I will grab from the data explorer query. :slight_smile: