Transformer behaving erratically

I have a transformer with the following code to generate autoincrement IDs:

i = {{quotes.data.id.length-1}};
return ({{quotes.data}}==null ? 0 : {{quotes.data.id[i]}}+1);

However, when I run it (or use it with a button) I get random results. Sometimes I get null, sometimes it doesnt work at all.

I suspect the array's order is being modified by the way the table is working. Is it possible the array changes its components order from one run to the other?

Hey @Johan,

Try formatDataAsObject(quotes.data).id. Does that solve it?

hi @minijohn thanks for checking this out. No the issue still persists, the transformer stops working after the first row creation. This is how the transformer looks like now:

i = {{quotes.data}}==null ? 0 : {{quotes.data.id.length-1}};
return ({{quotes.data}}==null ? 0 : {{formatDataAsObject(quotes.data).id[i]}}+1)

but every time I try to run the process again after the first insertion, every subsequent ID is equal to 1, it seems this part: {{formatDataAsObject(quotes.data).id[i]}} is always evaluating to null.

I believe this must be a lack of understanding from my side and not retool's fault or bug so I thank you in advance any light you could shed on the reason why this may be happening.

And yes, quotes is the query which results an Object of arrays according to the documentation.

EDIT:

Having said that, if the result is already an object of arrays, then my best bet would be to use something to turn it to an Array no?

I tried the following:

i = {{quotes.data}}==null ? 0 : {{quotes.data.id.length-1}};
return ({{quotes.data}}==null ? 0 : {{formatDataAsArray(quotes.data)[i].id}}+1)

In this case, the row creation gets to ID = 2, and after that, all subsequent ID's are still = 2.
I have to wait a bit (a couple minutes) and when I run the process again, the Id is correctly added with new insertions I create. I believe this is an actual bug of retool now.

Hey Johan, Retool support here happy to help!

Could you share a screenshot of what {{quotes.data}} looks like? Also, are you instantiating i anywhere? For example, let i = ... I'm wondering if i is not actually being set correctly.

Thanks!