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

Custom Calculation Script Help

New Here ,
Oct 06, 2022 Oct 06, 2022

So I'm attempting to create a custom calculation script and running into kind of an infinite loop, and JS really isn't my forte. Script is as follows.

//Acquire Value

var v1 =

this.getField("1").valueAsString;

//Calculate Adjustment

if (v1=="") event.value = "";

else {

      var v2=Number(v1);

      var v3=v2.toFixed(2);

      var v4=Math.round((v3 - 886.43) * 100)/100;

 

event.value=v4.toFixed(2)

}

This script works fine at what I want it to do, which is to take entry from Field 1, subtract it by a set value, and then place the result with 2 significant figures in field 2. The issue I run into however, is ideally, I want both field 1 and 2 to be able to function in this fashion. To hopefully explain, Field 1 is an adjust to field, so inputting a number there will adjust the set value to that number, and the adjustment is calculated and input into field 2. Field 2 is an adjustment field, so whatever you put in that field, the set value will be increased or decreased by that value (if its a negative input).  However using the same script in both fields but changing Field 1's script to show the sum of the value and field 2 seems to result in a somewhat infinite loop. I'm unable to type in a value into field 2 and have it do anything, but if I type into field 1, the field 2 script runs fine, and only then does field 1 try to run the script. In an ideal world, I would really only want each script to run if and ONLY if you manually type data into one field or the other. I'm not sure if thats really possible but I've exhausted every resource I could find. Any input would be appreciated 🙂

 

TOPICS
JavaScript
1.3K
Translate
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 ,
Oct 06, 2022 Oct 06, 2022

I don't fully understand what you want, but try using scripts as validation, not calculation.

Translate
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
New Here ,
Oct 06, 2022 Oct 06, 2022

Hopefully I can try to explain it a little better, its a practice tool showing how a tool used to adjust an account works. 

So typically the tool will show you the current balance on the account, and theres two fields for you to enter data into to alter the balance. One of them being an, "Adjust To" field. So any value in this field will change the balance to the data input. The other being an, "Adjustment Field". So any data input here will either be added to or subtracted from the balance.

I'm trying to recreate this function in an adobe form that looks like the tool.

So the way i achieved this was by using calculation to subtract the balance from whatever you input in the adjust to field, and that calculates your adjustment amount. I have not been able to make it so both fields work to change the other however. So I made the adjustment field read only, and the user only puts data into the adjust to box. Ideally I would want it to calculate the adjust to field if you put data into the adjustment box, and vice versa.

Translate
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 ,
Oct 06, 2022 Oct 06, 2022

So basically you wish to reverse calculation, for example if you enter 100 in "Adjust To" field you will get -786.43 in "Adjustment Field" but if you change value in "Adjustment Field" for this example to -686.43 you want "Adjust To" field to show 200?

Translate
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
New Here ,
Oct 06, 2022 Oct 06, 2022

Effectively, In the instance the account has a $10,000 balance, and I wanted to increase that, I could either do it by putting $11,000 in the adjust to field, which would then calculate the adjustment as $1,000. But also working the other way, I could input $1000 and the Adjust to Field should calculate to $11,000.

Translate
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 ,
Oct 06, 2022 Oct 06, 2022

Ok, where do you get balance from, is it a 3rd field or?

Translate
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
New Here ,
Oct 06, 2022 Oct 06, 2022

Currently, I just input the balance as part of the script, but the plan is to make it pull it from a field eventually. Its a practice exercise at this point, so I just put whatever is displayed as the balance in the sum calculation for the adjustment. 

Translate
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 ,
Oct 06, 2022 Oct 06, 2022
LATEST

Then use scripts as 'Validation' of each field something like this:

Validation script of "Adjust To" field:
var balance = 10000;
var adj = this.getField("Adjustment Field");

if(event.value == "") adj.value = "";
else
adj.value = Number(event.value)-balance;


Validation script of "Adjustement Field" field:
var balance = 10000;
var at = this.getField("Adjust To");

if(event.value == "") at.value = "";
else
at.value = Number(event.value)+balance;

Translate
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