Update rows and add new ones using an api in loop

Hey @IshanBizongo!

Does your enriched table need to be saved to an external DB or are you just looking to view it in Retool? Either way, this post on running a query for each item in an array and returning all the results together might be helpful. You may be able to use something like the following:

const originalData = fileButton1.parsedValue[0]; //should be [{hsn_code: 4203}, {hsn_code: 4202}]
async function enrichRow(row){
   const data = await enrichingQuery.trigger({additionalScope: {hsn_code: row.hsn_code}});
   const additionalData = _.pick(data.results[0].hits[0], ['chapter_name', 'product_description']); //this uses the lodash _.pick function to grab the properties you want but you may need to do some extra parsing here instead
   return Object.assign(row, additionalData); 
}
const enrichedData = await Promise.all(originalData.map(enrichRow));
return enrichedData;

Once you have the enriched data you can either pass it to and update query to modify an external database or store it in a temp state if you only intend to use it within the current session.

This likely won't be the exact syntax you need but is intended to give an idea of what's possible. Let me know if this seems like it could work or if you have any further questions :slightly_smiling_face:

1 Like