Select to view content in your preferred language

Arcade popup expression to display related attributes but ignore 0 attribute

1550
10
Jump to solution
10-03-2022 02:09 PM
Labels (1)
JonathanWhite1
Occasional Contributor

Hello I am trying to display related data in popups using an arcade script I found and altered. The issue is that I need to not return 0  or replace 0 with '' where attribute values are equal to 0. New to Arcade and I have no idea where I would insert a Replace or an if then statement. Right now the following code will show for example 5000 State Highway 149 0 as 0 is listed if the feature doesn't have a StreetType value. I have nulls and blanks calculate automatically to be 0 .  I cant change the database values to be blank or null for a number of reasons which has to do with comparing values..

var related_table = FeatureSetById($datastore, "12");
var filter_query = "Asmt = '" + $feature["APN"] + "'";
var related_data_filtered = Filter(related_table, filter_query);
var related_data_filtered_count = Count(related_data_filtered);

var output = "";
if (related_data_filtered_count > 0) {
output = "Total of " + related_data_filtered_count + " Address(es)";
for (var related_data_row in related_data_filtered) {
output += TextFormatting.NewLine + related_data_row.StreetNum + " " + related_data_row.Street + " " + related_data_row.StreetType + TextFormatting.Newline + "____________";
}
} else {
output = "No Related Records...";

0 Kudos
2 Solutions

Accepted Solutions
jcarlson
MVP Esteemed Contributor

Well, you're nearly there. What you'd need is something to check the value of each field, then only append it to the output string if it's not a "0". There are lots of ways you could do it, but let's look at using an array and Erase / Concatenate.

By the way, are these layers truly related with a relationship class? You could bypass the filtering step entirely if you used FeatureSetByRelationshipName.

Instead of cobbling the string together all in one go, we can first create an array. Then we can loop through the array and drop any items that are equal to "0". Once our array is "cleaned", we can use concatenate to build that string.

var related_table = FeatureSetById($datastore, "12");
var filter_query = "Asmt = '" + $feature["APN"] + "'";
var related_data_filtered = Filter(related_table, filter_query);
var related_data_filtered_count = Count(related_data_filtered);

var output = "";

if (related_data_filtered_count > 0) {
    output = `Total of ${related_data_filtered_count} Address(es)\n`;

    for (var r in related_data_filtered) {
        
        // Array of address parts
        var addr_parts = [
            r.StreetNum,
            r.Street,
            r.StreetType
        ]
        
        // Clean '0' items from array
        for (var part in addr_parts){
            if (addr_parts[part] == '0'){
                Erase(addr_parts, part)
            }
        }
        
        // Create output string
    output += `${Concatenate(addr_parts, ' ')}\n____________`;
    
    }
} else {
output = "No Related Records...";
}

 

- Josh Carlson
Kendall County GIS

View solution in original post

jcarlson
MVP Esteemed Contributor

Oh, gosh. When I copied my code in, I forgot the last line:

return output

Add that to the expression I posted, it should return something.

And take a look at FeatureSetByRelationshipName. When you use that to access the related data, it is pre-filtered to those related items. You'll need to know the relationship name, or use the expression builder to pipe it in.

jcarlson_0-1664842553282.png

 

jcarlson_1-1664842577055.png

 

- Josh Carlson
Kendall County GIS

View solution in original post

0 Kudos
10 Replies
KenBuja
MVP Esteemed Contributor

If the last value is the only one that would be empty, then you can use the IsEmpty function to do something like this

 

output += TextFormatting.NewLine + related_data_row.StreetNum + " " + related_data_row.Street;
if (!IsEmpty(related_data_row.StreetType)){
  output += " " + related_data_row.StreetType; 
}
output += TextFormatting.NewLine + "____________";

 

 

RhettZufelt
MVP Notable Contributor

Decode might work for you:

output += TextFormatting.NewLine + related_data_row.StreetNum + " " + related_data_row.Street + " " + Decode(related_data_row.StreetType, "0", "", related_data_row.StreetType) + TextFormatting.Newline + "____________"

Assuming StreetType is a text field, if the value is "0", will replace it with "" (blank), if not, uses the value for related_data_row.StreetType.

R_

 

JonathanWhite1
Occasional Contributor

I wish your code would work for me because that is exactly what I am trying to do. It is saying  Identifier Not Found. related_data_row

0 Kudos
jcarlson
MVP Esteemed Contributor

Well, you're nearly there. What you'd need is something to check the value of each field, then only append it to the output string if it's not a "0". There are lots of ways you could do it, but let's look at using an array and Erase / Concatenate.

By the way, are these layers truly related with a relationship class? You could bypass the filtering step entirely if you used FeatureSetByRelationshipName.

Instead of cobbling the string together all in one go, we can first create an array. Then we can loop through the array and drop any items that are equal to "0". Once our array is "cleaned", we can use concatenate to build that string.

var related_table = FeatureSetById($datastore, "12");
var filter_query = "Asmt = '" + $feature["APN"] + "'";
var related_data_filtered = Filter(related_table, filter_query);
var related_data_filtered_count = Count(related_data_filtered);

var output = "";

if (related_data_filtered_count > 0) {
    output = `Total of ${related_data_filtered_count} Address(es)\n`;

    for (var r in related_data_filtered) {
        
        // Array of address parts
        var addr_parts = [
            r.StreetNum,
            r.Street,
            r.StreetType
        ]
        
        // Clean '0' items from array
        for (var part in addr_parts){
            if (addr_parts[part] == '0'){
                Erase(addr_parts, part)
            }
        }
        
        // Create output string
    output += `${Concatenate(addr_parts, ' ')}\n____________`;
    
    }
} else {
output = "No Related Records...";
}

 

- Josh Carlson
Kendall County GIS
JonathanWhite1
Occasional Contributor

Yes it is a relationship class a one to many AGOL hosted feature service.

0 Kudos
jcarlson
MVP Esteemed Contributor

Oh, gosh. When I copied my code in, I forgot the last line:

return output

Add that to the expression I posted, it should return something.

And take a look at FeatureSetByRelationshipName. When you use that to access the related data, it is pre-filtered to those related items. You'll need to know the relationship name, or use the expression builder to pipe it in.

jcarlson_0-1664842553282.png

 

jcarlson_1-1664842577055.png

 

- Josh Carlson
Kendall County GIS
0 Kudos
JonathanWhite1
Occasional Contributor

That seems to do the trick! The only thing I cant figure out is why when the street name is *UNASSIGNED* it just returns the underscores I use to separate the records in the popup.

 

var related_table = FeatureSetByRelationshipName($feature, "Address_Situs");
var related_table_count = Count(related_table);

var output = "";

if (related_table_count > 0) {
output = `Total of ${related_table_count} Address(es)\n`;

for (var r in related_table) {

// Array of address parts
var addr_parts = [
r.StreetNum,
r.Street,
r.StreetType
]

// Clean '0' items from array
for (var part in addr_parts){
if (addr_parts[part] == '0'){
Erase(addr_parts, part)
}
}

// Create output string
output += `${Concatenate(addr_parts, ' ')}\n____________` + TextFormatting.NewLine;

}
} else {
output = "No Related Records...";
}
return output;

 

0 Kudos
jcarlson
MVP Esteemed Contributor

Those underscores show up when there is at least 1 related record, regardless of its contents. Do you mean that the street name is literally "UNASSIGNED", or it's null? What's the data look like for rows where this happens?

- Josh Carlson
Kendall County GIS
0 Kudos
JonathanWhite1
Occasional Contributor

Literally *UNASSIGNED*. It must have something to do with the * because when the street name is just UNASSIGNED the popup displays UNASSIGNED. This is just old data that came out of an AS400 system. The new data in the new system doesn't have the *

0 Kudos