How do I replace empty strings with null in table.recordUpdates?

If you are using editable tables in a Retool app, you might notice that when you clear a value from a table cell, Retool interprets this as an empty string '' by default. However if you want that value to be null (or something else), you’ll need to write a short transformer. Here’s one JS snippet (using Lodash, which we include with all Retool instances!) to find these empty strings in my table.recordUpdates array and replace them with null values. This works for editing one or many rows at a time.

let tableData = {{table1.recordUpdates}};

// iterate through each row of recordUpdates
for (let i = 0; i < tableData.length; i++) {  
  // check each value in row object
  _.forOwn(tableData[i],function(value,key) {
  // replace empty strings with `null`
    if (value == "") {
      tableData[i][key] = null
    }
  })
}

return tableData

In the case of this example, I’ve cleared out the name value of this record and it comes up as an empty string '' in the table1.recordUpdates array.

Shared with CloudApp

But after I add the transformer, it changes that empty string value to null, since this is the value that I want to write back to my database in the bulk update query. So instead of using {{table1.recordUpdates}} in my query, I’ll just use {{transformer1.value}}.

3 Likes