Arcade scripting in Attribute Rules

629
19
04-01-2021 11:15 AM
LorindaGilbert
Occasional Contributor

Hi All,

I've gotten over my hurtle with creating attribute rules now, but I'm a newbie with Arcade - either in the WAB or in Pro.

I'm attempting to create a Calculation rule that is based on If Then Else statements.  I am getting a message about Invalid expression.  Error on line 8.  Open parenthesis expected.

Here's the beginning of my expression; at the end I do have return eru;  As this is the field that I am attempting to calculate.  There will be three of these set up so as to do the required pre-calcs before getting the last two calculations that are the real ones to be returned and saved.

It is highly likely that I'm missing just something simple, I thought that I was following the examples that I found for doing calcs and using if statements.  But ....

var fs = FeatureSetByName($datastore,"IDF_Stormwater");
var billclass = fs["MultiUserType"];
var eru = fs["ERUArea"];
var parea = fs["IDFArea"];
var pimparea = fs["ImperviousArea"];
var dwell = fs["Dwellings"];

if billclass == 'D'
eru = 0
else if billclass in ('V','I','H')
eru = Round(parea/43560,2)
else if billclass == 'R'
eru = Round(parea*(2000/7760)/2000,2)
else if billclass in ('A','O')

.....

Thanks,

Lorinda

0 Kudos
19 Replies
Luke_Pinner
MVP Regular Contributor

Not altogether familiar with arcade, but it uses Javascript like syntax.

So your statement should look something like:

 

if (condition1) {
  //  block of code to be executed if condition1 is true
} else if (condition2) {
  //  block of code to be executed if condition1 is false and condition2 is true
} else {
  //  block of code to be executed if the condition1 is false and condition2 is false
}

 

 

 For your code, something like:

 

if (billclass == 'D') {
  eru = 0
} else if (['V','I','H'].indexOf(billclass) >= 0) {  
    eru = Round(parea/43560,2)
} else if (billclass == 'R') {
    eru = Round(parea*(2000/7760)/2000,2)
} etc...
return eru;

 

 

Note there's no "in" operator, so even if you got the if and else if syntax correct, you'll get a Syntax Error: Binary Operator not recognised in so I've used the indexOf function and I've changed your python-like tuples ('V','I','H') to JavaScript-like arrays ['V','I','H']

I recommend you look at the arcade reference.

KenBuja
MVP Honored Contributor

You can leave off the brackets if there is only one line in the condition, although it's good practice to use them. This code does work

var test = "";
if ($feature.name1 == 'value1') 
  test = 1;
else if ($feature.name1 == 'value2')
  test = 2;
return test;
LorindaGilbert
Occasional Contributor

Hi All,

I've gotten past that first hurtle, have three attribute rules validating and saved in the project - still have to test to see if they are doing as expected though.  That's another day.

I've now run into a situation where I need to do another if/then/else statement, but need to have it with two parts.  If field1 == value && field2 between "0123" and "1789" - yes, the values are stored as text.

Can this be done?  I've tried to find some examples, but may not be putting the proper search string in.

Thanks for your help!

Lorinda

0 Kudos
JoeBorgione
MVP Esteemed Contributor

Check out the logic page for arcade

Also, you can use the Arcade Playground to do just that: play with Arcade.  I played around with this statement there until I got it right...

 

var field1 = 12
var field2 = 250

if (field1 == 12 && field2 < 300 && field2 > 200){
    Console("yay")}

 

The playground is the only place I know where the Console() function works. It's the same as the print() function in python.

can't wait to retire....
0 Kudos
LorindaGilbert
Occasional Contributor

Where do you find this playground for testing commands?  I've not seen it yet.  I can see where it could be really useful.

BTW, love your 'signature'; I can relate 😅

Thanks,

Lorinda

0 Kudos
JoeBorgione
MVP Esteemed Contributor

click in the link in my post...

Also, depending on what you are using, you will be presented the same Arcade edit window, which allows you, test, access global variables and functions.

 

 

can't wait to retire....
0 Kudos
LorindaGilbert
Occasional Contributor

Thanks, was going to edit the post to say that I saw the link afterwards, but you beat me to it.  Hazards of having an eye infection, things are a bit blurry.

I'll take a look at your suggestions, I've been wracking my brain trying to figure out the proper terms for looking up some of these things and having troubles with it.  But bookmarking several items for the next go around with this exercise.  Have a few legacy apps to migrate to new and hopefully improved processes.

Thanks,

Lorinda

0 Kudos
JoeBorgione
MVP Esteemed Contributor

I've been attending the developers summit yesterday and today, and have learned a ton of Arcade tips and tricks...

can't wait to retire....
LorindaGilbert
Occasional Contributor

Hi All,

I really appreciate the help that's being offered.  I've been searching for how to do nested if then now and have gotten hung up again.  I'm sure that it is something that I'm overlooking, but I've tried many iterations and still get that I have 'Unexpected Identifier' on line 31, the last else if.  This is the testing that I'm using in the playground. 

Thanks,

Lorinda

var billclass = "V"
var dor = 6400
var parea = 3457
var newfee = 0
var pimparea = 1500;
var dwell = 2;
var ouswmm = "Y"

if (parea > 0 && (billclass == "V") && (dor < 6647 && dor > 5399)) {
newfee = Round(((parea/43560)*(12*0.1*5.99))+119.88,2)
}
else if (parea > 43560 && (billclass == "V") && (dor < 5400 || dor > 6647)) {
newfee = Round((parea/43560)*(12*(4.0 +(0.2*5.99))),2)
}
else if (parea <= 43560 && (billclass == "V")) {
newfee = Round((12*(4.0 +(0.2*5.99))),2)
}
else {
if ((billclass == "M") && ((pimparea/2000) < (1.15 * dwell)) && (ouswmm == "N")) {
newfee = Round((pimparea/2000)*(12*(4.00 + (1.0* 5.99))),2)
}
else if ((billclass == "M") && (pimparea/2000 < (1.15 * dwell)) && (ouswmm == "Y")) {
newfee = Round((pimparea/2000)*(12*(4.00 + (0.3* 5.99))),2)
}
else if ((billclass == "M") && (pimparea/2000 >= (1.15 * dwell)) && (ouswmm == "N")) {
newfee = ((119.88*1.15)*dwell)
}
else if ((billclass == "M") && (pimparea/2000 >= (1.15 * dwell)) && (ouswmm == "Y")) {
newfee = ((69.56*1.15)*dwell)
}
else if billclass == "M" && newfee < 5 {
newfee = 5
}
}
}
Console (newfee)

0 Kudos
KenBuja
MVP Honored Contributor

You're missing parenthesis around the last else if and you have an extra "}" at the end. It's easier to see this if you indent the code.

var billclass = "V"
var dor = 6400
var parea = 3457
var newfee = 0
var pimparea = 1500;
var dwell = 2;
var ouswmm = "Y"

if (parea > 0 && (billclass == "V") && (dor < 6647 && dor > 5399)) {
  newfee = Round(((parea / 43560) * (12 * 0.1 * 5.99)) + 119.88, 2)
}
else if (parea > 43560 && (billclass == "V") && (dor < 5400 || dor > 6647)) {
  newfee = Round((parea / 43560) * (12 * (4.0 + (0.2 * 5.99))), 2)
}
else if (parea <= 43560 && (billclass == "V")) {
  newfee = Round((12 * (4.0 + (0.2 * 5.99))), 2)
}
else {
  if ((billclass == "M") && ((pimparea / 2000) < (1.15 * dwell)) && (ouswmm == "N")) {
    newfee = Round((pimparea / 2000) * (12 * (4.00 + (1.0 * 5.99))), 2)
  }
  else if ((billclass == "M") && (pimparea / 2000 < (1.15 * dwell)) && (ouswmm == "Y")) {
    newfee = Round((pimparea / 2000) * (12 * (4.00 + (0.3 * 5.99))), 2)
  }
  else if ((billclass == "M") && (pimparea / 2000 >= (1.15 * dwell)) && (ouswmm == "N")) {
    newfee = ((119.88 * 1.15) * dwell)
  }
  else if ((billclass == "M") && (pimparea / 2000 >= (1.15 * dwell)) && (ouswmm == "Y")) {
    newfee = ((69.56 * 1.15) * dwell)
  }
  else if (billclass == "M" && newfee < 5) {
    newfee = 5
  }
}

Console(newfee)