Copy link to clipboard
Copied
I have been attempting to calculate a subtraction javascript in acrobat X. (i am an inexperienced javascript coder and found part of this this in a search online. I made some assumptions and elaborated the rest)
This is my javascript:
// Get first field value
var v1 = getField("Qty Recd").value;
// Get second field value
var v2 = getField("Text4.0.1").value;
// Get third field value
var v3 = getField("Text4.1.1").value;
// Get fourth field value
var v4 = getField("Text4.2.1").value;
// Get fifth field value
var v5 = getField("Text4.3.1").value;
// Get sixth field value
var v6 = getField("Text4.4.1").value;
// Get seventh field value
var v7 = getField("Text4.5.1").value;
// Get eighth field value
var v8 = getField("Text4.6.1").value;
// Get ninth field value
var v9 = getField("Text4.7.1").value;
// Get tenth field value
var v10 = getField("Text4.8.1").value;
// Get eleventh field value
var v11 = getField("Text4.9.1").value;
// Set this field value equal to the difference
event.value = v1-v2;-v3;-v4;-v5;-v6;-v7;-v8;-v9;-v10;-v11;-v12;-v13;-v14
When I test the calculation, initially the result is correct. However, when I change one of the values to another number or delete it, the result continues to subtract what was initially entered in the field. How do I correct this? .
I have been trying different things and searching for help for two days.
Can anyone figure out what I am doing wrong? And then help me correct it?
Thank you Emelone
Copy link to clipboard
Copied
This line is not correct:
event.value = v1-v2;-v3;-v4;-v5;-v6;-v7;-v8;-v9;-v10;-v11;-v12;-v13;-v14
What exactly do you want to calculate?
Copy link to clipboard
Copied
The ("Qty Recd") field is a number of total pounds
there are 14 other fields each of which may contain a reject number of weight
I want each of those fields (when they contain a number) to be subtracted from the total pounds.
Copy link to clipboard
Copied
I also want them to update correctly if changed or added to at a later time.
Copy link to clipboard
Copied
You may want to lookup what the correct JavaScript syntax is for such an expression. You cannot copy&paste your way to a solution without knowing basic JavaScript take a look here for some guidance about how to learn the programing language: Learning to Program JavaScript for Adobe Acrobat - KHKonsulting LLC
Try this:
event.value = v1-v2-v3-v4-v5-v6-v7-v8-v9-v10-v11-v12-v13-v14;
As you can see, there is only one ";" at the end of the line. You are ending a statement with the semi-colon. In your code, your expression used for the calculation was only this:
event.value = v1-v2;
Copy link to clipboard
Copied
Thank you for the direction Karl.
Even when I correct the last line the original problem exists.
The code subtracts the numbers fine, but if I make a change or a deletion to one of the numbers, it does not correct the answer.
The correction to the code did not correct the error.
Copy link to clipboard
Copied
Do you have other calculations in your script? If so, then you have to make sure that the calculation order is correct.
If that's not the problem, then make sure that you actually tab out of the field that you modified to see if that triggers the calculation. As long as the curser is still in the field that you've modified, the edit process is not considered to be done.
Copy link to clipboard
Copied
Yes there is a calculation set up to add the same 14 fields. (the "total" field is in a completely different location and works properly.)
As for the order, they follow top to bottom of the column.
I am tabbing (or clicking) out of the field. This is how I noticed the error during testing.
It is when I delete one of the fields of data (and tab out of the field) that the problem occurs.
EX: "Qty Recd" = 20000
subtract 100 = 19900
subtract 100 = 19800
subtract 100 = 19700
CHANGE subtracted number to 200 = 19600 (this is the number that calculates into the "Qty Recd" field)
DELETE subtracted number = 19500 (this is the number that calculates into the "Qty Recd" field)
If I change one of the subtracted numbers to 200, the calculation continues to subtract the original 100 (when I click or tab out of the field)
If I delete the subtracted number, the calculation continues to subtract the original 100 (when I click or tab out of the field)
Copy link to clipboard
Copied
This is definitely a field calculation order issue. Notice that this order has nothing to do with the tabbing order or the physical location of the fields on the page. It's a completely independent order which can be set in Form Edit mode via Tasks - Other Tasks - Set Fields Calculation Order.
Copy link to clipboard
Copied
Here is a screen shot of where you will find the field calculation order:
Once you select this function, you get a new dialog that lists all your calculated fields. Bring them into the correct order from top to bottom so that the field that requires other fields to be calculated first is lower than these fields it depends on.
Copy link to clipboard
Copied
These fields came up automatically. They are not the fields that are part of the calculation. How do these fields get there? I am not able to rename these fields either. I forgot about these fields. They are calculating averages (and are working correctly).
Does Acrobat allow for multiple calculations in various fields on the same form?
Copy link to clipboard
Copied
All fields that are calculated will be in this list, even if they don't contribute to the field that you are working on. This is the calculation order for the whole form. You will need to bring that list into the correct order so that the fields that don't depend on other calculated fields are calculated first, and the others only after all the contributing fields are calculated.
So yes, Acrobat does allow for multiple calculated fields, and that is exactly what this list is about. Move your field to the end of the list (assuming that no other field relies on the output of this field), and you should no longer have problems with the calculation order. If your form still does not behave correctly after you make this change, the problem is caused by something else.
Copy link to clipboard
Copied
It has always been there. It placed itself in that positiion.
Copy link to clipboard
Copied
In that case you'll need to share the file so we can see for ourselves what's going on there.
You can upload it to somewhere like Dropbox, Google Drive, etc., and then post the link to it here.
Copy link to clipboard
Copied
A while ago, I wrote up some instructions about how to share a file via Adobe's Document Cloud: Share Documents via Adobe's Document Cloud - KHKonsulting LLC
Copy link to clipboard
Copied
Sorry for the delay, I have been out sick for a few days. We have our own share file location. I will post the link once I upload the file.
Thank you for your continued efforts.
Copy link to clipboard
Copied
Here is the link. It was acting a bit odd when I pasted it. Please let me know if it works.
I have been playing around with renaming fields in the form. This will not look like the original caLculation screen shot I posted but nothing has been moved.
Thank you.
Copy link to clipboard
Copied
There are two problems: The first one is that you still are using the incorrect last line that I corrected in an earlier comment. What you have in your script will not work unless you correct it. However, even after you correct that line, your logic is wrong: You are trying to calculate the field named "Qty Recd", but in your calculation you start our by getting the value from the field named "Qty Recd" - this means that there is a circular dependency, which will not work. Where is the number coming from that you use to subtract all other values from? It cannot be the same field that you are trying to calculate.
Copy link to clipboard
Copied
Ok, so I correct the last line and I changed the calculation formula to look like the following:
// Get first field value
var v1 = getField("RW1").value;
// Get second field value
var v2 = getField("RW2").value;
// Get third field value
continuing on to the end:
// Set this field value equal to the difference
event.value = v1-v2-v3-v4-v5-v6-v7-v8-v9-v10-v11-v12-v13-v14;
now nothing happens.
As previously stated, the "Qty Recd" field is the field I am using to subtract all other numbers from.
That number should be reduced based on any number entered into the "Reject Weight" fields.
Are you indicating I need to create an entirely different field for the calculation? Can it be hidden?
I tried a simple subtraction calculation (Qty Recd - Total) (the Total field contains a calculation to add the numbers in the Reject Weight fields)
Copy link to clipboard
Copied
Where does the "Qty Recd" information come from? Is that something that the user enters? In that case, you will need a separate field for the user input, which you can can use as the input for your calculation, which needs to happen in a second field. You cannot use the same field for user input and for a calculation on that user specified information. First of all, it would confuse the user, and secondly, you would never know if you've already subtracted information from that number, or if you still need to make further adjustments.
Copy link to clipboard
Copied
Yes, Qty Rec'd is manually entered.
I removed the calculation from that field.
I created a new field and named it "Minus Reject" which now holds the calculation.
I get nothing.
Copy link to clipboard
Copied
Are you getting any errors on the JavaScript console? You can bring up the console via Ctrl-J or Cmd-J. If not, you may want to share your modified file again.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Change the value of one of the fields in the file and then look again...
On Fri, Feb 10, 2017 at 11:47 PM, Eileen987 <forums_noreply@adobe.com>
Copy link to clipboard
Copied
There is an error in the JavaScript console:
TypeError: getField(...) is null
4:Field:Calculate
This shows that there is an error in line #4 of your calculation script. When you check line 4, you will find this:
var v1 = getField("Qty Recd").value;
The erro message is telling you that 'getField("Qty Recd")' is null - and therefore does not have a property named "value". The only reason why this expression would return null is if there is no field named "Qty Recd", and that is correct: Your field name is actually "QtyRecd" - no space. JavaScript cannot guess what you meant, it will just take whatever name you've provided and will either execute the command successfully, or it will fail. It is your job as programmer to make sure that you know when something fails.
Also, you are still using the wrong syntax for your final calculation. This is what I find as the last line of your script:
event.value = v1-v2;-v3;-v4;-v5;-v6;-v7;-v8;-v9;-v10;-v11;-v12;-v13;-v14
I've pointed this out a number of times already, this is wrong, and you need to fix this line if you want to see a correct value in this field. We cannot help you if you ignore the errors we are pointing out. You are programming in JavaScript, which means that you have to know the basics of the JavaScript language. There is no way around that.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now