Copy link to clipboard
Copied
Hi there
Relatively new to ADOBE forms and scripting.
I have an expense form that has 14 rows and each row has a uniquely identified dropdown (Category) with 6 different values: KM, Accommodations, Meals, Transportation, Other and empty value (" ").
Each dropdown name is obviously different: Cat1, Cat2,..., Cat14. The dropdown is set to Commit Selected Value Immediatelly.
I also have three other fields: Units (u1, u2,..., u14), Cost (c1, c2,..., c14) and Total Value (tv1,..., tv14). The Total Value = Unit*Cost (u1*c1,..., u14*c14).
The idea is that when I select KM, it should automatically set the Cost value to the value I defined (e.g. 0.40) and the Unit ("u1", etc) field is cleared.
When the selection changes to any other option other than KM, the Unit value (e.g. u1) changes to 1 and the cost gets cleared.
I am using the following calculation script for Cat1 dropdown:
if (event.value == "KM") {
this.getField("c1").value = this.getField("MileageCost").value; //MileageCost is another field that has a default value
this.getField("u1").value = "";
}
if (event.value !== "KM") {
this.getField("c1").value = "";
this.getField("u1").value = "1";
}
if (event.value == " ") {
this.getField("c1").value = "";
this.getField("u1").value = "";
}
For the second dropdown Cat2, I am using this calculation script
if (event.value == "KM") {
this.getField("c2").value = this.getField("MileageCost").value; //MileageCost is another field that has the a default value
this.getField("u2").value = "";
}
if (event.value !== "KM") {
this.getField("c2").value = "";
this.getField("u2").value = "1";
}
if (event.value == " ") {
this.getField("c2").value = "";
this.getField("u2").value = "";
}
All other dropdowns use a similar code, with the updated field names for the Unit and Cost
Let's say I am selecting KM in th efirst dropdown for Cat1. The u1 clears and c1 becomes 0.40. Then I enter the u1 value, let's say 100. The tv1 shows $40.00. All good so far
Then for Cat2 dropdown I select Meals.
The u2 populates the "1" and c2 field clears. I now enter a cost in the c2 field. Let's say 50.
This is the part I cannot figure it out yet... Once the cost value for c2 is entered, the Unit value of u1 gets erased. Similarly, if I enter 100 in the Units for u1, the Cost value of c2 gets erased.
I am not sure if this is triggered by the fact that all 14 dropdowns use the same values: KM, Meals, etc.
I thought the fact that each dropdown is uniquely identified will alow me to reach the results I wanted.
Copy link to clipboard
Copied
Calculation scripts run every time any field value changes. So if u1 is empty based on the dropdown selection of KM, and then you enter a value into the u1 field, it is always going to revert back to the calculated value based on the KM selection. The way to avoid this to either use a validation script instead - that only runs when the value of the field that contains the script runs, allowing for a different value to be entered, or set conditions in the calculation script so it only runs when the value of the source field of the calculation changes. These articles explain all of this:
https://pdfautomationstation.substack.com/p/calculation-vs-validation-scripts
https://pdfautomationstation.substack.com/p/calculation-vs-validation-scripts-eb5
https://pdfautomationstation.substack.com/p/another-method-for-calculation-vs
Also, there's an easier method in which you create the first row, add the script, then right-click > Create multiple copies to add the rest of the fields:
https://pdfautomationstation.substack.com/p/anticipating-field-names-in-custom
Copy link to clipboard
Copied
As mentioned by @PDF Automation Station any input by user in "u1" field is overwritten by calculation event, so you need to use script in 'Validate' tab.
Use a script in 'Validate' tab like this:
if (event.value == "KM") {
this.getField("c1").value = this.getField("MileageCost").value;
this.getField("u1").value = "";}
else if (event.value == " ") {
this.getField("c1").value = "";
this.getField("u1").value = "";}
else {
this.getField("c1").value = "";
this.getField("u1").value = "1";}