Skip to main content
SamZFD
Inspiring
April 23, 2020
Answered

If/Then Java Script to Populate Fields based on one field's value

  • April 23, 2020
  • 2 replies
  • 1728 views

Hi! I hope someone might help me.

 

I have this script but it doesn't seem to run. I have no errors popping up.

 

I'm trying to get my form to pull through the details of the package based on the unit selection, i.e. 50 units is $75 and another field has an overage rate and another field has a billing code. They're all connected, but I want them in individual fields. It's quite possible it's better to use a different formula since ultimately I want to populate multiple fields, but this looked like it should accomplish my goal if I wrote a script for each field I wanted to populate. I'm new to this and got stuck on my first one.

 

All fields are drop-down menus currently. I'm entering my script in the "Rate" field properties, Actions, Mouse Up, Run a JavaScript.

var Units = this.getField("Units").value;
if (Units=="50") event.value = 75;
else if (Units=="100") event.value = 119;
else if (Units=="150") event.value = 125;
else event.value = "False"

I'd like to have $ signs also, but when it didn't work I took them out.

I set the "Units" field to "Commit selected value immediately" under the options.

I appreciate any advice!

This topic has been closed for replies.
Correct answer SamZFD

that's great! 

 

I am also learning javascripting, can you share the finalized code to see how made it work ?


For the text or the symbol, I entered it with quotes. There was really nothing wrong with the script itself originally technically, but this allowed me use text values for the output:

 

var Units = this.getField("Units").value;
if (Units=="50") event.value = "$75";
else if (Units=="100") event.value = "$119";
else if (Units=="150") event.value = "$125";
else event.value = "False"

2 replies

try67
Community Expert
Community Expert
April 23, 2020

That code needs to be the Calculation script of your field, not a MouseUp script.

Also, you can't use the Number formatting option if some of your values are not numbers... You would need to either add the currency symbol manually, as a string, or write your own custom Format script to do it for you, when the value is a number.

SamZFD
SamZFDAuthor
Inspiring
April 23, 2020

Thank you so much for your help! This works for the #'s and the $ I can work out for the rate.

But the plan codes have letters in them. Is there a way to have a number value input and a text output?

SamZFD
SamZFDAuthor
Inspiring
April 23, 2020

Got it to read the text using quotes around it "A-01". Thank you!!

ls_rbls
Community Expert
Community Expert
April 23, 2020

Hit the CTRL+J keys in your keyboard to open the debugger console and see what errors do you get.