Populate text field from button group AND another text field
Copy link to clipboard
Copied
Yet another road block for me! I need to have my form auto populate a text field. The issue I am having is that it needs to be populated from a variety of buttons, check boxes and other text fields. I know how to do that if it just buttons and check boxes, but not how to add in the text fields. I'm also hoping to add in some permanent words if something is typed into the text fields. For example:
1. "Shortage" is chosen from the Group1 buttons
2. 9/2023 is inserted in the "Bill period" text field
3. $100.00 is inserted in the "Short amt" text field
4. "Late payment fee" is chosen from the Group2 buttons
I want the result text to say (not the colors, but colors are the tie-ins): "Shortage for 9/2023 bill period in the amount of $100.00 due to late payment fees."
Below is my form:
Below is my current script for the completed text box:
event.value = "";
var v1 = this.getField("Group1").valueAsString;
if(v1 == "Choice1"){
event.value += "BILL PERIOD balanced to zero, no O/S. ";
}
if(v1 == "Choice2"){
event.value += "Zero hours reported for BILL PERIOD. ";
}
if(v1 == "Choice3"){
event.value += "Shortage for ";
}
if(v1 == "Choice4"){
event.value += "Overage for ";
}
Beyond this, I am a little stumped as to how to include the text fields. I also don't want the person completing the form to have to type "bill period in the amount of" after the date. I just want them to enter the date and the rest will be inserted automatically because a date has been entered in the field. Thanks all for your help!
Copy link to clipboard
Copied
event.value += "Shortage for " + this.getField("Bill period").value + " bill period in the amount of $" + this.getField("Short amt").value + " due to late payment fees.";
Copy link to clipboard
Copied
Getting closer, but I don't want the words to show up in the text box UNLESS something is typed in the "Bill period" and "Short amt" fields. The below is also problematic as it no longer shows the result of Group1, the "Bill period", the "Short amt", but does show the reason chosen from Group2:
event.value = "";
var v1 = this.getField("Group1").valueAsString;
if(v1 == "Choice1"){
event.value += "BILL PERIOD balanced to zero, no O/S. ";
}
if(v1 == "Choice2"){
event.value += "Zero hours reported for BILL PERIOD. ";
}
if(v1 == "Choice3"){
event.value += "Shortage for ";
}
if(v1 == "Choice4"){
event.value += "Overage for ";
}
event.value += this.getField("Bill period").value + " bill period in the amount of $" + this.getField("Short amt").value;
event.value = "";
var v2 = this.getField("Group2").valueAsString;
if(v2 == "Choice1"){
event.value += "due to employer miscalculation. ";}
if(v2 == "Choice2"){
event.value += "due to individual employee rate change. ";}
if(v2 == "Choice3"){
event.value += "due to late payment fees. ";}
Copy link to clipboard
Copied
You need to include it in the condition where v1 == "Choice3"...
So if these fields are empty, and Choice3 is selected, you want the calculated field to be empty?
Copy link to clipboard
Copied
Not quite. Specifically I want the resulting text field to be empty unless something is either toggled or entered into the other text fields, I simplified the code below:
event.value = "";
var v1 = this.getField("Group1").valueAsString;
if(v1 == "Choice3"){
event.value += "Shortage for ";
}
event.value += this.getField("Bill period").value + " bill period in the amount of $" + this.getField("Short amt").value;
var v2 = this.getField("Group2").valueAsString;
if(v2 == "Choice3"){
event.value += "due to late payment fees. ";}
If "Shortage" is selected from Group1, then "A shortage for" is populated.
IF a date is entered into "Bill period", then "DATE bill period in the amount of $" is populated.
IF an amount is entered into "Short amt", then "AMOUNT" is populated.
If "Late payment fee" is selected from Group2, then "due to late payment fees" is populated.
So, I want the populated text box to be empty UNLESS something is toggled or entered in the other text boxes.
Copy link to clipboard
Copied
Use this, field should be empty until both text fields have value and both radio buttons are selected "Choice3":
var v1 = this.getField("Group1").valueAsString;
var v2 = this.getField("Group2").valueAsString;
var v3 = this.getField("Bill period").valueAsString;
var v4 = this.getField("Short amt").valueAsString;
event.value = (v1 === "Choice3" && v2 === "Choice3" && v3 !== "" && v4 !== "") ?
"Shortage for " +v3+ " bill period in the amount of $" +v4+ " due to late payment fees." : "";
Copy link to clipboard
Copied
Thank you @Nesa Nurani, that worked beautifully. Now, how would I add multiple choices to this code? What if they choose Choice4? Also, not all choices are in a group, some are regular check boxes. Thank you again!
Copy link to clipboard
Copied
Can you explain what exactly you wish to achieve?
Copy link to clipboard
Copied
@Nesa Nurani Please see my original post.
Copy link to clipboard
Copied
From what I can see in your original post script already do that.
Now, if you wish to add more conditions, you will have to use statements 'if' and 'else if', here is an example:
if (v1 === "Choice3" && v2 === "Choice3" && v3 !== "" && v4 !== "")
event.value = "Shortage for " +v3+ " bill period in the amount of $" +v4+ " due to late payment fees.";
else if(v1 === "Choice4" && v2 === "Choice4")
event.value = "Shortage for " +v3+ " bill period in the amount of $" +v4+ " due to late payment fees.";
else
event.value = "";
To help you further, I would need to know exactly what you want to do.
Copy link to clipboard
Copied
@Nesa Nurani getting closer...top part works except that I keep getting a NaN in the result instead of the text entered in for v1. Red portion isn't working at all. Keeps telling me there is a syntax error. I'm sorry this is so frustrating, I am not a coder.
event.value = "";
var v1 = this.getField("Bill period1").valueAsString;
var v2 = this.getField("Group1").valueAsString;
var v3 = this.getField("Short amt1").valueAsString;
var v4 = this.getField("Group2").valueAsString;
if(v2 == "Choice1"){
event.value += +v1+ " balanced to zero, no O/S. ";
}
else if(v2 == "Choice2"){
event.value += +v1+ " zero hours reported. ";
}
else if(v2 == "Choice3"){
event.value += "Shortage for " +v1+ " bill period in the amount of " +v3+ " due to " +v4+;
}
Copy link to clipboard
Copied
You have too many plus signs, remove ones before v1 variable +v1+.
In the last line, there is no need for last plus sign +v4+

