Skip to main content
Known Participant
November 17, 2019
Answered

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

  • November 17, 2019
  • 2 replies
  • 1155 views

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 has been closed for replies.
Correct answer try67

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

2 replies

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
November 18, 2019

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

Known Participant
November 18, 2019

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!

try67
Community Expert
Community Expert
November 17, 2019

You have an error in your code.

Change this:

else if (B=A)

To:

else if (B==A)

Known Participant
November 17, 2019

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.

try67
Community Expert
Community Expert
November 17, 2019

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.