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

Help with Custom Calculations

Explorer ,
Sep 13, 2023 Sep 13, 2023

Hello,

 

I'm preparing a form that calculates a projects complexity based off of the project length and the number of utilities. The Project Length field will either be less than, greater than or equal to 1 mile and the No of UTs field would be a range of utilities. For example, a project with 7-10 utilities and a length of 1 mile or more would be a complexity level 7. I couldn't find a discussion where someone had the same problem.Screenshot 2023-09-13 105029.pngexpand image

TOPICS
How to , JavaScript , Modern Acrobat , PDF , PDF forms
2.5K
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
1 ACCEPTED SOLUTION
Explorer ,
Nov 27, 2023 Nov 27, 2023

I wish I knew how to use variables but this script seems to work just fine. Thank you for your help!

 

if((this.getField("No of UTs").value == 1))
{
event.value = 1;
}
if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value <= 3))
{
event.value = 2;
}
if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value >= 3))
{
event.value = 3;
}
if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value = 4))
{
event.value = 4;
}

if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value = 5))
{
event.value = 4;
}

if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value = 6))
{
event.value = 4;
}
if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value = 4))
{
event.value = 5;
}

if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value = 5))
{
event.value = 5;

}
if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value = 6))
{
event.value = 5;
}
if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value = 7))
{
event.value = 6;

}

if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value = 8))
{
event.value = 6;
}

if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value = 9))
{
event.value = 6;
}

if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value = 10))
{
event.value = 6;
}

if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value = 7))

{
event.value = 7;
}

if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value = 8))

{
event.value = 7;
}

if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value = 9))

{
event.value = 7;
}

if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value = 10))

{
event.value = 7;
}

if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value = 11))
{
event.value = 8;
}

if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value = 12))
{
event.value = 8;
}

if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value = 13))
{
event.value = 8;
}

if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value = 14))
{
event.value = 8;
}

if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value = 15))
{
event.value = 8;
}

if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value = 11))
{
event.value = 9;
}

if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value = 12))
{
event.value = 9;
}

if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value = 13))
{
event.value = 9;
}

if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value = 14))
{
event.value = 9;
}

if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value = 15))
{
event.value = 9;
}


if((this.getField("No of UTs").value >= 15))
{
event.value = 10;
}
else
{
// do nothing
}

View solution in original post

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 ,
Sep 13, 2023 Sep 13, 2023

You need to better define the formula for this calculation...

> The Project Length field will either be less than, greater than or equal to 1 mile

So basically, any possible value...

> and the No of UTs field would be a range of utilities.

Not very descriptive...

> For example, a project with 7-10 utilities and a length of 1 mile or more would be a complexity level 7.

Why? What's the formula to arrive to that? Why not 10? Why not 8? Is it the product of the length by the number of utilities? What's the cut-off? A lot of info missing there.

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
Explorer ,
Sep 13, 2023 Sep 13, 2023

I apologize for being so vague. I'm trying to get rid of this cheat sheet I use. I'm hoping there's a way to make a script or multiple scripts that will help us figure out the complexity based off of the project length and number of utilities.

Screenshot 2023-09-13 105443.pngexpand image

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 ,
Sep 14, 2023 Sep 14, 2023

I still don't understand how these values are specified by the user. Are you using a drop-down?

Can you share the actual file?

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
Explorer ,
Sep 18, 2023 Sep 18, 2023

No but you could use a drop down for the No of UTs field. Here is the file I'm working with.

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
Explorer ,
Nov 07, 2023 Nov 07, 2023

try67,

 

What am I doing wrong? I get ReferenceError: invalid assignment left-hand side 1: at line 2. Ln 12, Col 2

 

Here is the script I used...

 

if((this.getField("No of UTs#1").value == ('Proj Length' = 1))
{
event.value = 1;
}

if((this.getField("Proj Length#1").value == ('Proj Length' < 1)) && (this.getField("No of UTs#1").value == ("No of UTs#1" ≤ 3"))

{

event.value = 2;

}

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

Following is wrong:

'Proj Length' = 1

You can't assign a value to a string.

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

There are a lot of issues with your code. Bernd pointed out the main one, but this is also wrong:

if((this.getField("Proj Length#1").value == ('Proj Length' < 1))

You're combining multiple comparisons in a way that doesn't make sense.

Also, "≤ " is not a valid operator in JS.

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
Explorer ,
Nov 07, 2023 Nov 07, 2023

I think I understand what you guys are trying to say. Does this look better?

 

if(this.getField("No of UTs#1").value == 1)
{
event.value = 1;
}

if(this.getField("Proj Length#1").value < 1) && (this.getField("No of UTs#1").value <= 3)

{

event.value = 2;

}

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

Yes, much better, although you need to consider what will happen if neither condition is true.

Currently, whatever value the field had before will stay in place. Not sure that's what you want to happen...

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

Here is my script (It says I have syntax errors but I don't know what they are or how to fix them) If none of the conditions are true I want the Complexity field to stay blank. What am I doing wrong?

 

if(this.getField("No of UTs#1").value == 1)
{
event.value = 1;
}
if(this.getField("Proj Length#1").value < 1) && (this.getField("No of UTs#1").value <= 3)
{
event.value = 2;
}
if(this.getField("Proj Length#1").value > 1) && (this.getField("No of UTs#1").value >= 3)
{
event.value = 3;
}
if(this.getField("Proj Length#1").value < 1) && (this.getField("No of UTs#1").value >= 4 && <=6)
{
event.value = 4;
}
if(this.getField("Proj Length#1").value > 1) && (this.getField("No of UTs#1").value >= 4 && <=6)
{
event.value = 5;
}
if(this.getField("Proj Length#1").value < 1) && (this.getField("No of UTs#1").value >= 7 && <=10)
{
event.value = 6;
}
if(this.getField("Proj Length#1").value > 1) && (this.getField("No of UTs#1").value >= 7 && <=10)
{
event.value = 7;
}

if(this.getField("Proj Length#1").value < 1) && (this.getField("No of UTs#1").value >= 11 && <=15)
{
event.value = 8;
}
if(this.getField("Proj Length#1").value > 1) && (this.getField("No of UTs#1").value >= 11 && <=15)
{
event.value = 9;
}

if(this.getField("No of UTs#1").value >= 15)
{
event.value = 10;

}

else

{

// do nothing

}

 

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

Each condition after the if-statement has to be encapsulated in a set of parentheses.

So instead of this:

if (this.getField("Proj Length#1").value < 1) && (this.getField("No of UTs#1").value <= 3) {

You must use this:

if ((this.getField("Proj Length#1").value < 1) && (this.getField("No of UTs#1").value <= 3)) {

etc.

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

So like this? It says line 14 has an invalid XML name (I deleted the # by the field names but that didn't fix problem)

 

if((this.getField("No of UTs").value == 1))
{
event.value = 1;
}
if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value <= 3))
{
event.value = 2;
}
if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value >= 3))
{
event.value = 3;
}
if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value >= 4 && <=6))
{
event.value = 4;
}
if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value >= 4 && <=6))
{
event.value = 5;
}
if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value >= 7 && <=10))
{
event.value = 6;
}
if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value >= 7 && <=10))
{
event.value = 7;
}

if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value >= 11 && <=15))
{
event.value = 8;
}
if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value >= 11 && <=15))
{
event.value = 9;
}
if((this.getField("No of UTs").value >= 15))
{
event.value = 10;
}
else
{
// do nothing
}

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

This is not valid:

(this.getField("No of UTs").value >= 4 && <= 6)

You need to either use a variable, or repeat the full statement for each condition, like this:

(this.getField("No of UTs").value >= 4 && this.getField("No of UTs").value <= 6)

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

I wish I knew how to use variables but this script seems to work just fine. Thank you for your help!

 

if((this.getField("No of UTs").value == 1))
{
event.value = 1;
}
if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value <= 3))
{
event.value = 2;
}
if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value >= 3))
{
event.value = 3;
}
if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value = 4))
{
event.value = 4;
}

if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value = 5))
{
event.value = 4;
}

if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value = 6))
{
event.value = 4;
}
if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value = 4))
{
event.value = 5;
}

if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value = 5))
{
event.value = 5;

}
if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value = 6))
{
event.value = 5;
}
if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value = 7))
{
event.value = 6;

}

if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value = 8))
{
event.value = 6;
}

if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value = 9))
{
event.value = 6;
}

if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value = 10))
{
event.value = 6;
}

if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value = 7))

{
event.value = 7;
}

if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value = 8))

{
event.value = 7;
}

if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value = 9))

{
event.value = 7;
}

if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value = 10))

{
event.value = 7;
}

if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value = 11))
{
event.value = 8;
}

if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value = 12))
{
event.value = 8;
}

if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value = 13))
{
event.value = 8;
}

if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value = 14))
{
event.value = 8;
}

if((this.getField("Proj Length").value < 1) && (this.getField("No of UTs").value = 15))
{
event.value = 8;
}

if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value = 11))
{
event.value = 9;
}

if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value = 12))
{
event.value = 9;
}

if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value = 13))
{
event.value = 9;
}

if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value = 14))
{
event.value = 9;
}

if((this.getField("Proj Length").value > 1) && (this.getField("No of UTs").value = 15))
{
event.value = 9;
}


if((this.getField("No of UTs").value >= 15))
{
event.value = 10;
}
else
{
// do nothing
}

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

It's quite simple. You define them once at the start of your code with the values of the fields, and then use them directly instead of accessing the fields each time. Something like this:

 

var numUTs = Number(this.getField("No of UTs").value);
var projLength = Number(this.getField("Proj Length").value);
if(numUTs == 1)
{
event.value = 1;
}
if((projLength < 1) && (numUTs <= 3))

etc.

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