I am trying to set up a javascript "If" formula on a form and keep running into errors.

New Here ,
Jun 14, 2019

Copy link to clipboard

Copied

Good Morning/Afternoon!

I am trying to set up a calculation on a form that is based of an "if/Else" format.  The calculation in words would look something like this:

Skill Calculation:

If “Trained Skill 1”>0;

“bonus 1 = “Dex Mod” + “Trained Skill 1” + “Skill Misc Mod 1” + “Addtl Misc Mod 1”

Else

“bonus 1” = 0

If it were in excel it would look like this as far as format and expected outcome:

As you can see from the Excel snip, Row 2 shows that C1 ("Trained Skill 1") has a 1 in the column.  As a result, the sum of B2 ("Ability Mod"), C2 ("Trained Skill 1"), D2 ("Skill Misc Mod 1"), and E2 ("Addtl Misc Mod 1") is calculated and placed in A1 ("Bonus 1")

Conversely, Row 3 shows that C2 ( "Trained Skill 1") has a 0 in the column.  As a result, the sum of B2 ("Ability Mod"), C2 ("Trained Skill 1"), D2 ("Skill Misc Mod 1"), and E2 ("Addtl Misc Mod 1") is NOT calculated and a 0 is placed in B1 ("Bonus 1")

Can anyone help me create a javascript formula depicting this action?   I cant seem to get a working script.   

Thank you tremendously in advance!


Best!

JLD

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

You can use this code as the custom calculation script of "bonus 1":

if (Number(this.getField("Trained Skill 1").valueAsString)>0)

    event.value = Number(this.getField("Dex Mod").valueAsString) + Number(this.getField("Trained Skill 1").valueAsString) +

     Number(this.getField("Skill Misc Mod 1").valueAsString) + Number(this.getField("Addtl Misc Mod 1").valueAsString);

else event.value = 0;

TOPICS
Acrobat SDK and JavaScript, Windows

Views

63

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

I am trying to set up a javascript "If" formula on a form and keep running into errors.

New Here ,
Jun 14, 2019

Copy link to clipboard

Copied

Good Morning/Afternoon!

I am trying to set up a calculation on a form that is based of an "if/Else" format.  The calculation in words would look something like this:

Skill Calculation:

If “Trained Skill 1”>0;

“bonus 1 = “Dex Mod” + “Trained Skill 1” + “Skill Misc Mod 1” + “Addtl Misc Mod 1”

Else

“bonus 1” = 0

If it were in excel it would look like this as far as format and expected outcome:

As you can see from the Excel snip, Row 2 shows that C1 ("Trained Skill 1") has a 1 in the column.  As a result, the sum of B2 ("Ability Mod"), C2 ("Trained Skill 1"), D2 ("Skill Misc Mod 1"), and E2 ("Addtl Misc Mod 1") is calculated and placed in A1 ("Bonus 1")

Conversely, Row 3 shows that C2 ( "Trained Skill 1") has a 0 in the column.  As a result, the sum of B2 ("Ability Mod"), C2 ("Trained Skill 1"), D2 ("Skill Misc Mod 1"), and E2 ("Addtl Misc Mod 1") is NOT calculated and a 0 is placed in B1 ("Bonus 1")

Can anyone help me create a javascript formula depicting this action?   I cant seem to get a working script.   

Thank you tremendously in advance!


Best!

JLD

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

You can use this code as the custom calculation script of "bonus 1":

if (Number(this.getField("Trained Skill 1").valueAsString)>0)

    event.value = Number(this.getField("Dex Mod").valueAsString) + Number(this.getField("Trained Skill 1").valueAsString) +

     Number(this.getField("Skill Misc Mod 1").valueAsString) + Number(this.getField("Addtl Misc Mod 1").valueAsString);

else event.value = 0;

TOPICS
Acrobat SDK and JavaScript, Windows

Views

64

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
Jun 14, 2019 0
Most Valuable Participant ,
Jun 14, 2019

Copy link to clipboard

Copied

You can use this code as the custom calculation script of "bonus 1":

if (Number(this.getField("Trained Skill 1").valueAsString)>0)

    event.value = Number(this.getField("Dex Mod").valueAsString) + Number(this.getField("Trained Skill 1").valueAsString) +

     Number(this.getField("Skill Misc Mod 1").valueAsString) + Number(this.getField("Addtl Misc Mod 1").valueAsString);

else event.value = 0;

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
Reply
Loading...
Jun 14, 2019 1
QB1Sho LATEST
New Here ,
Jun 14, 2019

Copy link to clipboard

Copied

Wow!  Such a fast response and right on the money!  Worked like a dream.  Thank you try67​ , you are a great asset to this community.  I was reading your past posts and was close...I just had a few missing things! 

Thank you a ton!  Have a great weekend!

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
Reply
Loading...
Jun 14, 2019 0