Skip to main content
Known Participant
Nov 17, 2019 7:03 AM UTC
Answered

Conditional if/then doesn't clear when data in related field(s) is deleted.

I have form of 12 rows, each with the following calculation in a field 'CD#Result' (the first two letters of the fields change per row).  "A" is a single field and "B" is a total of three other fields, that when compared to A determines what is in 'CD#Result'.  

 

It works almost perfectly.  However, I realized I have a problem if the user changes their mind.  If they make any entry in A or any of the three other fields and then delete those entries, the Result field does not clear.  I used the validation script to hide any 0 total in the 'CD#MoTotal' field to get a blank, but can't figure out how to do that in the Results field, or how to add to the calculation below.

 

What I want is that if both 'CD#Expect' and 'CD#MoTotal' are blank, then the 'CDResult' should be blank too.

TIA for your help!

 

var A = this.getField("CD#Expect").value; // Expected Activity for period reviewed;
var B = this.getField("CD#MoTotal").value; // Actual Activity for period reviewed;

if (B>A) {  event.value = "Exceeds";}
else if(B<A) {  event.value = "Under"; }
else if (B=A) {  event.value = "Meets";}

 

validate for 'CD#MoTotal':

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

This topic is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Nov 18, 2019 6:19 PM UTC

Use this code:

 

var A = this.getField("CD#Expect").valueAsString; // Expected Activity for period reviewed;
var B = this.getField("CD#MoTotal").valueAsString; // Actual Activity for period reviewed;

if (A=="" && B=="") event.value = "";
else {
	A = Number(A);
	B = Number(B);
	if (B>A) event.value = "Exceeds";
	else if (B<A) event.value = "Under";
	else if (B==A) event.value = "Meets";
}

8 replies

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
Nov 18, 2019 6:19 PM UTC

Use this code:

 

var A = this.getField("CD#Expect").valueAsString; // Expected Activity for period reviewed;
var B = this.getField("CD#MoTotal").valueAsString; // Actual Activity for period reviewed;

if (A=="" && B=="") event.value = "";
else {
	A = Number(A);
	B = Number(B);
	if (B>A) event.value = "Exceeds";
	else if (B<A) event.value = "Under";
	else if (B==A) event.value = "Meets";
}

Translate
Known Participant
Nov 18, 2019 7:01 PM UTC

Wow!  Thank you so much!!!.  Happy to have the correct formula rather than one that works at the moment, but may not under future/other circumstances!  Will certainly save this message for future forms that I am converting from Word templates to PDF form.  You Rock!

Translate
try67
Community Expert
Community Expert
Nov 17, 2019 8:43 AM UTC

You have an error in your code.

Change this:

else if (B=A)

To:

else if (B==A)

Translate
Known Participant
Nov 17, 2019 10:43 PM UTC

Hi Try67 - thanks for responding.  However, putting the double == in my formula didn't work the way I wanted.  Previously, using just the single = resulted in "Meets" when A and/or B had an entry and it met the condition.  If both the A & B were blank, it would say "Exceeds" as that is the first one listed  (I assume that is why).  When I changed it to == it kept the Meets even if both were blank. 

 

I found my answer in someone else's formula ........I kept the single =, but added another condition to the bottom of the calculation.................else event.value = "";

It was so simple, I'm mad at myself for not seeing it!  Now it works perfectly.

Translate
try67
Community Expert
Community Expert
Nov 17, 2019 10:57 PM UTC

No, it didn't. The way you had it before the value of A was assigned to B, not compared with it.

However, since they were both identical in that scenario you didn't notice it, but it was still wrong.

Translate