Select to view content in your preferred language

Arcade script" IF/Else trouble

1106
4
Jump to solution
11-03-2025 03:35 PM
Labels (1)
Yeaton
by
Regular Contributor

I'm having trouble with an if/else statement for an expression that evaluates the intersection of two polygons.

I get the first return, or the correct value(s) for the "if" portion, but nothing for the else portion.

Thanks for any help.

Best,

Aaron Y.

// Define a variable to hold the FeatureSet of the intersecting layer.
// Replace "Polygon Layer Name" with the actual name of your polygon layer in the map.
var intersectLayer = FeatureSetByName($map, "Future Land Use Designations");

// Find the features in 'intersectLayer' that intersect the current feature ($feature).
var intersectingFeatures = Intersects(intersectLayer, $feature);

// Initialize an empty array to store the attributes you want to display.
var attributesToDisplay = [];

// Iterate through the intersecting features and extract desired attributes.
// Replace "AttributeField1" and "AttributeField2" with the actual field names from your polygon layer.
for (var f in intersectingFeatures) {
    Push(attributesToDisplay, f['FLUM_Cat']);
}
//Remove square brackets from array and and "/" instead of comma
var myarray = attributesToDisplay;
var arrayasstring = Text(attributesToDisplay);
var nobrakets= Replace(Replace(Replace(arrayasstring, '[', ''), ']', ''),',','/'); 

var NoBracketsDist = Distinct(nobrakets)


// Check if any intersecting features were found.
if (Count(NoBracketsDist) > 0) {
    return "This parcel is within the following Land Use Designation Categories:" + " "+ Concatenate(NoBracketsDist);}

else if (Count(NoBracketsDist) == 0) {
    return "This parcel is outside of the Land Use Designation Categories";
}

layers

0 Kudos
1 Solution

Accepted Solutions
CodyPatterson
MVP Regular Contributor

Hey @Yeaton 

I think that your function here is turning nobrakets into a string, which Distinct needs a list or array, so it's turning your string into a list, so "example" would be e,x,a,m,p,l,e so it's always going to show up wrong.

In this case, I would something like this:

// Define a variable to hold the FeatureSet of the intersecting layer.
// Replace "Polygon Layer Name" with the actual name of your polygon layer in the map.
var intersectLayer = FeatureSetByName($map, "Future Land Use Designations");

// Find the features in 'intersectLayer' that intersect the current feature ($feature).
var intersectingFeatures = Intersects(intersectLayer, $feature);

// Initialize an empty array to store the attributes you want to display.
var attributesToDisplay = [];

// Iterate through the intersecting features and extract desired attributes.
// Replace "AttributeField1" and "AttributeField2" with the actual field names from your polygon layer.
for (var f in intersectingFeatures) {
    Push(attributesToDisplay, f['FLUM_Cat']);
}
//Remove square brackets from array and and "/" instead of comma
var myarray = attributesToDisplay;

var NoBracketsDist = Distinct(myarray)

var joined = Concatenate(NoBracketsDist, "/");


// Check if any intersecting features were found.
if (Count(joined) > 0) {
    return "This parcel is within the following Land Use Designation Categories:" + " "+ Concatenate(joined);}

else if (Count(joined) == 0) {
    return "This parcel is outside of the Land Use Designation Categories";
}

The Concatenate function can replace the commas with / if you'd like by default, no need to replace, here's the docs for that: https://developers.arcgis.com/arcade/function-reference/text_functions/

Let me know if that helps!

Cody

View solution in original post

4 Replies
CodyPatterson
MVP Regular Contributor

Hey @Yeaton 

I think that your function here is turning nobrakets into a string, which Distinct needs a list or array, so it's turning your string into a list, so "example" would be e,x,a,m,p,l,e so it's always going to show up wrong.

In this case, I would something like this:

// Define a variable to hold the FeatureSet of the intersecting layer.
// Replace "Polygon Layer Name" with the actual name of your polygon layer in the map.
var intersectLayer = FeatureSetByName($map, "Future Land Use Designations");

// Find the features in 'intersectLayer' that intersect the current feature ($feature).
var intersectingFeatures = Intersects(intersectLayer, $feature);

// Initialize an empty array to store the attributes you want to display.
var attributesToDisplay = [];

// Iterate through the intersecting features and extract desired attributes.
// Replace "AttributeField1" and "AttributeField2" with the actual field names from your polygon layer.
for (var f in intersectingFeatures) {
    Push(attributesToDisplay, f['FLUM_Cat']);
}
//Remove square brackets from array and and "/" instead of comma
var myarray = attributesToDisplay;

var NoBracketsDist = Distinct(myarray)

var joined = Concatenate(NoBracketsDist, "/");


// Check if any intersecting features were found.
if (Count(joined) > 0) {
    return "This parcel is within the following Land Use Designation Categories:" + " "+ Concatenate(joined);}

else if (Count(joined) == 0) {
    return "This parcel is outside of the Land Use Designation Categories";
}

The Concatenate function can replace the commas with / if you'd like by default, no need to replace, here's the docs for that: https://developers.arcgis.com/arcade/function-reference/text_functions/

Let me know if that helps!

Cody

Yeaton
by
Regular Contributor

Hi Cody,

Your solution works great! Thanks for the help!

Best,

Aaron

Corbinb
New Contributor

If you’re running into IF/ELSE trouble in an Arcade script, it usually comes down to syntax issues, incorrect field references, or a logic flow that doesn’t evaluate the way you expect. In Arcade, each condition must be clearly defined, strings must be in quotes Read more here, and fields must be wrapped in $feature.fieldname. Also remember that Arcade stops evaluating once it finds a true condition, so ordering your IF/ELSEIF statements correctly is essential.

halicoptis
New Contributor

Hey Aaron,

Great question – I've run into this exact behaviour before. The issue is likely that your else condition (Count(NoBracketsDist) == 0) never evaluates to true because NoBracketsDist is an empty string ('') rather than an empty array. So Count() on a string that's empty still returns something (often 0 in some contexts), but the logic flow might be skipping it because of how Distinct() works on empty data.

Here's a cleaner approach:

javascript
var intersecting = Intersects(FeatureSetByName($map, "Future Land Use Designations"), $feature);
var cats = [];

for (var f in intersecting) {
    Push(cats, f['FLUM_Cat']);
}

if (Count(cats) > 0) {
    var cleaned = Concatenate(Distinct(cats));
    // Replacing commas with slashes
    var formatted = Replace(Replace(cleaned, ',', '/'), ' ', ' ');
    return "This parcel is within the following Land Use Designation Categories: " + formatted;
} else {
    return "This parcel is outside of the Land Use Designation Categories";
}

Notice:

  • Check intersecting count before manipulating strings.

  • Use Concatenate() directly on distinct values.

  • Avoid converting to string earlier – it creates empty string issues.

This reminds me of Xeno Executor troubleshooting – sometimes the simplest condition (like whether a script actually injected) gets buried under formatting and extra steps. Strip it back, check the base result first, and handle the empty case cleanly. Hope this helps!


 
0 Kudos