Custom calculated cell does not autopopulate when new row added

Hello for a third time this week!
Yet another slightly different problem from my previous posts (Problem summing items in changesetArray), although still working on the same invoicing system tables.

I have a table (tableAddLineItems) with a custom column (item_total) where I calculate the new line item total prices from the sample# x unit price when I make a new invoice. So far, so good...and my previous post went over getting to that point successfully by using some forum help to finally land on this code:

  (_.find(tableAddLineItems.changesetArray, {})?.sample_number * 
_.find(tableAddLineItems.changesetArray, {})?.unit_price) || 
  (tableAddLineItems.newRows['0'].sample_number * tableAddLineItems.newRows['0'].unit_price)

So this multiplies the sample# x unit price from the changesetArray and, if not present (which all of the lines are except the one new line), it uses the values from the one new row added (tableAddLineItems.newRows['0'], which I know will have to change later once Retool activates multiple new rows which are a planned update).

However, for now the only problem I'm facing is when I turn on that one new row, the cell for the above calculation is not populating with a value, even though the above code is not producing errors and should be working for the newRows['0'] instance. See the screenshot below.

I originally thought it was because the item_total custom column was not editable, since I want it to simply display the math result and not let people mess with it (since they should only be changing sample# or unit price), but activating the "editable in new rows only" option does not fix the problem. Right now, the only solution to make the subtotal correct on the bottom (which sums all the values in that item_total column) is to manually input the final number into the item_total cell of the new row for things to be correct (here would be $2080 instead of the displayed $1080).

Any ideas...or was the new row feature not intended to pick up default/calculated values yet? Thanks!

I also found out, once previewing the above table setup, that the subtotal doesn't "kick it" and display the sum until I put something in the new row item total box (so right-hand column of table)...even if there are a whole bunch of numbers to sum in the other line items above. So I'm thinking there is also something wrong with the order of the numbers it is looking for in the syntax.

Hi again! Since the problem occurs only for adding a row we only need to debug the last part of your code (the part after ||).

(tableAddLineItems.newRows['0'].sample_number * tableAddLineItems.newRows['0'].unit_price)

I think the problem is using ['0'], have you tried [0] instead?

There are actually 2 types that use the subscript operator []: Objects and Arrays. when you use a string inside the operator like ['0'] you're saying the variable is an Object and when you use an int you're saying it's an Array.

myObject = { 
  prop1: "stuff"

myArray = ["stuff2"]

in the above, myObject["prop1"] would be the same as myObject.prop1.

Ah I was hoping those two little apostrophes were going to solve the issue! On a side-note, I find these little JS trip-ups are happening frequently with us working on this, causing us to lose days of frustration, and I swore I would make time for a nice JS syntax intro tutorial (if you have a recommended one, let me know).

It still doesn't seem to be working to make the new row cell autopopulate (see pic), even though the proper numbers are coming up when I hover over the [0] code, as in the pic.

As you'll see in the pic, related to the above second comment I made, it does autopopulate the other cells when they are empty to start! So it is getting read, just not in the one new cell...and the behaviour of the newRows data going into those non-newRows cells above before I put other things in there is also not desired. I have to change the code from "use changeset or newRow" to something like "use changeset or (newRow only if you are a newRow yourself)"...not sure how to get there yet.