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

PDF Form JavaScript Conditional Formula

Community Beginner ,
Sep 17, 2024 Sep 17, 2024

Copy link to clipboard

Copied

I'm trying to have a field only calculate if there the information in another field is greater than 0. I've tried a few different formulas but haven't been able to get it to work.

 

This is the current formula without the condition that works but is calculating all the time (vs just when the one field is above 0):

TuitionAmountBeforetheCOF + StateTABooKAllowance + MandatoryFees -CollegeOpportunityFundAmountONLY - PellGrantAmountAwardedForSemester - AdditionalFundingSourceAmount1 - AdditionalFundingSourceAmount2 – AdditionalFundingSourceAmount3 

 

This the conditional attempt:

var PellGrantAmountAwardedForSemester = this.getField("PellGrantAmountAwardedForSemester").value

var TuitionAmountBeforetheCOF = this.getField("TuitionAmountBeforetheCOF").value 

var StateTABooKAllowance = this.getField("StateTABooKAllowance").value 

var MandatoryFees = this.getField("MandatoryFees").value 

var CollegeOpportunityFundAmountONLY = this.getField("CollegeOpportunityFundAmountONLY").value 

var AdditionalFundingSourceAmount1 = this.getField("AdditionalFundingSourceAmount1").value 

var AdditionalFundingSourceAmount2 = this.getField("AdditionalFundingSourceAmount2").value 

var AdditionalFundingSourceAmount2 = this.getField("AdditionalFundingSourceAmount2").value; 

  

if(PellGrantAmountAwardedForSemester >= 0) event.value =( (TuitionAmountBeforetheCOF + StateTABooKAllowance + MandatoryFees - CollegeOpportunityFundAmountONLY - PellGrantAmountAwardedForSemester - AdditionalFundingSourceAmount1 - AdditionalFundingSourceAmount2 - AdditionalFundingSourceAmount3)); 
TOPICS
JavaScript , PDF forms

Views

257

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
1 ACCEPTED SOLUTION
Community Expert ,
Sep 17, 2024 Sep 17, 2024

Copy link to clipboard

Copied

LATEST

Just saying doesn't work is not of much help to us, describe what doesn't work, like it doesn't calculate anything or something else?

You said you wish calculation to only trigger if value is greater than 0, but in your script you used >=0 which will calculate all the time even if field is empty, change it to >0

 

EDIT:
This works fine for me, although you may want to add 'else' statement to set field back to 0 or blank:

var PellGrantAmountAwardedForSemester = Number(this.getField("PellGrantAmountAwardedForSemester").valueAsString);
var TuitionAmountBeforetheCOF = Number(this.getField("TuitionAmountBeforetheCOF").valueAsString);
var StateTABooKAllowance = Number(this.getField("StateTABooKAllowance").valueAsString);
var MandatoryFees = Number(this.getField("MandatoryFees").valueAsString);
var CollegeOpportunityFundAmountONLY = Number(this.getField("CollegeOpportunityFundAmountONLY").valueAsString);
var AdditionalFundingSourceAmount1 = Number(this.getField("AdditionalFundingSourceAmount1").valueAsString);
var AdditionalFundingSourceAmount2 = Number(this.getField("AdditionalFundingSourceAmount2").valueAsString);
var AdditionalFundingSourceAmount3 = Number(this.getField("AdditionalFundingSourceAmount3").valueAsString);


if (PellGrantAmountAwardedForSemester > 0) {
    event.value = (
        TuitionAmountBeforetheCOF 
        + StateTABooKAllowance 
        + MandatoryFees 
        - CollegeOpportunityFundAmountONLY 
        - PellGrantAmountAwardedForSemester 
        - AdditionalFundingSourceAmount1 
        - AdditionalFundingSourceAmount2 
        - AdditionalFundingSourceAmount3
    );
}

View solution in original post

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 ,
Sep 17, 2024 Sep 17, 2024

Copy link to clipboard

Copied

Check the console for errors.  You have at least one error.  There is no AdditionalFundingSourceAmount3 variable as referenced in your script.  You have AdditionalFundingSourceAmount2 twice.

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 Beginner ,
Sep 17, 2024 Sep 17, 2024

Copy link to clipboard

Copied

I noticed that after posting and fixed it but it still doesn'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 ,
Sep 17, 2024 Sep 17, 2024

Copy link to clipboard

Copied

There should be a semi-colon after each variable declaration line.  Any errors in the console?

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 Beginner ,
Sep 17, 2024 Sep 17, 2024

Copy link to clipboard

Copied

I've tried with and without the semi-colons and still doesn'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 ,
Sep 17, 2024 Sep 17, 2024

Copy link to clipboard

Copied

LATEST

Just saying doesn't work is not of much help to us, describe what doesn't work, like it doesn't calculate anything or something else?

You said you wish calculation to only trigger if value is greater than 0, but in your script you used >=0 which will calculate all the time even if field is empty, change it to >0

 

EDIT:
This works fine for me, although you may want to add 'else' statement to set field back to 0 or blank:

var PellGrantAmountAwardedForSemester = Number(this.getField("PellGrantAmountAwardedForSemester").valueAsString);
var TuitionAmountBeforetheCOF = Number(this.getField("TuitionAmountBeforetheCOF").valueAsString);
var StateTABooKAllowance = Number(this.getField("StateTABooKAllowance").valueAsString);
var MandatoryFees = Number(this.getField("MandatoryFees").valueAsString);
var CollegeOpportunityFundAmountONLY = Number(this.getField("CollegeOpportunityFundAmountONLY").valueAsString);
var AdditionalFundingSourceAmount1 = Number(this.getField("AdditionalFundingSourceAmount1").valueAsString);
var AdditionalFundingSourceAmount2 = Number(this.getField("AdditionalFundingSourceAmount2").valueAsString);
var AdditionalFundingSourceAmount3 = Number(this.getField("AdditionalFundingSourceAmount3").valueAsString);


if (PellGrantAmountAwardedForSemester > 0) {
    event.value = (
        TuitionAmountBeforetheCOF 
        + StateTABooKAllowance 
        + MandatoryFees 
        - CollegeOpportunityFundAmountONLY 
        - PellGrantAmountAwardedForSemester 
        - AdditionalFundingSourceAmount1 
        - AdditionalFundingSourceAmount2 
        - AdditionalFundingSourceAmount3
    );
}

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