• Global community
• Language:
• Deutsch
• English
• Español
• Français
• Português
• 日本語コミュニティ
Dedicated community for Japanese speakers
• 한국 커뮤니티
Dedicated community for Korean speakers
Exit
• Re: Populating values when a checkbox selected and...

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

Explorer ,
Nov 27, 2023 Nov 27, 2023

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?

TOPICS
How to , JavaScript , PDF , PDF forms

Views

1.9K

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

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

...

15 Replies 15
Nov 27, 2023 Nov 27, 2023

Copied

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/

Hope it will help

Regards
Amal

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Nov 28, 2023 Nov 28, 2023

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.

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

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

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

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

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Nov 28, 2023 Nov 28, 2023

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;

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

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 }

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Nov 28, 2023 Nov 28, 2023

Copied

Thank you! 😃

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Mar 06, 2024 Mar 06, 2024

Copied

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

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

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

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Mar 08, 2024 Mar 08, 2024

Copied

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

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

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

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Mar 12, 2024 Mar 12, 2024

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?

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

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

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Mar 13, 2024 Mar 13, 2024

Copied

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

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Mar 13, 2024 Mar 13, 2024

Copied

LATEST

Nevermind, I noticed Text23 wasn't adding Text1