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.


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

New Here ,
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

39

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

Adobe Community Professional , 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;

Likes

Translate

Translate
Adobe Community Professional ,
Sep 07, 2021 Sep 07, 2021

Copy link to clipboard

Copied

Which Adobe app or service are you using?

 

Nancy O'Shea, Adobe Product User & Community Professional
Alt-Web Design & Publishing ~ Web : Print : Graphics : Media

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
New Here ,
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/

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 ,
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;

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

Copy link to clipboard

Copied

LATEST

This is perfect! Thank you!

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