Skip to main content
Participant
October 9, 2024
Answered

JavaScript Newbie Trying to Figure out an If/Then/Else depending on if a Field is Blank

  • October 9, 2024
  • 2 replies
  • 1709 views

Hi All!

 

So I've been googling, but I think I don't fully understand how to tell javascript what I want to do.

I'm trying to get a total in Field4 based on three other fields, Field1, Field2, Field3.

 

If Field 3 is blank, I want the minimum of Filed 1 and Field 2. Otherwise, I want the maximum.

var a = this.getField("Field1");
var b = this.getField("Field2");
var c = this.getField("Field3");

if (c==="") {
Math.min(a,b);
} else {
Math.max(a,b);
}

When I put that in, it gives me 0. For reference, Field1 = 7, Field2 = 9, and Field3 is blank on my form. So I'd be hoping the calculation would show 7, and would change to a 9, if I put a "1" in Field3.

I know what I'm missing has got to be simple or an inherent misunderstanding of javascirpt, but I can't for the life of me figure it out.

 

 

This topic has been closed for replies.
Correct answer Nesa Nurani

Here's what the Java Script Editor for the field looks like


You also need to assign min and max to event field as pointed by  @PDF Automation Station 

2 replies

PDF Automation Station
Community Expert
Community Expert
October 9, 2024

this.getField("Field1") calls the field object.  You need to call the value of the field object like this:

this.getField("Field1").value

Nesa Nurani
Community Expert
Community Expert
October 9, 2024

Use like this:

var a = this.getField("Field1").value;

var b = this.getField("Field2").value;
var c = this.getField("Field3").value;

if (c==="") {
Math.min(a,b);
} else {
Math.max(a,b);
}

 

if you use like this:

var a = this.getField("Field1");
var b = this.getField("Field2");
var c = this.getField("Field3");

then need to add values like this:

if (c.value==="") {
Math.min(a.value,b.value);
} else {
Math.max(a.value,b.value);
}

Participant
October 9, 2024

Thank you so much!

 

I pasted your first example, and changed out the actual field names in my file (pasting them as they are in my file this time, in case that's part of the problem)

var a = this.getField("SPI").value;
var b = this.getField("WLL").value;
var c = this.getField("Rank1").value;

if (c==="") {
Math.min(a,b);
} else {
Math.max(a,b);
}

The field calculates blank, now. 
SPI=7 on my form (Field1)
WLL = 9 on my form (Field2)
Rank1 = whether it's blank or has a number in it, the field (Field3)
ART = Regardless of whether Rank1 is blank, 0, or 1, ART is staying blank (Field4)

 

Is making Rank1 a value somehow negating that it's blank in javascript?

Participant
October 9, 2024

Here's what the Java Script Editor for the field looks like