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

Custom Calculation Script: Sum, only add positive values (Exclude negative values)

Community Beginner ,
Sep 07, 2021 Sep 07, 2021

Copy link to clipboard

Copied

I am making a form to quote out multiple products, where the customer receives a rebate.  The product is taxable, the rebate does not effect the taxable value of the quote.  The way I am formatting it, Product & Rebates are added in the body of the quote.

 

For example, if a product is $100, the rebate is $50 (written as -75), and the Sales Tax is 7%, I need to subtotal to read as $100, and not $25, since the sum adds positive and negative number together.

 

How can I write a custom script where all positive fields are added together, but if the value of a field is negative, the calulation treats it as a value of 0, rather than it's negative value?

Views

444

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 1 Correct answer

Community Expert , Sep 08, 2021 Sep 08, 2021

You can use this code as the custom calculation script of your field (adjust the first line, of course):

 

var fieldsToAdd = ["Text1", "Text2", "Text3"]; // Replace with actual field names
var total = 0;
for (var i in fieldsToAdd) {
	var f = this.getField(fieldsToAdd[i]);
	var v = Number(f.valueAsString);
	if (v>0) total+=v;
}
event.value = total;

Votes

Translate

Translate
Community Expert ,
Sep 07, 2021 Sep 07, 2021

Copy link to clipboard

Copied

Which Adobe app or service are you using?

 

Nancy O'Shea— Product User, Community Expert & Moderator
Alt-Web Design & Publishing ~ Web : Print : Graphics : Media

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 07, 2021 Sep 07, 2021

Copy link to clipboard

Copied

I am using Adobe Acrobat Pro. I have used many custom calulations before with great success, but this one illudes me.

 

Moving from Using the Community (which is about the forums) to the correct forum... Mod
To ask in the forum for your program please start at https://community.adobe.com/

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 08, 2021 Sep 08, 2021

Copy link to clipboard

Copied

You can use this code as the custom calculation script of your field (adjust the first line, of course):

 

var fieldsToAdd = ["Text1", "Text2", "Text3"]; // Replace with actual field names
var total = 0;
for (var i in fieldsToAdd) {
	var f = this.getField(fieldsToAdd[i]);
	var v = Number(f.valueAsString);
	if (v>0) total+=v;
}
event.value = total;

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 08, 2021 Sep 08, 2021

Copy link to clipboard

Copied

LATEST

This is perfect! Thank you!

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