Arcade Calculate expression in Map Viewer Forms

587
7
Jump to solution
04-18-2023 08:56 AM
Labels (1)
LeilaJackson1
Occasional Contributor III

Hi - 

I am still new to Arcade and need some help identifying what is wrong with my below expression. I have 4 questions (see image 1). Question 1 and 4 are answered in all cases. Question 2 and 3 are answered dependent upon another question indicating whether the project is an engineered or natural solution. If they answer Engineered, then they answer Question 2, if they answer Natural, they answer question 3. There is logic that makes the questions editable depending upon the initial response. The response to each question receives a score of 0, 10 or 20.  We then give bonus points (Highest Possible Score question in image) based upon how many questions received 20 points using the below logic (image 2). My code does not return a number if any question=20, if they are all < 20 it correctly returns 0. Can anyone tell me what I am doing wrong?

//all highest
if ($feature.PR_RESF1_SCORE==20 && $feature.PR_RESF2ENG_SCORE==20 && $feature.PR_RESF3_SCORE==20) { //All highest - engineering
return 20;

} else if ($feature.PR_RESF1_SCORE==20 && $feature.PR_RESF2NAT_SCORE==20 && $feature.PR_RESF3_SCORE==20) { //All highest - natural
return 20;

//2 of 3 highest

} else if ($feature.PR_RESF1_SCORE==20 && $feature.PR_RESF2ENG_SCORE==20 && $feature.PR_RESF3_SCORE<20) { //RF1 and 2 highest - engineering
return 10;

} else if ($feature.PR_RESF1_SCORE==20 && $feature.PR_RESF2NAT_SCORE==20 && $feature.PR_RESF3_SCORE<20) { //RF1 and 2 highest - natural
return 10;

} else if ($feature.PR_RESF1_SCORE==20 && $feature.PR_RESF2ENG_SCORE<20 && $feature.PR_RESF3_SCORE==20) { //RF1 and 3 highest  - engineering
return 10;

} else if ($feature.PR_RESF1_SCORE==20 && $feature.PR_RESF2NAT_SCORE<20 && $feature.PR_RESF3_SCORE==20) { //RF1 and 3 highest  - natural
return 10;

} else if ($feature.PR_RESF1_SCORE<20 && $feature.PR_RESF2ENG_SCORE==20 && $feature.PR_RESF3_SCORE==20) { //RF2 and 3 highest  - engineering
return 10;

} else if ($feature.PR_RESF1_SCORE<20 && $feature.PR_RESF2NAT_SCORE==20 && $feature.PR_RESF3_SCORE==20) { //RF2 and 3 highest  - natural
return 10;

//1 of 3 highest

} else if ($feature.PR_RESF1_SCORE==20 && $feature.PR_RESF2ENG_SCORE<20 && $feature.PR_RESF3_SCORE<20) { //only RF1 highest - engineering
return 5;

} else if ($feature.PR_RESF1_SCORE==20 && $feature.PR_RESF2NAT_SCORE<20 && $feature.PR_RESF3_SCORE<20) { //only RF1 highest - natural
return 5;

} else if ($feature.PR_RESF1_SCORE<20 && $feature.PR_RESF2ENG_SCORE==20 && $feature.PR_RESF3_SCORE<20) { //only RF2 highest  - engineering
return 5;

} else if ($feature.PR_RESF1_SCORE<20 && $feature.PR_RESF2NAT_SCORE==20 && $feature.PR_RESF3_SCORE<20) { //only RF2 highest  - natural
return 5;

} else if ($feature.PR_RESF1_SCORE<20 && $feature.PR_RESF2ENG_SCORE<20 && $feature.PR_RESF3_SCORE==20) { //only RF3 highest  - engineering
return 5;

} else if ($feature.PR_RESF1_SCORE<20 && $feature.PR_RESF2NAT_SCORE<20 && $feature.PR_RESF3_SCORE==20) { //only RF3 highest  - natural
return 5;

//None are highest
} else if ($feature.PR_RESF1_SCORE<20 && $feature.PR_RESF2ENG_SCORE<20 && $feature.PR_RESF3_SCORE<20) { //none are highest  - engineering
return 0;

} else if ($feature.PR_RESF1_SCORE<20 && $feature.PR_RESF2NAT_SCORE<20 && $feature.PR_RESF3_SCORE<20) { //none are highest  - natural
return 0;
}


LeilaJackson1_2-1681833006969.png

 

LeilaJackson1_1-1681832773395.png

 

 

0 Kudos
1 Solution

Accepted Solutions
LeilaJackson1
Occasional Contributor III

@JoshuaSharp-Heward  Thanks for your assistance on this - I really appreciate it. Knowing that you could get it to run, I started tracing back my steps and finally found the root cause. The Score fields are all short integers, but are calculated based upon select one questions. In those calculation scripts I had my numbers in single quotes which was causing the problem.

if ($feature.PR_RESF1=='1. RSS asset(s) BRE meet ALR'){

  return '0';

 

I removed the single quotes, and the script is now working as expected :-). Being new to Arcade, I have to say I do not know when to use double quotes, single quotes or no quotes. It is often trial by error, but in this case the calculations for the score fields were running as expected and returning the correct values, so I did not know the single quotes were problematic.

Thanks again!

View solution in original post

0 Kudos
7 Replies
JoshuaSharp-Heward
Occasional Contributor III

Hi,

I've gone for a slightly different approach to the problem as I think with that many if/else if clauses it can be tricky to work out what's going on.

var RESF1Score = $feature.PR_RESF1_SCORE
var RESF2Score = $feature.PR_RESF2ENG_SCORE + $feature.PR_RESF2NAT_SCORE
var RESF3Score = $feature.PR_RESF3_SCORE

function isTwenty(i) { return i == 20 }

var num20s = Count(Filter([RESF1Score, RESF2Score, RESF3Score], isTwenty)
When(num20s==3,20,num20s==2, 10, num20s==1, 5, 0)

To summarise what this is doing:

  • Adds ENG/NAT together as one is filled in each time and the other is 0, for the purposes of this calculation I believe they can be lumped together because ENG and NAT are treated equally
  • Assigns a variable for the above plus the RESF1 and RESF3 score
  • Create a function that returns true if the input is equal to 20
  • Uses the Arcade Filter function with the list of scores from above, to get rid of any that aren't 20, and returns the count of the number of 20s
  • Uses the When function to return the 20 if there are 3 20s, 10 if there are 2, 5 if there's 1 and 0 if there are none

Hope this works for you! Seemed to be working fine in the Arcade playground for me.

Cheers,

Josh

0 Kudos
LeilaJackson1
Occasional Contributor III

Hi @JoshuaSharp-Heward  -

Sorry for the slow reply, I am just getting back to this after getting pulled away on another project. I need to get the default values updated in my layer, and then I will see if this works.

Many thanks!

Leila

0 Kudos
JoshuaSharp-Heward
Occasional Contributor III

Hi Leila,

No worries, let me know how you get on 🙂

Josh

0 Kudos
LeilaJackson1
Occasional Contributor III

@JoshuaSharp-Heward I was able to test your code today, and it too only returns 0 for some reason. I also tried a hybrid script posted below with the same result. It seems like all of these should work - I feel like I am missing something really obvious. I cleared the entire "Highest" field to Null to make sure there wasn't lingering data in there and that the scripts were actually calculating, and they are, just to 0 always. The score fields are all short integers.

Thanks!

Leila

LeilaJackson1_0-1683740711056.png

 

Hybrid script

var RESF1Score = $feature.PR_RESF1_SCORE
var RESF2Score = $feature.PR_RESF2ENG_SCORE + $feature.PR_RESF2NAT_SCORE
var RESF3Score = $feature.PR_RESF3_SCORE

//all highest
if (RESF1Score==20 && RESF2Score==20 && RESF3Score==20) { //All highest
return 20;

//2 of 3 highest

} else if (RESF1Score==20 && RESF2Score==20 && RESF3Score!=20) { //RF1 and 2 highest
return 10;

} else if (RESF1Score==20 && RESF2Score!=20 && RESF3Score==20) { //RF1 and 3 highest
return 10;

} else if (RESF1Score!=20 && RESF2Score==20 && RESF3Score==20) { //RF2 and 3 highest
return 10;

//1 of 3 highest

} else if (RESF1Score==20 && RESF2Score!=20 && RESF3Score!=20) { //only RF1 highest
return 5;

} else if (RESF1Score!=20 && RESF2Score==20 && RESF3Score!=20) { //only RF2 highest
return 5;

} else if (RESF1Score!=20 && RESF2Score!=20 && RESF3Score==20) { //only RF3 highest
return 5;

//None are highest
} else if (RESF1Score!=20 && RESF2Score!=20 && RESF3Score!=20) { //none are highest
return 0;

}



0 Kudos
JoshuaSharp-Heward
Occasional Contributor III

Hi Laila,

Is the "Highest Possible Score" an integer field as well? I realised my script was missing a close bracket, although I assume you fixed that to get it to run! To clarify are you setting this up in the Forms tab in a web map? I've managed to replicate it and get it to run perfectly using this code:

var RESF1Score = $feature.RESF1
var RESF2Score = $feature.RESF2a + $feature.RESF2b
var RESF3Score = $feature.RESF3

function isTwenty(i) { return i == 20 }

var num20s = Count(Filter([RESF1Score, RESF2Score, RESF3Score], isTwenty))
When(num20s==3,20,num20s==2, 10, num20s==1, 5, 0)

JoshuaSharpHeward_0-1683756855087.pngJoshuaSharpHeward_1-1683756888396.png

So I feel confident that the code I've written works and is a bit easier to understand! 

It might be worth putting a very simple calculation like 

return 50

to see whether the calculation is even running at all? Could you post a screenshot of the questions in the form builder as well? 

 

0 Kudos
LeilaJackson1
Occasional Contributor III

@JoshuaSharp-Heward  Thanks for your assistance on this - I really appreciate it. Knowing that you could get it to run, I started tracing back my steps and finally found the root cause. The Score fields are all short integers, but are calculated based upon select one questions. In those calculation scripts I had my numbers in single quotes which was causing the problem.

if ($feature.PR_RESF1=='1. RSS asset(s) BRE meet ALR'){

  return '0';

 

I removed the single quotes, and the script is now working as expected :-). Being new to Arcade, I have to say I do not know when to use double quotes, single quotes or no quotes. It is often trial by error, but in this case the calculations for the score fields were running as expected and returning the correct values, so I did not know the single quotes were problematic.

Thanks again!

0 Kudos
JoshuaSharp-Heward
Occasional Contributor III

No worries 🙂 I'm glad I could help out and that you managed to solve it in the end!

0 Kudos