dynamic calculations

Participant ,
Mar 26, 2021 Mar 26, 2021

Copy link to clipboard

Copied

Hello!

I am making a form with the purpose of having different simple calculations depending on a dropdown field:

Depending on Dropdown make field 3 calculate the feild1* feild2

I need this calculation to be able to change when feild1 or feild2 is edited.

 

I know how to change field values depending on dropdowns but I don't know what script I would use for changing the calculation inside a certain field.

 

Any help would be greatly apreciated!

Thank you in advance.

TOPICS
Create PDFs, Edit and convert PDFs, General troubleshooting, How to, JavaScript, PDF forms

Views

3.9K

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

correct answers 1 Correct Answer

Adobe Community Professional , Mar 27, 2021 Mar 27, 2021
Try this as calculation script:if(Number(this.getField("ID1").value) >0 && Number(this.getField("ID1").value) <= 999) event.value = Number(this.getField("No1").value)*Number(this.getField("Price1").value);

Likes

Translate

Translate
Adobe Community Professional ,
Mar 26, 2021 Mar 26, 2021

Copy link to clipboard

Copied

Be a  little more specific.

 

What exactly is the dropdown menu providing in its list: Numbers?   Words?  Listed items with an export value?

 

How many items are in that dropdown menu and What item exactly needs to be selected from that list in order for field 3 to execute the calculating event?

 

If I understood correctly, field 3 will display the product of field1* field2 values. Correct? But if any of those two fields values are manually edited by the user, and as long as the desired list item remains selected in the dropdown menu, the value in field 3 must update when the user inputs a different value in fields1 and fields2 respectively, correct?

 

I think that you may be able to accomplish this using a custom calculation script in field 3.

 

In my example below I created a dropdwon menu named "myDropdown", and I also  added three items to that list: "item1", "item2", "item3".

 

So in my example,  when the user selects"item2" from dropdown field 3 it  automatically multiply whatever input values are in field1 and field2.

 

 

 

var f = this.getField("myDropdown").value;

  if (f != "item2" && (this.getField("field1").value !=="" || this.getField("field2").value !=="") )  {

  event.value =  "";

} else {

  event.value = this.getField("field1").value * this.getField("field2").value;

}

 

 

The script above is an example and it can be improved, but you may need to be more specific about what you're trying to accomplish.

 

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
Participant ,
Mar 26, 2021 Mar 26, 2021

Copy link to clipboard

Copied

My goal is to be able to change the calculations in certain fields like so:

Screen Shot 2021-03-27 at 3.43.51 pm.png

I want to be able to edit the calculation through here, is there a way javascript can do this (maybe by selecting/unselecting this option?

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
Participant ,
Mar 26, 2021 Mar 26, 2021

Copy link to clipboard

Copied

@ls_rbls 
The reason I want it done this way is that I can still edit these fields that are being multiplied.

Thanks for your response

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 ,
Mar 27, 2021 Mar 27, 2021

Copy link to clipboard

Copied

There are few ways to do this, but if you want to use "Value is the", as Validation script of same field you can use event.rc to stop calculations if for example specific item is selected in dropdown field and turn them back on if specific item is no longer selected.

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
Participant ,
Mar 27, 2021 Mar 27, 2021

Copy link to clipboard

Copied

@Nesa Nurani 

where would I be typing event.rc? in validation script?

And also do you have any example of this code?

thanks you for your reply!

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 ,
Mar 27, 2021 Mar 27, 2021

Copy link to clipboard

Copied

Yes, in validation.

Lets say you want to stop calculations when "item1" is selected from dropdown:

if(this.getField("Dropdown").valueAsString == "item1")
event.rc = false;
else event.rc = true;

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
Participant ,
Mar 27, 2021 Mar 27, 2021

Copy link to clipboard

Copied

@Nesa Nurani 

I'm having trouble getting this to work, it seems that nothing is happening.

if(this.getField("ID1").valueAsString == "0" || this.getField("ID1").valueAsString == "" || this.getField("ID1").valueAsString == " "){
event.rc = false;
}
else{
event.rc = true;
}

 What am I doing wrong?

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 ,
Mar 27, 2021 Mar 27, 2021

Copy link to clipboard

Copied

When you enter code perhaps your calculation got removed, go to calculation tab and make calculation again.

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
Participant ,
Mar 27, 2021 Mar 27, 2021

Copy link to clipboard

Copied

@Nesa Nurani 
The calculation is still there, I've also retyped it in.
Maybe it's because it isn't in the custom calculation script area?

Screen Shot 2021-03-27 at 3.43.51 pm.png


The problem is the calculation won't t urn off

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 ,
Mar 27, 2021 Mar 27, 2021

Copy link to clipboard

Copied

Can you share file so I can take a look?

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
Participant ,
Mar 27, 2021 Mar 27, 2021

Copy link to clipboard

Copied

@Nesa Nurani 

Thank you!

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
Most Valuable Participant ,
Mar 27, 2021 Mar 27, 2021

Copy link to clipboard

Copied

Setting event.rc is not going to work. This only applies to user-inputted values, not automatically calculated ones.

Instead, I would do it all using a custom calculation script (not a validation one, either).

If you provide the exact specifications of how it should work we can help you out with the code to achieve it.

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
Participant ,
Mar 27, 2021 Mar 27, 2021

Copy link to clipboard

Copied

@try67 

This is all I'm after! I think if I have this information I should be able to understand how to alter it.
Depending on Dropdown make field 3 calculate feild1 Multiplied by feild2
Thank you for your help!

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
Most Valuable Participant ,
Mar 27, 2021 Mar 27, 2021

Copy link to clipboard

Copied

This is not enough. You need to provide more details, such as:

- What are the exact field names of the fields involved?

- What values of the "drop-down field" should cause Field3 to be Field1*Field2?

- What should be the value of Field3 when another value is selected under the "drop-down field"?

 

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
Participant ,
Mar 27, 2021 Mar 27, 2021

Copy link to clipboard

Copied

@try67 

- What are the exact field names of the fields involved?

Depending on Dropdown ("ID1") make field 3("Total1") calculate feild1("No1") Multiplied by feild2 (Price1)

- What values of the "drop-down field" should cause Field3 to be Field1*Field2?

The values are any number from 1 - 999

- What should be the value of Field3 when another value is selected under the "drop-down field"?

There should be no calculation on Field3 ("Total1") The value of the field itself should be able to be editable.

 

Thank you for all your help.

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 ,
Mar 27, 2021 Mar 27, 2021

Copy link to clipboard

Copied

Wait, so you just want to multiply field1*field2 depending on dropdown choice?

 

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 ,
Mar 27, 2021 Mar 27, 2021

Copy link to clipboard

Copied

Try this as calculation script:

if(Number(this.getField("ID1").value) >0 && Number(this.getField("ID1").value) <= 999)
event.value = Number(this.getField("No1").value)*Number(this.getField("Price1").value);

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
Participant ,
Mar 27, 2021 Mar 27, 2021

Copy link to clipboard

Copied

LATEST

@Nesa Nurani 
haha yep, I don't know why I didn't think this work!

 

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