How to Override Calculated Field

New Here ,
Dec 16, 2019 Dec 16, 2019

Copy link to clipboard

Copied

Hi there,

 

We have a form that includes a field where it autocalculates the amount of commission a sales guy will get based off the Job Total and Percentage they fill in. 

 

How can I make it so the last field (commission total) can be autocalculated but with also the ability to be manually overwritten? 

 

Right now, I have the field to calculate with Simplified Field Notation: 

(Commission_Row_1 * Row1) / 100

 

"Commission_Row_1" is the Job Total

"Row1" is the Percentage of Commission 

TOPICS
PDF forms

Views

313

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 ,
Dec 16, 2019 Dec 16, 2019

Copy link to clipboard

Copied

Overriding a calculation can be tricky.  Just in general, this line of code will disable a calculation.

 

event.rc = false;

 

But the problem is detecting the situation where the calculation is first disabled, then the situation where it is enabled. 

 

An easy solution is to use a checkbox. For your case it might be labeled as "Calculated Commission". Then the check is used to both override the calculation and set the ReadOnly value of the field. 

 

It is also possible to detect keystrokes and set state variables that determine whether the calc is overridden. But this is much more difficult. What setup do you want?

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

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 ,
Dec 16, 2019 Dec 16, 2019

Copy link to clipboard

Copied

Thank you for your response! 

 

If I'm understanding correctly, the sales person would check the box which will allow them to override the autocalculated Commission Total and manually input an amount in that same field? 

 

Can you walk me through the steps on how to do this?

 

Thanks! 

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 ,
Dec 16, 2019 Dec 16, 2019

Copy link to clipboard

Copied

Let's say the checkbox is named "SetCommission". 

You're going to need to change the calculation to a custom script. Here's the code

 

event.rc = this.getField("SetCommission").value != "Off";

event.value = (this.getField("Commission_Row_1").value * this.getField("Row1").value) / 100;

 

Now, add this JavaScript to the Mouse UP action for the checkbox. It sets the commission total field to Read Only when unchecked, so the user can't enter any data. Does the opposite when checked, so the user can enter data. 

 

this.getField("commission total").readonly = event.target.value == "Off";

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

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
Explorer ,
Apr 10, 2022 Apr 10, 2022

Copy link to clipboard

Copied

this is a great solution for me. Could you add a code here so that the value is calculated again if the field is set to 0 (or cleared by click)?

Many Thanks!

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 ,
Apr 11, 2022 Apr 11, 2022

Copy link to clipboard

Copied

LATEST

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