Copy link to clipboard
Copied
I'm making a form for my organization and I want to assign a dollar amount to a field. The dollar amount is based off of the complexity (value ranges between 1 and 10) and if the access control is yes or a no (Access control yes is an interstate project and no access control is a highway project). Can someone give me an example of a custom script for this?
Copy link to clipboard
Copied
You can use this script as 'Custom calculation script', you can put it in "Text1" field.
Just finish list with all the prices.
var cpx = Number(this.getField("Complexity").valueAsString);
var chk = this.getField("Check Box").valueAsString;
var total1 = 0;
var total2 = 0;
var prices = [
{complexity: 1, check: "Yes", price1: 459, price2: 966 },
{complexity: 1, check: "No", price1: 364, price2: 846 },
{complexity: 2, check: "Yes", price1: 628, price2: 1136 },
{complexity: 2, check: "No", price1: 532, price2: 979 }
//Fill in list with rest of the complexity and prices.
];
for(var i in prices){
if(cpx == prices[i].complexity && chk == prices[i].check){
total1 = prices[i].price1;
total2 = prices[i].price2;}}
this.getField("Text1").value = total1;
this.getField("Text2").value = total2;
Copy link to clipboard
Copied
That's because of how the calculation script is written.
First, always return the calculated value for a field in the "event.value property".
So replace this line:
this.getField("Text1").value = total1;
with this one:
event.value = total1;
Next, since the script aslo sets a value in a different field, the entire calculation has to be qualified in order to allow user entry into the "Text2" field.
So put the whole calculation into an "if" statement
event.rc = this.getField("CalcOverride").value != "Off";
if(event.rc)
{
..... calculation script ...
}
There are a few other things that could be improved with this script. For example, the data could be moved to a document level script so it's global to the entire document. And the loop for finding the matching data could be simplified by useing the Array.find() method.
Copy link to clipboard
Copied
Hi @Badhawg66
Hope you are doing well and thanks for reaching out.
The workflow you are trying to achieve is might be possible using JavaScript. For more information, please check the help pages listed below:
https://acrobatusers.com/tutorials/javascript_console/
https://helpx.adobe.com/acrobat/using/applying-actions-scripts-pdfs.html
Hope it will help
Regards
Amal
Copy link to clipboard
Copied
Could you help me delete my attachments? I tried attaching on to my post yesterday but it said I couldn't attach files. I can't edit my post yet.
Copy link to clipboard
Copied
The best way to achieve this is to use a custom calculation script on the field that is assigned the Dollar amount value.
If there is any complexity to the calculation, then you'll need to use "if" statements.
Here are some relevant articles :
https://www.pdfscripting.com/public/How-to-Write-a-Basic-PDF-Calculation-Script.cfm?sd=40
https://www.pdfscripting.com/public/How-to-write-an-If-statement.cfm?sd=40
Copy link to clipboard
Copied
You can use this script as 'Custom calculation script', you can put it in "Text1" field.
Just finish list with all the prices.
var cpx = Number(this.getField("Complexity").valueAsString);
var chk = this.getField("Check Box").valueAsString;
var total1 = 0;
var total2 = 0;
var prices = [
{complexity: 1, check: "Yes", price1: 459, price2: 966 },
{complexity: 1, check: "No", price1: 364, price2: 846 },
{complexity: 2, check: "Yes", price1: 628, price2: 1136 },
{complexity: 2, check: "No", price1: 532, price2: 979 }
//Fill in list with rest of the complexity and prices.
];
for(var i in prices){
if(cpx == prices[i].complexity && chk == prices[i].check){
total1 = prices[i].price1;
total2 = prices[i].price2;}}
this.getField("Text1").value = total1;
this.getField("Text2").value = total2;
Copy link to clipboard
Copied
I filled in the rest of the values but I get a Syntax error saying I'm missing a ] after the element list on line 11. Did I do something wrong?
var cpx = Number(this.getField("Complexity").valueAsString);
var chk = this.getField("Check Box").valueAsString;
var total1 = 0;
var total2 = 0;
var prices = [
{complexity: 1, check: "Yes", price1: 459, price2: 966 },
{complexity: 1, check: "No", price1: 364, price2: 846 },
{complexity: 2, check: "Yes", price1: 628, price2: 1136 },
{complexity: 2, check: "No", price1: 532, price2: 979 }
{complexity: 3, check: "Yes", price1: 701, price2: 1353 },
{complexity: 3, check: "No", price1: 604, price2: 1208 },
{complexity: 4, check: "Yes", price1: 894, price2: 1280 },
{complexity: 4, check: "No", price1: 749, price2: 1124 }
{complexity: 5, check: "Yes", price1: 942, price2: 1414 },
{complexity: 5, check: "No", price1: 834, price2: 1293 },
{complexity: 6, check: "Yes", price1: 1136, price2: 1353 },
{complexity: 6, check: "No", price1: 918, price2: 1196 }
{complexity: 7, check: "Yes", price1: 1196, price2: 1498 },
{complexity: 7, check: "No", price1: 1052, price2: 1353 },
{complexity: 8, check: "Yes", price1: 1438, price2: 1449 },
{complexity: 8, check: "No", price1: 1124, price2: 1293 }
{complexity: 9, check: "Yes", price1: 1498, price2: 1631 },
{complexity: 9, check: "No", price1: 1221, price2: 1449 },
{complexity: 10, check: "Yes", price1: 1836, price2: 1643},
{complexity: 10, check: "No", price1: 1341, price2: 1498 }
];
for(var i in prices){
if(cpx == prices[i].complexity && chk == prices[i].check){
total1 = prices[i].price1;
total2 = prices[i].price2;}}
this.getField("Text1").value = total1;
this.getField("Text2").value = total2;
Copy link to clipboard
Copied
You are missing comma after } in these lines:
complexity: 2, check: "No", price1: 532, price2: 979 }
{complexity: 4, check: "No", price1: 749, price2: 1124 }
{complexity: 6, check: "No", price1: 918, price2: 1196 }
{complexity: 8, check: "No", price1: 1124, price2: 1293 }
Copy link to clipboard
Copied
Thank you! 😃
Copy link to clipboard
Copied
How do I adjust this script so the user can write over it?
Copy link to clipboard
Copied
You mean write over the "Text1" and "Text2" field values.
To do this we need to know where the script is placed. Is this a calculation script on "Text1" as suggested by Nesa?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
In order to block a calculation and allow user input, there has to be some way for the calculation script to know the user has entered something that is going to override the calculation. A really simple way to do this is to put a checkbox next to the field and use it to store the override value. The checkbox can be visible or invisible. Depends on how you want the user to interact with the form. But lets just say it's visible and the user clicks on it to allow user entry. The checkbox will be named "CalcOverride".
Add this line to the top of the calculation script in the fields that are to be overridden.
event.rc = this.getField("CalcOverride").value != "Off";
Copy link to clipboard
Copied
Copy link to clipboard
Copied
That's because of how the calculation script is written.
First, always return the calculated value for a field in the "event.value property".
So replace this line:
this.getField("Text1").value = total1;
with this one:
event.value = total1;
Next, since the script aslo sets a value in a different field, the entire calculation has to be qualified in order to allow user entry into the "Text2" field.
So put the whole calculation into an "if" statement
event.rc = this.getField("CalcOverride").value != "Off";
if(event.rc)
{
..... calculation script ...
}
There are a few other things that could be improved with this script. For example, the data could be moved to a document level script so it's global to the entire document. And the loop for finding the matching data could be simplified by useing the Array.find() method.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Nevermind, I noticed Text23 wasn't adding Text1