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

Populating values when a checkbox selected and data is in a field

Explorer ,
Nov 27, 2023 Nov 27, 2023

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?

Badhawg66_0-1701117107649.png

Badhawg66_1-1701117134127.png

Badhawg66_2-1701117149242.png

TOPICS
How to , JavaScript , PDF , PDF forms

Views

2.2K

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 2 Correct answers

Community Expert , Nov 28, 2023 Nov 28, 2023

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: 
...

Votes

Translate

Translate
Community Expert , Mar 12, 2024 Mar 12, 2024

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("CalcOver

...

Votes

Translate

Translate
Adobe Employee ,
Nov 27, 2023 Nov 27, 2023

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

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 ,
Nov 28, 2023 Nov 28, 2023

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.

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 ,
Nov 27, 2023 Nov 27, 2023

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

 

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
Community Expert ,
Nov 28, 2023 Nov 28, 2023

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;

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 ,
Nov 28, 2023 Nov 28, 2023

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;

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 ,
Nov 28, 2023 Nov 28, 2023

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 }

 

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 ,
Nov 28, 2023 Nov 28, 2023

Copy link to clipboard

Copied

Thank you! 😃

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 ,
Mar 06, 2024 Mar 06, 2024

Copy link to clipboard

Copied

How do I adjust this script so the user can write over it?

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 ,
Mar 08, 2024 Mar 08, 2024

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?

 

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 ,
Mar 08, 2024 Mar 08, 2024

Copy link to clipboard

Copied

Yes sir, I would like to write over the field values in Text1-Text22 of this file

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 ,
Mar 12, 2024 Mar 12, 2024

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

 

 

 

 

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 ,
Mar 12, 2024 Mar 12, 2024

Copy link to clipboard

Copied

I created a "CalcOverride" checkbox and entered your code in "Text1" to see if it would work but it still auto populates if "CalcOverride" is checked or not. Any suggestions?

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 ,
Mar 12, 2024 Mar 12, 2024

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. 

 

 

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 ,
Mar 13, 2024 Mar 13, 2024

Copy link to clipboard

Copied

I'm able to manually enter values now but Text23 doesn't add Text1-22 correctly

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 ,
Mar 13, 2024 Mar 13, 2024

Copy link to clipboard

Copied

LATEST

Nevermind, I noticed Text23 wasn't adding Text1

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