Not sure why when I run the JS script the second time, the value from last run is still stored as table name in the additional scope.
So first run shows table name as 'shift_segments_tmp_nb....' and second run shows it stored again but table name should be 'shift_segments_tmp_dv....' instead.
The first time I ran the JS script I selected 'shift_segments_tmp_nb..' and the second time I ran it I selected 'shift_segments_tmp_dv..' from the table.
It could be a problem of synchronisation.
I believe that the trigger() function returns a Promise and you need to work with them properly.
I would try to isolate the problem in a separate query and call it like this:
I think this error is probably what is causing my issue of the 'result' variable not getting the correct value. But I am not sure what is causing the error.