Coded form field with number doesn't save to collection

Hi!
I use code to set an input field value to that of another input field.

In preview and on the live site it all works perfectly:

When input A is filled out, input B is filled out with the value of input A.

However, the value of input B never gets saved to the collection - it’s just blank in the database. No matter how I’ve tried to convert string to number, it never saves.

I’m using this code:

export function inputAmount_change(event) { var amount = parseFloat($w(“#inputAmount”).value) $w(“#inputRevenueItem1Amount”).value = amount; }

Any ideas out there?

Going by your post above, it sounds like you are just using code to set a field value, so that is the reason why it is not being saved into your dataset.

Using code to add a value to a input does not make the input trigger that there has been a change to that input and so needs saving, whereas if a user themselves added something to the input it would.

See here for more info about using code to set field values in a dataset.
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#setFieldValue

Setting the value of a field in a dataset item does not immediately set that value in the collection that the dataset is connected to. You still need to call the dataset save() function or any other function that performs a save to have the new value reflecting in the collection.

Hi Gos!

No of course I am running a save() command :slight_smile: the field saves to the dataset if I have entered the value myself, but if it entered via code it does not save.

Can you share the actual code which is being used for the save/insert/update operation.

Hi Shan!

Sure!
I have a button that calls this function:

export function SubmitApplication(event) {
$w(“#checkboxSubmit”).checked = true ;
validateForm(“submit”)
$w(‘#dataset1’).save();

}

Hi again!

Thanks for all you help guys.

i did an extra read through of https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#setFieldValue , and I finally understood what the problem was.

I did an extra read-through of with code, you need to update the dataset too, otherwise, it won’t be regarded at all.

So my final code looks something like this:

export function inputAmount_change(event) {
var amount = parseFloat($w(“#inputAmount”).value)
$w(“#inputRevenueItem1Amount”).value = amount;
}

And then in the beforeSave for the dataset I added this:

$w(“#dataset1”).onBeforeSave( () => {
$w(“#dataset1”).setFieldValue(“revenueItem1Amount”, Number($w(‘#inputRevenueItem1Amount’).value));
}

Now it works!

I’ve been looking for this solution for awhile. Just to clarify for others looking for this: I was trying to use a string to calculate a formula, then have the answer sent to the collection.

This worked https://www.wix.com/velo/reference/wix-dataset/dataset/setfieldvalues

BUT I was missing the element of saving the dataset.

I had a button that did the calculation 1st, then when it calculates it shows a hidden button that allows submission to the collection.

This is what worked for me:

export function calculatebutton_click(event) {
let Bombay = Number($w( ‘#input13’ ).value);
let Ciroc = Number($w( ‘#input14’ ).value);
let Mermaid = Number($w( ‘#input15’ ).value);
let Captain = Number($w( ‘#input16’ ).value);
let Grey = Number($w( ‘#input17’ ).value);
let Haig = Number($w( ‘#input18’ ).value);
let Bacardi = Number($w( ‘#input19’ ).value);
let Absolut = Number($w( ‘#input20’ ).value);
let Credits = String(Bombay* 2 +Ciroc* 4 +Mermaid* 5 +Captain* 2 +Grey* 3 +Haig* 2 +Bacardi+Absolut* 2 );
let Bottles = String(Bombay+Ciroc+Mermaid+Captain+Grey+Haig+Bacardi+Absolut);

$w( '#text51' ).text = Credits; 
$w( '#text52' ).text = Bottles; 

$w( “#dataset1” ).setFieldValues( {
“credits” : Credits,
“totalBottles” : Bottles
} );
$w( ‘#submitbutton’ ).expand();
}

export function submitbutton_click(event) {
$w( “#dataset1” ).save();
}