Skip to main content
Participant
February 17, 2023
Answered

Adding if/then javascript for expense report based on drop-down field

  • February 17, 2023
  • 1 reply
  • 1181 views

Hello, I am trying to create an expense report that uses a dropdown menu to choose the type of expense, and then a number field to indicate how much the expense was. I need help creating a sub-total of each type of expense. So for example, I have Dropdown 1-16 and AmountRow 1-16. I need the subtotal for Advertising Promotion for every time Dropdown= Advertising Promotion. I've attached the PDF below so you can see where it is I'm getting stuck. Thank you for your help.

This topic has been closed for replies.
Correct answer Nesa Nurani

Try this as custom calculation script of the field where you want to show subtotal:

var sub = 0;
for(var i=1; i<=16; i++){
if(this.getField("Dropdown"+i).valueAsString == "Advertising Promotion" && this.getField("AmountRow"+i).valueAsString != "")
sub += Number(this.getField("AmountRow"+i).valueAsString);}
event.value = sub;

1 reply

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
February 17, 2023

Try this as custom calculation script of the field where you want to show subtotal:

var sub = 0;
for(var i=1; i<=16; i++){
if(this.getField("Dropdown"+i).valueAsString == "Advertising Promotion" && this.getField("AmountRow"+i).valueAsString != "")
sub += Number(this.getField("AmountRow"+i).valueAsString);}
event.value = sub;
Participant
February 17, 2023

This worked perfectly, thank you.