Copy link to clipboard
Copied
Hello,
I have a form with 5 fields that are drop down boxes. There are 4 choices: Blank (default), Y, N, N/A. I have two fields that produce a calculation. One returns '# of #' and the other produces a % of Y's.
In the default state of blank, the calculations are '0 of 0' and '0%'. The problem is that, unfortunately, some providers will want a printed hardcopy to fill out and the fields have these zero's in them.
How can I make the field so blank? I have tried several variations to no avail.
1st script '0 of 0':
var totalY = 0;
var total = 0;
for (var i=1; i<=5; i++) {
var f = this.getField("JJCRDropdown"+i);
if (f.valueAsString=="Y") {
totalY++;
total++;
} else if (f.valueAsString=="N") {
total++;
}
}
if (total==0) event.value = "";
else event.value = totalY / total;
2nd script '0%':
var totalY = 0;
var total = 0;
for (var i=1; i<=5; i++) {
var f = this.getField("JJCRDropdown"+i);
if (f.valueAsString=="Y") {
totalY++;
total++;
} else if (f.valueAsString=="N") {
total++;
}
}
if (total==0) event.value = "";
else event.value = totalY / total;
I think what would work is if I could say, but I can't find reference for this:
if (totalY==0 || total==0) AND (totalNA<5) event.value = "";
To do that use this code:
if ((totalY==0 || total==0)) && (totalNA<5)) event.value = "";
Copy link to clipboard
Copied
Change:
if (total==0) event.value = "";
To:
if (totalY==0 || total==0) event.value = "";
Copy link to clipboard
Copied
Thank you once again Try67, it works for the '0 of 0' field but not the %
Is there something else I have to do for that field. I know % outputs sort of screw some things up.
Copy link to clipboard
Copied
Have you looked at the "real" value of the field and not just the "formatted" displayed value?
To see the actual value of the calculated field try:
var totalY = 0;
var total = 0;
for (var i = 0; i < 6; i++) {
var f = this.getField("JJCRDropdown" + i);
if (f.valueAsString == "Y") {
totalY++;
total++;
} else if (f.valueAsString == "N") {
total++;
}
}
// display totals used in calculations;
console.show();
console.clear();
console.println("totalY: " + totalY);
console.println("total : " + total);
console.println("toatlY / total : " + (totalY / total));
// end display of values;
if (total == 0)
{
event.value = "";
}
else{
event.value = totalY / total;
}
You should see that the actual value of the field is a decimal value and not a "%". You need to set the format as needed by the value of the field. I do not think you want to use the "total" variable since if there is any "N" value you will have a none zero value for the "total".
I would set the format for the field to "Custom" then use a custom format script area to set the format for the field using a script like:
if(event.value == "" )
{
AFNumber_Format(2, 0, 2, 0, "", false);
}
else{
AFPercent_Format(2, 0, false);
}
Copy link to clipboard
Copied
Ok, I have to study that one a bit.
I had to adjust the event.values because once I plugged in the requested script, it would no longer recognize the totalNA script.
Why can't I get ALL of these to function. I can only get 2 of the 4 to work at a time.
if (totalNA==5) event.value = "N/A";
if (totalY==0 || total==0) event.value = "";
if (total==0) event.value = totalY+ " of " + total;
else event.value = totalY + " of " + total;
Copy link to clipboard
Copied
Think about the logic of your statements.
The last two statements cover all possible scenarios (either total is 0 or it's not), so they make the first two statements moot.
Copy link to clipboard
Copied
I get that, but this was what happened.
When I went from this:
if (totalNA==5) event.value = "N/A";
else event.value = totalY + " of " + total;
To this:
if (totalNA==5) event.value = "N/A";
if (totalY==0 || total==0) event.value = "";
else event.value = totalY + " of " + total;
the blank field worked but it negated the N/A.
I think what would work is if I could say, but I can't find reference for this:
if (totalY==0 || total==0) AND (totalNA<5) event.value = "";
Copy link to clipboard
Copied
I think what would work is if I could say, but I can't find reference for this:
if (totalY==0 || total==0) AND (totalNA<5) event.value = "";
To do that use this code:
if ((totalY==0 || total==0)) && (totalNA<5)) event.value = "";
Copy link to clipboard
Copied
Samples that others can review work much better than verbal descriptions.
You also have to have to clearly describe your problem. Form your other posts, it appears you need to count all the "Yes", "No", and ".N/
A" responses. You are calculating the percentage of "Yes" response of the total of the "Yes" and "No" responses.
Is it possible this method might work for you?
See example
I thought you wanted a result of "0" or "null" to appear as a null string without the percentage mark. Now it looks like you want that and some other value if the result is not zero or null.
Copy link to clipboard
Copied
I have two fields that calculate from the same 5 drop downs. The % field calculates from the '0 of 0' field. I want to count the yes's and no's and have it populate, but I only want it to say N/A if ALL 5 drop downs are N/A. If 1) <5 dropdowns are NA and the others are blank, I want both calc fields to be blank, 2) all drops are blank, then calc's are blank.
The scripts are different for each calc field due to the percentage.
Did that make sense?
My government securities would not allow the example link locations to be viewed.
Copy link to clipboard
Copied
I would calculate the fields individually since it is just to complex to parse the numbers form field displaying "# of #".
When all 5 fields have the "N/A" response, what do you want to display in percentage field?
I have provided you an approach for blanking the percentage result when the percentage is zero.
You can always provide an example form with just the fields needed for the calculations and the results of the calculations. That way you are not disclosing any .design, instructions,, country, or departmental information
Copy link to clipboard
Copied
I've got a similar situation. I'm creating a form where one field “cd” is calculated based on the inputs of three other fields. I don't want “cd” to calculate until all three of the other fields have been filled in by the user. I wrote a script so that does this, but it only works when the inputs in the other three fields are >0. Since we will regularly be entering 0 in the “ef” field I need it to perform the calculation when one of the inputs is zero, and to not perform the calculation when the field is blank. Any ideas on what I’m doing wrong?
Here’s a list of the Field Types. I’m wondering if it has something to do with the non-numeric values from my dropdown fields, of the way I’m using a space “ “ to create a blank default value for fields ow and vt.
And Here's my script:
var ef = this.getField("EstimatedFanCfmBath1").value;
var ow = this.getField("OppWindowBath1").value;
var vt = this.getField("VentTypeBath1").value;
if(ef=="" || ow==" " || vt ==" " ) {event.value="";}
else
if(ow=="No" && ef>=50) {event.value = 0;}
else
if(ow=="Yes" && ef+20>=50) {event.value = 0 ;}
else
if(ow=="No" && ef<50) {event.value = -50+ef;}
else
if(ow=="Yes" && ef<50) {event.value = -50+ef+20;}
Copy link to clipboard
Copied
Try this:
var ef = this.getField("EstimatedFanCfmBath1").valueAsString;
var ow = this.getField("OppWindowBath1").value;
var vt = this.getField("VentTypeBath1").value;
if (ef=="" || ow==" " || vt ==" " ) {event.value="";}
else {
ef = Number(ef);
if (ow=="No" && ef>=50) {event.value = 0;}
else if(ow=="Yes" && ef+20>=50) {event.value = 0 ;}
else if(ow=="No" && ef<50) {event.value = -50+ef;}
else if(ow=="Yes" && ef<50) {event.value = -50+ef+20;}
}
Copy link to clipboard
Copied
Thanks for helping a newby out. That worked perfectly