Replace empty string to null in form submit, and JS limitation in Changeset Object

I might be asking trivial questions but I really couldn't figure them out

In a very simple pgsql form submit, some of foreign key need to be null instead of "" otherwise I get below error

Screenshot 2023-12-19 095952

My first thought is to use inline js to replace "" with null inside Changeset -> Object -> {{form.data}}, but immediately I get run into a error object.keys() is not a function shown in below screenshot.

Screenshot 2023-12-19 095853
I already changed all the field default value to null as a workaround

Then I want to inset modified_at, but then run into another problem where the value would replace the whole object rather than append

image

Am I doing something stupid?
Or there's some other limitation that retool does not run certain inline js?

Hello, you would need to iterate thru the form data and create a new object with the overwritten values, as following
image
image

let newObject = {}
Object.keys(form1.data).forEach((key) => { // iterate thru each key of form data
  if (form1.data[key] === "") { // if form data [key] value is an empty string, replace it with null
    newObject[key] = null 
  } else { // else, keep the current value
    newObject[key] = form1.data[key]
  }
})
return newObject

Thanks for the suggestion.

I thought about creating a JS query which would allow multi-line js code, which I'd assume would be the best practice here.

What's been bugging me is understanding limitation of {{js}} in Retool. after reading the documentation, I thought as long as its one line JS, retool can handle it . apparently there's deeper limitation that I do not yet understand

This turns out to be a stupid mistake on my end, should have been Object.keys(json)