Button Set Field Value Script

Trying to have a button set the value of a field based on multiple true/false conditions on the form. The script appears to run correctly as shown in the attached image. But it does not put any value in the targeted field. Basically the user identifies the regular price of the item, selects if it was on sale, the sale price, if there was any discount, the discount amount. The user then clicks on the button to set the Purchase Price.

The following is the script I have:

The 'Edit click handler' is set as follows:

  • Action = Control component
  • Component = purchase_ticket_price (Number Input)
  • Method = Set value
  • Value = if ( {{ on_sale.value }} = true & {{ discount.value }} = true) { {{ price_sale.value }} - {{ discount_amount.value }}; }
    else if ( {{ on_sale.value }} = true & {{ discount.value }} = false ) { {{ price_sale.value }};}
    else if ( {{ on_sale.value }} = false & {{ discount.value }} = true ) { {{ price_regular.value }} - {{ discount_amount.value }}; }
    else { {{ price_regular.value }}; }

This is what is shown in the box below the script:
"if ( true = true & true = true) { 90 - 3; } else if ( true = true & true = false ) { 90;} else if ( true = false & true = true) { 103 - 3; } else { 103; }"

Advance Options are empty.

If I change the Component to a Text Input then it inserts the 'if (true =...' line but not the value.

I figure I'm likely missing something or did something wrong.
Help!
Please!

You need to update your code a bit; you are using = when you should use ==.

Instead of setting the value of a component from the button, have the button trigger a query that executes the code and sets the value of the target:

The code is:

let val;

if ( on_sale.value == true & discount.value == true) { 
  val = price_sale.value - discount_amount.value; }
else if ( on_sale.value == true & discount.value == false ) {
  val = price_sale.value;}
else if ( on_sale.value == false & discount.value == true ) {
  val = price_regular.value - discount_amount.value; }
else { val = price_regular.value; }

purchase_ticket_price.setValue(val)

Couple notes: in my example, I used dropdowns where I used the value of {{Boolean(true)}} and displayed "TRUE" in the sheet. If your true/false sources are text, you need to quote the "true" and "false" comparisons since you would be passing strings, not booleans.

2 Likes

Greatly Appreciated !
It worked beautifully.