Copy link to clipboard
Copied
I am attempting to write a script that checks the value of a field and displays fixed text + the value. If the value is zero, it should display nothing and check the next field.
If there are multiple fields with a value greater than zero, it should display them with a comma separating them.
The purpose of this document is to allow someone to choose how many awards they have and then display them in a specific format.
For example:
If someone indicates that they have an achievement award and two commendations, it should display as "UTACH-1, UTCOM-2". If they have zero achievement awards but two commendations, it'll display as "UTCOM-2".
The issue I'm running into is that there are 44 total awards so I have to come up with an eloquent solution rather than just do a bunch of "if else" commands.
Copy link to clipboard
Copied
Use this:
var medal = ["MOVSM-", "CGACH-", "AFAM-", "NAM-", "NAMV-", "AAM-", "JSAM-","CGCM-", "AFCM-", "NCM-", "NCCDV-", "ARCOM-V-", "ARCOM-", "JSCM-","JSCMV-", "AFAAM-", "AM-V-", "AM-", "MSM-", "DMSM-", "BSM-V-","BSM-", "ASM-", "CGM-", "NMCM-", "SM-", "DFC-", "LM-", "DSSM-","SS-", "CGDSM-", "DSM-", "DDSM-", "AFC-", "NC-", "DSC-", "MH-","UTACH-", "UTCOM-", "UTJCM-", "UTMOM-", "UTJMM-", "UTCRS-", "UTMOV-"];
var score = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var str = [];
for (var i = 1; i <= 44; i++) {
var newScore = Number(this.getField("Text"+i).valueAsString);
if(newScore !== 0){
score[i-1] = newScore;}}
for(var j=0; j<score.length; j++){
if(score[j] !== 0)
str.push(medal[j]+score[j]);}
event.value = str.join(",");
Copy link to clipboard
Copied
After looking at this more closely, You're best strategy is to place the display text in the tooltip text.
var aAwards = [];
this.getField("Award").getArray().forEach(function(oFld){if(oFld.value != 0) aAwards .push(oFld.userName + "-" + oFld.value);});
event.value = aAwards.join(",");
Note that the script is generic and provides excellent separation between the number of fields, text, and the behavior.
You can add new fields and change the display text without changing the script.
Copy link to clipboard
Copied
I do have this script that does work but I'd very much like to avoid the headache of writing out all of the "if else" commands I was talking about.
var a = this.getField("Text38").value;
var b = this.getField("Text39").value;
if (a == "0")
{
event.value = ""
} else {
event.value = "UTACH-" + a + ", " + "UTCOM-" + b
}
Copy link to clipboard
Copied
Name all of the award fields "Award.Field1", "Award.Field2", etc.
Then this calculation script will collect all the data
var aAwards = [];
this.getField("Award").getArray().forEach(function(oFld){if(oFld.value != 0) aWards.push("UTACH-" + oFld.value);});
event.value = aAwards.join(",");
This is nearly as elegant as you can get.
You did not provide a way to differentiation between the "UTACH" and "UTCOM". How were you planning on doing this?
Copy link to clipboard
Copied
That is a great question, sir! I am still trying to wrap my head around JavaScript. I have attached the document I am trying to create in order to help illustrate what I'm working with.
Thank you for the help with the array. I've been combing through the Adobe JavaScript Reference to try and get something working here.
Copy link to clipboard
Copied
You could put the Name you want displayed with the number in either the name of the field, or the tooltip for the field.
But you need to explain more.
Is the script counting the total number of awards and comendations? or is it displaying the individual award or comendation selected?
Copy link to clipboard
Copied
After looking at this more closely, You're best strategy is to place the display text in the tooltip text.
var aAwards = [];
this.getField("Award").getArray().forEach(function(oFld){if(oFld.value != 0) aAwards .push(oFld.userName + "-" + oFld.value);});
event.value = aAwards.join(",");
Note that the script is generic and provides excellent separation between the number of fields, text, and the behavior.
You can add new fields and change the display text without changing the script.
Copy link to clipboard
Copied
Thank you so much!
Copy link to clipboard
Copied
You are welcome.
Copy link to clipboard
Copied
Use this:
var medal = ["MOVSM-", "CGACH-", "AFAM-", "NAM-", "NAMV-", "AAM-", "JSAM-","CGCM-", "AFCM-", "NCM-", "NCCDV-", "ARCOM-V-", "ARCOM-", "JSCM-","JSCMV-", "AFAAM-", "AM-V-", "AM-", "MSM-", "DMSM-", "BSM-V-","BSM-", "ASM-", "CGM-", "NMCM-", "SM-", "DFC-", "LM-", "DSSM-","SS-", "CGDSM-", "DSM-", "DDSM-", "AFC-", "NC-", "DSC-", "MH-","UTACH-", "UTCOM-", "UTJCM-", "UTMOM-", "UTJMM-", "UTCRS-", "UTMOV-"];
var score = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var str = [];
for (var i = 1; i <= 44; i++) {
var newScore = Number(this.getField("Text"+i).valueAsString);
if(newScore !== 0){
score[i-1] = newScore;}}
for(var j=0; j<score.length; j++){
if(score[j] !== 0)
str.push(medal[j]+score[j]);}
event.value = str.join(",");
Copy link to clipboard
Copied
Thank you! This worked perfectly!
Copy link to clipboard
Copied
Hello ma'am! Is there a way to have it display "NO AWARDS" if all text boxes are 0?
Copy link to clipboard
Copied
Yes, there are a couple of ways to do this. And don't call me Ma'am
Use one, and only one, of the following methods. Do not use both.
1. If you don't want the actual field value to be affected by no awards being selected, then use a custom format script
if(event.value == "") event.value = "NO AWARDS";
A format script changes the text displayed to the user, but does not change the actual field value.
2. Just modify the custom calculation srcript.
var aAwards = [];
this.getField("Award").getArray().forEach(function(oFld){if(oFld.value != 0) aAwards .push(oFld.userName + "-" + oFld.value);});
if(aWards.length == 0)
event.value = "NO AWARDS";
else
event.value = aAwards.join(",");
Copy link to clipboard
Copied
I apologize! I was referring to Nesa Nurani. Thank you for the help again!
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more