How to get Component's value by component name/id?

Instead of writing a lot of queries that update its own single field, I would like to use one generic query which is triggered by multiple components.

I have access to the triggeredById variable which returns the component’s Id.
How can I get the component’s value using this id?

Hey @_andrey! Great question. The issue is that “value” means different things across different components. What’s your use case exactly - what are you trying to do with the triggering component’s value? You should be able to take the triggered by ID and just add .value in most cases.

Hi @justin!
I have 2 Editable Text components calling the same query.
Inside of this query I can get the component id (stored in the triggeredById variable).
Is there any way to get a reference to the component and read it’s value?

I can obviously do something like this:

let value = ""
if(triggeredById == "component1") value = component1.value
else if(triggeredById == "component2") value = component2.value

Is there any way to avoid the if-else?
E.g. something like this (pseudocode): triggeredById.value
Thanks!

Ah, that’s what I guessed. Yea, for now there’s no way around this unfortunately - your logic seems pretty much correct :confused: How many components are you working with here - would the if else be too unwieldy?

The best option I’ve come to with this kind of scenario is creating a “dictionary” object like this and then selecting from that using bracket notation

var value = {"eventLogs": eventLogs.value, "otherThing": otherThing.value}[triggeredById]
2 Likes

As a complement to this question - I have a series of components which are identified by their IDs, i.e. "name7", "name8","name9". I plan to iterate through a script with (7,8,9) as an array, which will then locate the corresponding component. For example, when the script is on 7, it will find "name7" and return the value, and so forth. Is there a way to do this programmatically? Simply concatenating "name" + [array value] as a string doesn't work, as Retool won't recognise that it should reference a component.

Perhaps a similar solution to what @alex-w described could work here

const components =  {"name4": name4.value, , "name5": name5.value, "name6": name6.value, "name7": name7.value,"name8": name8.value, "name9": name9.value}

[7,8,9].forEach( (i) => {
let value = components[`name${i}`]
// do something with value
})

It is not the most ergonomic solution for sure, but I am not sure that there is a better, safe alternative.