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

Override Calculation and Reinstate Calculation

Explorer ,
Feb 21, 2018 Feb 21, 2018

Copy link to clipboard

Copied

In the example the fee column is calculated by the unit price x quantity entered. I need the amount paid column to show the calculated amount from the fee column unless a user wants to override the amount paid value and enter a different amount. I also need for the amount paid value to go back to the original calculated amount from the fee column if the user decides no different amount from the fee calculation is needed. Any ideas?

The fields are labeled: Unit Price 1, Quantity 1, Fee 1, Amount Paid 1 (for reference)

example.jpg

I found this message but it doesn't reinstate the calculated fee amount.

https://acrobatusers.com/forum/forms-acrobat/field-populated-another-field-can-be-written-over/

TOPICS
PDF forms

Views

1.5K

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

LEGEND , Feb 21, 2018 Feb 21, 2018

The custom calculation script for the Amount Paid field could be something like:

// Custom calculation script

// Get references to the input fields

var f1 = getField("Quantity 1");

var f2 = getField("Unit Price 1");

// If the field that triggered this calculation script is this one...

if (event.source && event.source === event.target) {

    if (event.source.value === "") {  // If this field was cleared...

        event.value = f1.value * f2.value;  // Set this field's value to the result of this calcula

...

Votes

Translate

Translate
LEGEND ,
Feb 21, 2018 Feb 21, 2018

Copy link to clipboard

Copied

You say: "I also need for the amount paid value to go back to the original calculated amount from the fee column if the user decides no different amount from the fee calculation is needed. "

How would they indicate that they decided that? By clearing the Amount Paid field, perhaps, or something else?

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
Explorer ,
Feb 21, 2018 Feb 21, 2018

Copy link to clipboard

Copied

Yes by clearing the amount paid field. Clearing the amount paid field it should go back to the original calculated amount (the amount in the fee column)

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 ,
Feb 21, 2018 Feb 21, 2018

Copy link to clipboard

Copied

The custom calculation script for the Amount Paid field could be something like:

// Custom calculation script

// Get references to the input fields

var f1 = getField("Quantity 1");

var f2 = getField("Unit Price 1");

// If the field that triggered this calculation script is this one...

if (event.source && event.source === event.target) {

    if (event.source.value === "") {  // If this field was cleared...

        event.value = f1.value * f2.value;  // Set this field's value to the result of this calculation

    }

} else if (event.source && event.source === f1) { // Set this field value to the calculated value if the quantity changed

    event.value = f1.value * f2.value;

}

This could be refined a bit, but it should get you started.

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 ,
Feb 21, 2018 Feb 21, 2018

Copy link to clipboard

Copied

I had to fix something in my previously posted code, so be sure to consider the revised version.

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
Explorer ,
Feb 21, 2018 Feb 21, 2018

Copy link to clipboard

Copied

Thank you George and Thom!! It works wonderfully!

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 ,
Feb 21, 2018 Feb 21, 2018

Copy link to clipboard

Copied

Use this calculation script.

event.rc = !(event.source && (event.target == event.source)) || (event.value == "");

event.value = f1.value * f2.value;

This is the correct way to override the calculation for manual entry.

No conditionals are needed.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 ,
Feb 21, 2018 Feb 21, 2018

Copy link to clipboard

Copied

Thom,

The problem with that is the field is recalculated if some other field changes, thus removing the custom entry. I had the same problem in my earlier script. Of course, you could add an additional condition so that the calculation only takes place if the quantity field is event.source.

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 ,
Feb 21, 2018 Feb 21, 2018

Copy link to clipboard

Copied

George, thank you for catching that!! I've done this a bunch of times and always had to save the state, I was trying to make it stateless.

this time, but was a little quick on the keystrokes. Saving state is absolutely necessary so the script can differentiate between a value that is user entered, and must be maintained, and a previously calculated value that can be changed.

There is a simple fix, which is to save the state into some field parameter that will be saved with the form. For example,the stroke color. This will also act as an indicator that the field has been overridden by user input

// Turns off user entered value if blank entry

// Turns on user entered value if user enters data

if(event.value == "")

  event.target.strokeColor = color.transparent;

else if(event.source && (event.target == event.source))

  event.target.strokeColor = color.blue;

event.rc = (event.target.strokeColor != color.blue);

There are several other properties that could also be used, some that don't change the appearance.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
Explorer ,
Feb 22, 2018 Feb 22, 2018

Copy link to clipboard

Copied

I have a follow up question to this. In the Total Amount Paid box at the bottom it totals the first 10 rows fine even if a user wants to enter their own total or clear it out again. But when you get down to row 11 (40 dollar row) you enter a quantity and it doesn't register the total correctly anymore in the total amount paid box. If I were to enter a quantity in the next row down it would then total up the previous entry (as if it were a step behind).

example2.jpg

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 ,
Feb 22, 2018 Feb 22, 2018

Copy link to clipboard

Copied

That means there's a problem with the field calculation order, most likely.

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
Explorer ,
Feb 22, 2018 Feb 22, 2018

Copy link to clipboard

Copied

Ok almost there. The calculation order fixed that problem. Now the only issue is the one row where a unit price is a variable. If you enter in the quantity first and then the unit price, the amount paid does not total, but that fee calculation is before the amount paid total calculation.

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 ,
Feb 22, 2018 Feb 22, 2018

Copy link to clipboard

Copied

Can you post the script you're using for the Amount Paid field for that row?

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
Explorer ,
Feb 22, 2018 Feb 22, 2018

Copy link to clipboard

Copied

It is the same script you had provided for the other amount paid row calculations.

var f1 = getField("Quantity33");

var f2 = getField("UnitPrice33");

if (event.source && event.source === event.target) {

    if (event.source.value === "") { 

        event.value = f1.value * f2.value;

    }

} else if (event.source && event.source === f1) {

    event.value = f1.value * f2.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
LEGEND ,
Feb 22, 2018 Feb 22, 2018

Copy link to clipboard

Copied

You'll have to include the unit price field in this as well, something like:

} else if (event.source && (event.source === f1 || event.source === f2)) {

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
Explorer ,
Feb 22, 2018 Feb 22, 2018

Copy link to clipboard

Copied

LATEST

Thanks for your help George, it works now.

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