Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Looking for javascript that compares two values and returns value of zero if first is >= 2d

Community Beginner ,
Aug 10, 2021 Aug 10, 2021

Copy link to clipboard

Copied

I wish to compare the values of two fields that are calculated based upon the sum of other fields like this:

sum1=fieldx +fieldy+fieldz

sum2=fielda+fieldb+fieldc

if sum1>sum2 then sum2 should display a 0 and if sum2 displays a 0, then fielda+fieldb+fieldc should become invisible only to the user, but still be there since it is a part of theabove equation.  so far I have this, but this does not even insert 0 into the field

event.value=this.getField("2ea3").value>=event.value ? this.getField("2eD").value : 0;

In the above example I use 2ea3 for sum1 and 2eDfor sum2.  When I run it in debugger, it does show a zeo but it does not insert it into the form.  I don't know why it is not inserting a zero into the field when the debugger results to 0.

Thanks for your help.

TOPICS
How to, JavaScript, PDF forms

Views

127

Likes

Translate

Translate

Report

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

correct answers 2 Correct answers

Adobe Community Professional , Aug 10, 2021 Aug 10, 2021
If you use that code in "2eD" field, then how do you calculate sum2? Also you write that you want to check if sum1>(is greater)sum2 but in your script you used sum1>=(is greater or equal)sum2 so I'm not sure which one you want so Il use > in my script and you change it if it's not correct. See if this is what you looking for,put it in "2eD" field as custom calculation script and if your actual name for fielda,fieldb and fieldc are different then in script, change them to your actual names: var a...

Likes

Translate

Translate
Adobe Community Professional , Aug 11, 2021 Aug 11, 2021
You can't manually input value into that field because calculation will overwrite your input, thats way field stay blank. For the other part, script is set so it stays blank until there is value in all four fields. See if this script does what you want: var a = this.getField("5a.0.4"); var b = this.getField("5a.1.4"); var c = this.getField("5a.2.4"); var d = this.getField("5b1"); var y = this.getField("2ea3").value; var x = Number(a.value)+Number(b.value)+Number(c.value)+Number(d.value); if(x !=...

Likes

Translate

Translate
Adobe Community Professional ,
Aug 10, 2021 Aug 10, 2021

Copy link to clipboard

Copied

If you use that code in "2eD" field, then how do you calculate sum2?

Also you write that you want to check if sum1>(is greater)sum2 but in your script you used sum1>=(is greater or equal)sum2 so I'm not sure which one you want so Il use > in my script and you change it if it's not correct.

See if this is what you looking for,put it in "2eD" field as custom calculation script and if your actual name for fielda,fieldb and fieldc are different then in script, change them to your actual names:

var a = this.getField("fielda");
var b = this.getField("fieldb");
var c = this.getField("fieldc");
var y = this.getField("2ea3").value;

if(typeof a.value == 'string' || typeof b.value == 'string' || typeof c.value == 'string')
var x = "";
else
x = a.value+b.value+c.value;

if(x != "" && y>x){
event.value = 0;
a.display = display.hidden;
b.display = display.hidden;
c.display = display.hidden;}
else{
event.value = x;
a.display = display.visible;
b.display = display.visible;
c.display = display.visible;}

Likes

Translate

Translate

Report

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 Beginner ,
Aug 11, 2021 Aug 11, 2021

Copy link to clipboard

Copied

Thank you for answering my question.  For the 2eD field, i used the built-in calculator under text field properties.  I told Adobe to give me the sum of these actual field names (actual names in my form -  5a.0.4, 5a.1.4, 5a.2.4, 5b1.  So 2eD = the sum of  5a.0.4, 5a.1.4, 5a.2.4, 5b1  I did not use any scripting for that.  Does that matter? Will javascripting override calculated values in that way?

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Aug 11, 2021 Aug 11, 2021

Copy link to clipboard

Copied

No, it doesn't matter. You just have to make sure the fields calculation order is correct.

Likes

Translate

Translate

Report

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 Beginner ,
Aug 11, 2021 Aug 11, 2021

Copy link to clipboard

Copied

I get a syntax err:sytax error

7;at line 8

var a = this.getField("5a.0.4");
var b = this.getField("5a.1.4");
var c = this.getField("5a.2.4");
var d = this.getField("5b1");
var y = this.getField("2ea3").value;

if(typeof a.value == 'string' || typeof b.value == 'string' || typeof c.value == 'string')|| typeof d.value == 'string'
var x ="";   <------points here
else
x = a.value+b.value+c.value+d.value;

if(x != "" && y>=x){
event.value = 0;
a.display = display.hidden;
b.display = display.hidden;
c.display = display.hidden;}
else{
event.value = x;
a.display = display.visible;
b.display = display.visible;
c.display = display.visible;}

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Aug 11, 2021 Aug 11, 2021

Copy link to clipboard

Copied

It's the line before that one. Your parentheses don't match... The entire condition in the if-statement has to be placed inside parentheses.

Likes

Translate

Translate

Report

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 Beginner ,
Aug 11, 2021 Aug 11, 2021

Copy link to clipboard

Copied

This seems to work but when I manual change the values, from the fields going invisible (I make it so y is less than x by changing the field numbers, the form does not seem to recalculate.  The invisible fields that were supposed to become visible again do indeed become visible but they do not calcuate in the "2ea3" field (where they were adding the 4 fields before.  They just stay zero.  Is there like an f5 refresh butting in Adobe?  I don't think there is.  How do I "wake up" the form and tell it to recalcuate?

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Aug 11, 2021 Aug 11, 2021

Copy link to clipboard

Copied

See the last part of my first reply.

Likes

Translate

Translate

Report

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 Beginner ,
Aug 11, 2021 Aug 11, 2021

Copy link to clipboard

Copied

I thought that was the issue but there are so many fields in that box.  I do not know which to place in what order.  What kind of fields do I prioritize?

Likes

Translate

Translate

Report

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 Beginner ,
Aug 11, 2021 Aug 11, 2021

Copy link to clipboard

Copied

I am stuck.  I reordered the calculation order and I am still getting zero in the field that calculates the four fields above (a b c and d).  The other summing fields calculate.  It is just the field with the scripting that does not.

Likes

Translate

Translate

Report

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 Beginner ,
Aug 11, 2021 Aug 11, 2021

Copy link to clipboard

Copied

I figured out that there was a validation scrip that was causing a 0 to be entered in the box but now it is coming up blank. The four fields that need to be calculated under the above script yeilds a blank field and there is not even a zero in the box.

Likes

Translate

Translate

Report

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 Beginner ,
Aug 11, 2021 Aug 11, 2021

Copy link to clipboard

Copied

To further illustrate, this is what the field looks like.  Look at line 2(e) D.  It should have a number in there.  You can actually type a number in there but it stays blank.

 

plan.png

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Aug 11, 2021 Aug 11, 2021

Copy link to clipboard

Copied

You can't manually input value into that field because calculation will overwrite your input, thats way field stay blank. For the other part, script is set so it stays blank until there is value in all four fields.

See if this script does what you want:

var a = this.getField("5a.0.4");
var b = this.getField("5a.1.4");
var c = this.getField("5a.2.4");
var d = this.getField("5b1");
var y = this.getField("2ea3").value;

var x = Number(a.value)+Number(b.value)+Number(c.value)+Number(d.value);

if(x != 0 && y>=x){
event.value = 0;
a.display = display.hidden;
b.display = display.hidden;
c.display = display.hidden;}
else{
event.value = x;
a.display = display.visible;
b.display = display.visible;
c.display = display.visible;}

Likes

Translate

Translate

Report

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
Engaged ,
Aug 12, 2021 Aug 12, 2021

Copy link to clipboard

Copied

Hi Nesa,

quote

You can't manually input value into that field because calculation will overwrite your input,

It's not totally true. You have the possibility to overwrite a calculated field and the calculation will overwrite the input only if the event source comes from a field included in the calculation (understandable????). Else the overwritten value stays in place.

Please have a look on the attached example.

@+

Likes

Translate

Translate

Report

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
Enthusiast ,
Aug 12, 2021 Aug 12, 2021

Copy link to clipboard

Copied

You can't manually input value into that field because calculation will overwrite your input,

See the words in red? I belive she referring to the  field where script is and script in it, so she is not saying it generally. My english is not that great I could be mistaken?

Likes

Translate

Translate

Report

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
Engaged ,
Aug 12, 2021 Aug 12, 2021

Copy link to clipboard

Copied

I will not argue about that because my English is certainly not quite good enough.

It was just information.

@+

Likes

Translate

Translate

Report

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 Beginner ,
Aug 12, 2021 Aug 12, 2021

Copy link to clipboard

Copied

Hey Nesa:  First: Thank you for all your help.  You are amazing.

I tried the script and it seems to work but I was wanting to blank out the 5b1 field as well in the priority debts (see picture line 2(e)(A)(3) exceeded or equalled the unsecured claims 2(e)(D).  I also wanted to blank out var d ("5b1") which is unsecured claims later on in the plan document (see pic)

plan2.png

See 5(b) on the document (4th line down) and field 5b1).  This should also become hidden because paying the priority claims (2(e)D on the form on the picture, eliminates the need to pay other unsecured creditors.

In case you are interested, this is for Chapter 13 cases in the Eastern District of PA.

 

 

So, should this:

var a = this.getField("5a.0.4");
var b = this.getField("5a.1.4");
var c = this.getField("5a.2.4");
var d = this.getField("5b1");
var y = this.getField("2ea3").value;
var x = Number(a.value)+Number(b.value)+Number(c.value)+Number(d.value);
if(x != 0 && y>=x){
event.value = 0;
a.display = display.hidden;
b.display = display.hidden;
c.display = display.hidden;}
else{
event.value = x;
a.display = display.visible;
b.display = display.visible;
c.display = display.visible;}

 

be this?

var a = this.getField("5a.0.4");
var b = this.getField("5a.1.4");
var c = this.getField("5a.2.4");
var d = this.getField("5b1");
var y = this.getField("2ea3").value;
var x = Number(a.value)+Number(b.value)+Number(c.value)+Number(d.value);
if(x != 0 && y>=x){
event.value = 0;
a.display = display.hidden;
b.display = display.hidden;
c.display = display.hidden;

d.display=display.hidden}
else{
event.value = x;
a.display = display.visible;
b.display = display.visible;
c.display = display.visible;

d.display = display.visible}

 

Likes

Translate

Translate

Report

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 Beginner ,
Aug 12, 2021 Aug 12, 2021

Copy link to clipboard

Copied

You removed this. 

if(typeof a.value == 'string' || typeof b.value == 'string' || typeof c.value == 'string'|| typeof d.value == 'string')

what was the purpose of that in the first place? I am genuinely curious.

Likes

Translate

Translate

Report

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 Beginner ,
Aug 13, 2021 Aug 13, 2021

Copy link to clipboard

Copied

LATEST

This is working pretty well.  Thank you everyone and particularly  Nesa!

Likes

Translate

Translate

Report

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