Performing database calculations

Hello guys, I have a payment system with account balance field i a database. I would like a condition that allows users to withdraw amount that is less than the account balance and if the input value is less than account balance it should subtract the value from account balance and save the result as new balance!
Can anyone please help?

Hey
What does your Data Collection look like? What fields do you have to use for this calculation? Screenshots help if possible. It also depends on if you use a Dataset or if you use just Wix-Data to query and work with data.

If you use a data set you can get the currentItem you have on page and then use the setFieldValue to alter values in data collection fields. Then you can use the save method on the data set to save back the modified information to the data collection.

So if you use data set, read the API docs and look at the setFieldValue and Save operations and you will see a lot on how to accomplish this.

Then you must check if one value is less than the other and to do that you need the fields to be Number or you have convert the contents to float or number before doing the check.

If (field1 < field2) {
// They have valid resources in account
} else {
// The don’t have the resources to withdraw this amount
}

You can use parseFloat on the fields to convert and check if float or Number(value) to make sure it is a number. It will be crucial when saving back the resulsts you save it in the correct format, string or Number.

I have a dataset that connects the fields and I would like to have a code that checks if there is enough balance(AccountB) and if there is enough balnce it deducts the input value(Withdrawals) from balance and save the new value as current balance.

Is your problem solved? I have same