How to use "Update a record, or create a new record if it doesn't exist"?

Hi! Can anyone give an example of how to use "Update a record, or create a new record if it doesn't exist" for a simple CRUD operation? Which fields to insert into Filter by and which objects into Changeset (changesetArray or newRows or something else). There is no documentation!

1 Like

Hi Alex,
Welcome to the Retool Community Forum! Looks like there is two asks here.

  1. To update a record that does exists, you typically want to filter by the most unique key attached to a specific entity. This is usually the primary key or id of an entity. You can reference the component and use dot notation to access the correct information like so.

    Then to update a specific field, you'd select the correct field that you want to update from the left hand drop down. On the right you'd leverage the changeSetArray object to access to correct info. Because this is an array, you'd want to key into the object at the 0th index. Example below

  2. To create a resource that does not already exist, you can select which resource you are trying to insert into, then the action you are trying to perform. Then you can select the parameters that match the qualities of that resource, then reference the components input value. Example below.

Please let us know if this was helpful or if you have any other questions :slight_smile:

Diego, big thanks for your answer, but how to use all these operations in case edit or add a row in an editable table (not from a form)? Table has only 'Save action'. Retool has Query action type "Update a record? or create a new record if it doesn't exist", but how it works can't figure out

Hi @AlexSh, here is a step by step guide that goes over adding an event handler to the "Save action," which should run your update query.

On the other hand, once you add the "Add Row" action to your table:

Just add an event handler that will run your query to create a new record.

1 Like