cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Super User
Super User

How to set a field's value on the push of a button

As per title, I want to set a field's value (a datacard in a form) to a value I have calculated on the push of a button.

 

Scenario:

This is an app for taking menu orders. First, the OrderHeader is populated (Cust Name, time, discount, amount paid).

 

Next, the OrderDetail is populated (Menu Item, Quantity, OrderHeaderID).

 

Finally, when the customer is ready to pay, we open OrderHeader again in Edit mode. The screen allows for a discount to be selected. A label on the screen has a formula which calculates the total of the OrderDetail for the selected customer less any discount.

 

I want the user to be able to accept the calculated amount by clicking a button or type in a value manually if, for some reason, the amount paid does not match the calculated amount.

 

I have all this working except populating the field's value by clicking the button. Can anyone give me a hint how to proceed?

 

Cheers

Paul

1 ACCEPTED SOLUTION

Accepted Solutions
PowerApps Staff CarlosFigueira
PowerApps Staff

Re: How to set a field's value on the push of a button

If there is already a value in the field, what is the current value for the Default property of the text input? If it's something like Parent.Default, then you can set it to

Parent.Default * (1 - discount / 100)

Notice that the discount will be 0 originally, so it will be equivalent to Parent.Default. Only when the value of the 'discount' variable is changed (by pressing that button) that the default value will change, which will cause the value shown in the text input to be updated.

5 REPLIES 5
PowerApps Staff CarlosFigueira
PowerApps Staff

Re: How to set a field's value on the push of a button

You can have a variable that specifies the discount (in percentage) to be applied to the total that is initialized to 0 when the form first appears (e.g., in the screen's OnVisible property). In the "apply discount" button, you can set the value of the discount variable to whatever discount you want to give. Then you can have the Default property of the input text control within data card to use that variable. Something along the lines of the following properties below:

OrderHeaderScreen.OnVisible: UpdateContext({discount:0})
ApplyDiscountButton.OnSelect: UpdateContext({discount:Value(DiscountTextInput.Text)})
DataCardValueOrderTotal.Default: Text(calculatedTotal * (1 - discount / 100))

Where 'calculatedTotal' is the value that you already have that contains the current total, and 'DiscountTextInput' is a control where the user will enter the discount amount (if the discount is entered in another way you'll need to change that).

 

When the button is selected, the value of the 'discount' variable will be changed (assuming that the discount is different than 0), so the default value of the text input that holds the order total will be updated - but it will still be editable by the user if they want to do so.

Super User
Super User

Re: How to set a field's value on the push of a button

Thanks @CarlosFigueira 

 

As per the workflow, the Amount Paid is populated as zero when the record is initially created. It is later when we edit the record that I want the press-able button. Your code is setting the Default property, but we already have a value in the field, so presumably, this will have no effect?

 

PowerApps Staff CarlosFigueira
PowerApps Staff

Re: How to set a field's value on the push of a button

If there is already a value in the field, what is the current value for the Default property of the text input? If it's something like Parent.Default, then you can set it to

Parent.Default * (1 - discount / 100)

Notice that the discount will be 0 originally, so it will be equivalent to Parent.Default. Only when the value of the 'discount' variable is changed (by pressing that button) that the default value will change, which will cause the value shown in the text input to be updated.

Super User
Super User

Re: How to set a field's value on the push of a button

Thanks @CarlosFigueira

 

Finally got back to this requirement and your tips got me there. What I needed was: to see the value already recorded for the amount paid,

* In the PAID box, see the value already paid

* Have a PAY IN FULL button that changes the value of the PAID box to the AMOUNT DUE (already calculated in a control on the screen)

 

What I did was to set the DEFAULT of the PAID box to a variable as suggested. When the form opens (IsVisible) populate the variable with the current PAID value. When the PAY IN FULL button is pressed, the value of the variable is set to the AMOUNT DUE.

 

Seems a bit convoluted just to set a text box's value, but seems to be working for now - thanks!

MK1
Level: Powered On

Re: How to set a field's value on the push of a button

Hi

In my scenario,

When we try to submit a new request to Add and Remove User.

I have two buttons, Add and Remove on my edit screen.

When we click on Add button ABC datacardvalue should be null.

when we click on Remove button ABC datacardvalue should be "Remove".

And the Datacardvalue should set to null once the form is submitted.

Any ideas how to fix this would help.

 

Thanks,

Mk