Skip to main content
Participating Frequently
July 2, 2023
Answered

Need to assign letters to text boxes, rank values, and put letters in text fields.

  • July 2, 2023
  • 2 replies
  • 1193 views

Hey Peeps, 

 

I have come to this wonderful community many times for answers with great success. I have another one for you. 

 

I'm working on an assessment PDF with 6 categories. Each of these categories have it own letters that the assessment selections can add up to when people click a checkbox. On the left and right column the letters each add up and populate at the bottom of the columns and then a line below that, there is the grand total for each corresponding letter. These grand totals also populate in text fields Total, Total_2, Total_3, Total_4, Total_5, and Total_6 I'm attaching the document for you to all see.

 

What I need to do is:
1) Have each of the corresponding text fields (Total, Total_2, Total_3, Total_4, Total_5, Total_6) have their own letter values (in this case each corresponding text box would go as follows: RIASEC).

2) Then I need to be able to have the top 3 highest values from the 6 text fields (Total, Total_2, Total_3, Total_4, Total_5, Total_6) to then be determined.

3) From those 3 totals, the corresponding letters (RIASEC) for the highest values will then auto-fill into the bottom 3 boxes with the top ranking letter going into the text field "MY INTEREST CODE", the second ranking in text field "undefined", and the third ranking in text field "undefined_2". 

 

Thank you so much in advance!

Correct answer try67

It's easier to do such things when the fields are named consistently. I renamed the first total field as "Total_1" and the last two "interest code" fields as "MY INTEREST CODE_2" and "MY INTEREST CODE_3" and then used the following code as the custom calculation script for "MY INTEREST CODE_1":

 

 

var letters = "RIASEC";
var totals = [];
for (var i=1; i<=6; i++) {
	totals.push({letter: letters.charAt(i-1), total: Number(this.getField("Total_"+i).valueAsString)});
}
totals.sort(sortTotals);
event.value = totals[0].letter;
this.getField("MY INTEREST CODE_2").value = totals[1].letter;
this.getField("MY INTEREST CODE_3").value = totals[2].letter;

function sortTotals(a,b) {
	return b.total-a.total;
}

 

 

The edited file is also attached.

2 replies

Participant
January 30, 2025

 

Hi @Alyssa5FBC ,

You can achieve this with JavaScript in your PDF form. Assign each total field (Total, Total_2, etc.) its corresponding letter (RIASEC). Then, sort the values in descending order and populate the top three into "MY INTEREST CODE," "undefined," and "undefined_2." Use JavaScript like below and learn from this site:

 

javascript
var scores = [ { letter: "R", value: this.getField("Total").value }, { letter: "I", value: this.getField("Total_2").value }, { letter: "A", value: this.getField("Total_3").value }, { letter: "S", value: this.getField("Total_4").value }, { letter: "E", value: this.getField("Total_5").value }, { letter: "C", value: this.getField("Total_6").value } ]; scores.sort((a, b) => b.value - a.value); this.getField("MY INTEREST CODE").value = scores[0].letter; this.getField("undefined").value = scores[1].letter; this.getField("undefined_2").value = scores[2].letter;

This should work—😊

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
July 2, 2023

It's easier to do such things when the fields are named consistently. I renamed the first total field as "Total_1" and the last two "interest code" fields as "MY INTEREST CODE_2" and "MY INTEREST CODE_3" and then used the following code as the custom calculation script for "MY INTEREST CODE_1":

 

 

var letters = "RIASEC";
var totals = [];
for (var i=1; i<=6; i++) {
	totals.push({letter: letters.charAt(i-1), total: Number(this.getField("Total_"+i).valueAsString)});
}
totals.sort(sortTotals);
event.value = totals[0].letter;
this.getField("MY INTEREST CODE_2").value = totals[1].letter;
this.getField("MY INTEREST CODE_3").value = totals[2].letter;

function sortTotals(a,b) {
	return b.total-a.total;
}

 

 

The edited file is also attached.

Participating Frequently
July 3, 2023

Thank you so much for this! And I'm going to fan girl for a moment. Try67, you've helped me with so much code for the documents I've created. I appreciate you as a teacher and Community Expert.