Increment IDs automatically

Hello everybody,
First of all, let me say that I am a beginner...
Currently I am consulting the Retool documentation.

I am in the chapter "Increment IDs automatically"

Unless I am mistaken, the following code, which is listed in the documentation, does not work:

{{
     table1.selectedRow
       ? table1.selectedRow
       : { id: (Math.max(...query1.data.id) + 1).toString() };
}}

If a line of the table is selected, no problem, the fields of the form are completed correctly.
If no line is selected, no field is completed, including the "ID" field when it should be returned by the code.

However, I can get this code to work:

{{
     table1.selectedRow.id
       ? table1.selectedRow
       : { id: (Math.max(...query1.data.id) + 1).toString() };
}}

I would like explanations and to know if my workaround is good.

To finish this code we find it in the documentation at this location also:

Thanks in advance. Sincerely.

Hello, welcome to the community.

You should not add .id to selectedRow, if the selectedRow is null, selectedRow.id will throw an error about referrence.

This code will not work, unless you delete the last ;;
It support only inline javascript in data source.

The demo code is work correct in my app if I delete the last ;;

1 Like