Calculated field on Form not saving to table

What I’m trying to do:

Saving a calculated field on a form to a table column on an edit/update form.
The calculated field is derived from data already entered on the form

What I’ve tried and what’s not working:

I have found that only way to save the calculated data is to enter the calculated field and press the enter key on the keyboard at the end of the entry.

** Help

What am I missing when setting the calculated field please so that it saves without having to press ‘enter’?

This will probably be relevant reading: Refresh data bindings when any key in `self.items` changes

Note that I’m making the assumption that you’re using writeback data bindings. If you’re not, you’ll need to explain how you’re saving the rest of the data to the table.

Hi

Thanks for your help - still struggling.

I am using an alert form loading the data to the table on a custom save button via a dictionary as follows
result = {
‘change_note_id’ : self.text_box_1.text,
‘title’: self.text_area_1.text,
‘description’: self.text_area_2.text,
‘function’ : self.drop_down_2.selected_value,
‘product_area’: self.product_area_drop_down.selected_value,
‘change_date’ : now,
‘change_search_date’ : nowdate,
‘stage’ :self.stage_drop_down.selected_value,
‘user’ :self.user_drop_down.selected_value,
‘type’ :self.tyype_drop_down.selected_value,
‘classid’ : self.class_drop_down.selected_value,
‘difficulty’: self.difficulty_textbox.text,
‘payoff’ : self.payoff_textbox.text,
‘ips’ : self.difficulty_textbox.text * self.payoff_textbox.text,
‘pick’ : self.pick_textbox.text, # this is defined by using a calculation using ‘difficulty’ and ‘payoff’
** using an involved if statement - the calculated ‘pick’ field won’t save to the table
‘due_date’ : self.due_date.date,
‘investigator’ : self.investigator_dropdown.selected_value,
‘possible_solution’ :self.possible_solution_textbox.text
}

More context is definitely needed. How is the calculation triggered? Where is the save to the table happening? How are you saving to the table?

Is this something you can share a clone link to? If not of the original app, of a scaled down version just showing the issue? Seeing it all work (or not work!) in context helps a lot.

Hi

Here is a link to a cut down app. The problem is that when I edit the row on the datagrid, it won’t save the fields PICK and IPS to the database. It seems to work OK on adding New rows. Where do I need to refresh bindings please.

https://anvil.works/classic-build#app:4CHUIKUANDJLGGBR

That’s not a clone link, that’s a link to your IDE. Here’s a section in the docs on creating a clone link to an app: Anvil Docs | Cloning Apps

Sorry I seem to missed your last response. Thanks for pointing this out regarding cloning. In the meantime, I think I have solved it by adding self.item[‘column_name’] = self.ips_textbox.text etc on the change event,

Here is my attempt to fix the issue

Thanks for your help

You’re using write back data bindings, which only trigger when the field loses focus. Your workaround is a fine way to get around that for calculated fields.

1 Like

Many thanks for your help

Syd