Performing calculation on a field depending on user input.

I would like to have a dataset for making withdrawals and i would like every user to enter the amount to withdraw depending on the balance on a field. If the user inputs a value more than the balance return an error message.If the user inputs a value less than the balance, it should subtract that value and set the new balance.
Anyone with an idea how this can be done with a pseudo code, I will truly appreciate.

I would start with creating a collection with columns of the user ID and the amount he currently have .
Once the user clicks on a button to perform an action, I would query the collection to find the user in the collection. Once I find the relevant record, I would check whether the current amount is above the amount added (using if statement ). If it’s above, you can use the update function to update the record with the new value. Otherwise, you can show an error message.

I hope it’s clear.

Good luck,
Tal.

Thanks for your response. The problem comes when the the input value needs to be subtracted from the from the current amount to get the new amount and the Update function. Here is my code.
export function button1_click(event, $w) {
let val = $w(“#input9”).value;
console.log(val);
let item = $w(“#dataset2”).getCurrentItem();
console.log(item);
if ( val > item.field ) {
// I would like the to subtract the input8 from input9 and update input9 as the new accountbalance
}
}
I will appreciate if you will help modify the code.

Hi,
getCurrentItem function here is not relevant. You should query the collection and search for the userId.
After getting the relevant record, you should check:

if (val > item.amount ){
       let newAmount =  item.amount - val ;
       let toUpdate = { "_id": item._id , "amount": newAmount , "userId": item.userId };
       wixData.update("myCollection", toUpdate) ;
}

I recommend re-reading the answer above and use the documentation links as they have examples of how to use those functions. Note that the code depends on how you’ve set your collection (the fields names).

Good luck,
Tal.

Thanks Tal. I will try the code using the fiels i have on my db.

Tal, the code above does not subtract the two input8 as expected. Here is the full code.

import wixData from ‘wix-data’;
export function button300_click(event, $w) {
let val = $w(“#input9”).value;
console.log(val);
let item = $w(“#dataset5”).getCurrentItem();
console.log(item);
if ( val < item.field ) {
// I would like the to subtract the input8 from input9 and update input9 as the new accountbalance
}
if (val < item.accB ){
let newaccB = item.accB - val ;
let toUpdate = { “_id”: item._id , “accB”: newaccB , “userId”: item.userId };
wixData.update(“custdetails”, toUpdate) ;
}

}