Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
Oct 09, 2024 Oct 09, 2024

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.

 

 

TOPICS
JavaScript , PDF , PDF forms
1.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
2 ACCEPTED SOLUTIONS
Community Expert ,
Oct 09, 2024 Oct 09, 2024

Try this:

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

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

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 09, 2024 Oct 09, 2024

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

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 09, 2024 Oct 09, 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);
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 09, 2024 Oct 09, 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 09, 2024 Oct 09, 2024

jeanne_3690_0-1728489413072.png

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 09, 2024 Oct 09, 2024

Try this:

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

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 09, 2024 Oct 09, 2024

Thank you so much! It made it work immediately!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 09, 2024 Oct 09, 2024

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 09, 2024 Oct 09, 2024
LATEST

Thank you so much! It would probably have been helpful if I'd shown a screenshot to begin with so you could see what "obvious bits" I was missing. 🙂 

I truly appreciate you looking over this for me!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 09, 2024 Oct 09, 2024

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

this.getField("Field1").value

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines