Using data from the table to trigger an API

Hi everyone,

I would like to use the data from a table column to query an API. For example, considering the attached image, we have the column "company." For each entry in this column, I intend to query the API and, with the obtained results, replace the value in the "company" column, if possible. If directly replacing the value in the "company" column is not feasible, it would be acceptable to insert the results into another column.

Will the API query return that company identifier as part of the payload? If so you can just map the column value to the value in the API response by searching for the identifier. In the 'Mapped value' field for the column:

{{apiResponse.data[apiResponse.data.findIndex(x=>x.company_identifier === item).company_name]}}

Or something like that, depending on the shape of the data that you're getting.

Thank you,

I will simplify my inquiry: for each value of company (id_company), it is necessary to query a specific API that returns the document of that company. The value of this company document should be added as a new column in the table or replace the value of company. How to implement this scenario?

In the attached image, it can be observed that the values in the "CNPJ da Empresa" column are all the same. This is happening because I am only querying the document for the first element of the array; I would like to query for all of them.


retool3

Ah I'm sorry, I misunderstood.

I think that you'll need to use a script similar to:

const company_ids = table1.data.map(x=>x.company)

return Promise.all(company_ids.map(x=>query.trigger({additionalScope:{company: x}})))

In your query, where you have the URL param, use company from the additional scope.

Now you should be able to use my previous code on the results of that script and get the company name out of that object.

If there's an index endpoint available that'd be much better than this. Running a query for every row in that table is going to be a really slow process.

1 Like

Thanks,

It works, but I would also like to fill the value of a certain table with the results of these requests, how would this be possible?

You just need to map the column 'mapped value' to the query result based on that identifier.

{{getCompanyNames.data.find(x=>x.id === item).name}}

Example:

Thanks,

Could you please verify my data? It seems I couldn't find the corresponding company ID, but it does exist, as shown in the image below.

Is there a query where you used the code that I gave you previously?

const company_ids = table1.data.map(x=>x.company)

return Promise.all(company_ids.map(x=>query.trigger({additionalScope:{company: x}})))

The database query itself is not an array so find doesn't work and it only contains the result of the most recent query.