Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
8

Calculating the number of times an item is selected from dropdown lists in a PDF form

Community Beginner ,
Sep 12, 2023 Sep 12, 2023

I am creating a PDF form that has 25 drop-down lists consisting of the following identical options, including:

  • ‘Blank’ (just a single space so the field looks blank)
  • L - Lead
  • GRR - Galvanized Requiring Replacement
  • NL - Non-Lead
  • UNK - Unknown

On another part of the form, I have the same items listed with a text field beside it in a recap section at the end of the form:

Lead ______        GRR ________     NL ________      UNK ________

My goal is that each time a selection is made from one of the lists, the total number of times that selection is made (from all the lists combined) will be displayed in the corresponding recap section.

For example:

If ‘GRR’ is selected total of 8 different times in 8 of the 25 dropdown lists, it will read "8" in the recap section next to the GRR heading.

I am brand new to this – no JavaScript experience at all – so please spell it out for me, and if it would be possible for me to copy and paste the script, that would be AWESOME!

TOPICS
Create PDFs , Edit and convert PDFs , How to , JavaScript , PDF , PDF forms
1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Sep 12, 2023 Sep 12, 2023

Then use this:

var L = 0, GRR = 0, NL = 0, UNK = 0;

for(var i=1; i<=25; i++){
 var fieldName = "Material-Category-" + (i < 10 ? "0" + i : i);
 if(this.getField(fieldName).valueAsString == "L - Lead")L++;
 if(this.getField(fieldName).valueAsString == "GRR - Galvanized Requiring Replacement")GRR++;
 if(this.getField(fieldName).valueAsString == "NL - Non-Lead")NL++;
 if(this.getField(fieldName).valueAsString == "UNK - Unknown")UNK++;}

this.getField("L-total").value = L;
this.getField("GRR-total").value = GRR;
this.getField("NL-total").value = NL;
this.getField("UNK-total").value = UNK;

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 12, 2023 Sep 12, 2023

What are the name of the fields?

Are the values just 'L', 'GRR'...etc  or 'L - Lead', 'GRR - Galvanized Requiring Replacement'...etc?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 12, 2023 Sep 12, 2023

The receiving fields are L-total, GRR-total, NL-total, and UNK-total

Thanks!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 12, 2023 Sep 12, 2023

Ok, assuming dropdown fields are named "Dropdown1-25" use this in one of the fields as custom calculation script:

var L = 0, GRR = 0, NL = 0, UNK = 0;

for(var i=1; i<=25; i++){
 if(this.getField("Dropdown"+i).valueAsString == "L - Lead")L++;
 if(this.getField("Dropdown"+i).valueAsString == "GRR - Galvanized Requiring Replacement")GRR++;
 if(this.getField("Dropdown"+i).valueAsString == "NL - Non-Lead")NL++;
 if(this.getField("Dropdown"+i).valueAsString == "UNK - Unknown")UNK++;}

this.getField("L-total").value = L;
this.getField("GRR-total").value = GRR;
this.getField("NL-total").value = NL;
this.getField("UNK-total").value = UNK;
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 12, 2023 Sep 12, 2023

Thank you.  I think I follow...  Please forgive my uncertainty; if you could confirm:

The code you provided should be copied into the Custom Calculation Script field of the fields where the totals should appear.  So, for the first field - L-total - I should use this piece of script:

var L = 0, GRR = 0, NL = 0, UNK = 0;

for(var i=1; i<=25; i++){
 if(this.getField("Dropdown"+i).valueAsString == "L - Lead")L++;
 if(this.getField("Dropdown"+i).valueAsString == "GRR - Galvanized Requiring Replacement")GRR++;
 if(this.getField("Dropdown"+i).valueAsString == "NL - Non-Lead")NL++;
 if(this.getField("Dropdown"+i).valueAsString == "UNK - Unknown")UNK++;}

this.getField("L-total").value = L;

In the code, for each instance of "Dropdown", do I need to have a line for each dropdown list, identifying each one (i.e., Material-Category-01, Material-Category-02, etc.)?  Sorry if I'm being dumb or making it more complicated than it is.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 12, 2023 Sep 12, 2023

Not in every field, it is enough if you use script in only one field.

It is important to name fields correctly, and since you named dropdowns 01,02...etc at number 10 and above are you naming your fields 10, 11, 12 or 010, 011, 012?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 12, 2023 Sep 12, 2023

Thank you!

The dropdown list names all have two-digits: Material-Category-01 thru Material-Category-25

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 12, 2023 Sep 12, 2023

Then use this:

var L = 0, GRR = 0, NL = 0, UNK = 0;

for(var i=1; i<=25; i++){
 var fieldName = "Material-Category-" + (i < 10 ? "0" + i : i);
 if(this.getField(fieldName).valueAsString == "L - Lead")L++;
 if(this.getField(fieldName).valueAsString == "GRR - Galvanized Requiring Replacement")GRR++;
 if(this.getField(fieldName).valueAsString == "NL - Non-Lead")NL++;
 if(this.getField(fieldName).valueAsString == "UNK - Unknown")UNK++;}

this.getField("L-total").value = L;
this.getField("GRR-total").value = GRR;
this.getField("NL-total").value = NL;
this.getField("UNK-total").value = UNK;
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 12, 2023 Sep 12, 2023
LATEST

This is perfect and worked like a charm.  Thank you so much for all your help!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines