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

Help with JavaScript for Subtraction

Guest
Oct 26, 2020 Oct 26, 2020

Copy link to clipboard

Copied

Hello, I'm learning JavaScript as I go and it is starting to make more sense.  

 

So I tried the following JavaScript as simplified field notation, but it is not doing the math correctly.

Number(this.getField("! Total Revenue").value - Number(this.getField("!S6 Subtotal SFA Costs").value - Number(this.getField("!Total FSMC Costs").value;

 I had started with this JavaScript, but I was getting an error.

event value = Number(this.getField("! Total Revenue").value - Number(this.getField("!S6 Subtotal SFA Costs").value - Number(this.getField("!Total FSMC Costs").value;

 I am trying to get the profit/loss by taking the field ! Total Revenue - !S6 Subtotal SFA Costs - !Total FSMC Costs.  And yes, I now know I should have done my field names differently but I had those set before I ever knew I needed to learn how to do JavaScript thus using the this.getField.

 

Any help would be appreciated.

TOPICS
PDF forms

Views

431

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 Expert ,
Oct 26, 2020 Oct 26, 2020

Copy link to clipboard

Copied

You miss many ) characters.

Votes

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 Expert ,
Oct 26, 2020 Oct 26, 2020

Copy link to clipboard

Copied

Also a "." between "event" and "value"...

Votes

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 Expert ,
Oct 26, 2020 Oct 26, 2020

Copy link to clipboard

Copied

Simplified field notation didn't requires JavaScript keywords, and requires blank spaces (and dots) in fields names to be escaped with a backslash, like this:

 

!\ Total\ Revenue - !S6\ Subtotal\ SFA\ Costs - !Total\ FSMC\ Costs

Votes

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 ,
Oct 26, 2020 Oct 26, 2020

Copy link to clipboard

Copied

That won't work.

Votes

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 Expert ,
Oct 26, 2020 Oct 26, 2020

Copy link to clipboard

Copied

JavaScript requires parentheses to be closed:

 

event.value = Number(this.getField("! Total Revenue").value) - Number(this.getField("!S6 Subtotal SFA Costs").value) - Number(this.getField("!Total FSMC Costs").value);

Votes

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
Guest
Oct 26, 2020 Oct 26, 2020

Copy link to clipboard

Copied

Thank you this worked for me.  I knew something was missing, but given I don't understand JavaScript I didn't know what was missing.  

Votes

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
LEGEND ,
Oct 26, 2020 Oct 26, 2020

Copy link to clipboard

Copied

Since you mentioned you wanted to use simplified field notation, you don't use JavaScript there. You're going to have a problem with it if you use an exclamation point in the fieldname. Some non-alphanumeric characters (e.g., spaces) can be escaped with a backslash, like:

 

Total\ Revenue - S6\ Subtotal\ SFA\ Costs - Total\ FSMC\ Costs

 

But the exclamation points would need to be removed from the fieldnames for this option to work.

Votes

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
Guest
Oct 26, 2020 Oct 26, 2020

Copy link to clipboard

Copied

LATEST

It wasn't that I wanted to use Simplified Field Notation that was what I tried.  Because I'm not going to change the Field Names I went with the Java Script.

Votes

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