Skip to main content
Monteigo99
Inspiring
August 24, 2016
Answered

Return blank if zero

  • August 24, 2016
  • 2 replies
  • 3901 views

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;

This topic has been closed for replies.
Correct answer try67

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 = "";


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 = "";

2 replies

Participating Frequently
December 18, 2020

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.

  • cd: is a read only, calculated text field with the script below
  • ef: is a text field
  • ow: is a dropdown field with three items listed: The first is supposed to be blank, but the only way I could figure out how to make a blank was to hit the cursor, so the item and the export value are both “ “ a single space. Item two is “Yes” for both the Item name and the Export value, and item three is “No” for both as well. 
  • vt: is also a dropdown field with 4 items listed: The first items is supposed to be blank so I used the same “ “ single space. And the other three are all non numeric Items and outputs.  

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;}

try67
Community Expert
Community Expert
December 19, 2020

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;}
}
Participating Frequently
December 21, 2020

Thanks for helping a newby out. That worked perfectly

try67
Community Expert
Community Expert
August 24, 2016

Change:

if (total==0) event.value = "";

To:

if (totalY==0 || total==0) event.value = "";

Monteigo99
Inspiring
August 25, 2016

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.

Inspiring
August 25, 2016

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);

}