Select to view content in your preferred language

Can someone explain what I'm doing wrong with this Arcade expression?

265
7
Jump to solution
Wednesday
Labels (1)
BillSchlatter
Regular Contributor

I'm using ArcGIS Online, trying to configure a customized pop-up in Arcade. I modified an example I found elsewhere on here in order to get output that looks like the standard Fields table, but with the fields in a different order than they are in the source layer, and with some of them being omitted should they contain no data or a code indicating that there is no data. Testing and debugging as I went was fine, including tests that successfully displayed the first several rows as intended, and the script runs fine in the editor - no errors, expected output format. But when I try clicking on a feature on the map, I wind up with an empty pop-up. Weirdly, if I add an off-the-shelf Fields List underneath it, then the expected custom fields table will render, but only in the sample pop-up displayed in the Map Builder - clicking another feature will make it go right back to pretending the script doesn't exist. 

Please note that I'm aware this is something of a hackjob, I'm just trying to meet a deadline, I can pretty it up later. Also, none of this data is sensitive, it's open data. 

Here's my code: 

var allAttributes = Dictionary($feature)["attributes"];
var attributes = {}
var fieldInfos = [];
var attributeValues = {};

var includeFields = ["County", "Primary_Applicant", "Other_Communities", "Obligation_Year", "Project_Status", "Project_Description", "Magazine_Project_Type", "SEMCOG_TAP_Award", "MDOT_TAP_Award"]

for (var key in includeFields) {
    insert(attributes, includeFields[key], allAttributes[includeFields[key]])
}

var layer_schema = Schema($feature);
var fieldAliases = {};


for (var i = 0; i < Count(layer_schema.fields); i++) {
    var field = layer_schema.fields[i];
    fieldAliases[field.name] = field.alias;
}
var value = null

key = "County"
value = attributes[key]
if (!IsEmpty(value) && value != "") {
    var displayName = key; // Default to field name
    // Use alias if available
    if (HasKey(fieldAliases, key)) {
        displayName = fieldAliases[key];
    }
    Push(fieldInfos, { fieldName: key, label: displayName });
    attributeValues[key] = value;
}

key = "Primary_Applicant"
value = attributes[key]
if (!IsEmpty(value) && value != "") {
    var displayName = key; // Default to field name
    // Use alias if available
    if (HasKey(fieldAliases, key)) {
        displayName = fieldAliases[key];
    }
    Push(fieldInfos, { fieldName: key, label: displayName });
    attributeValues[key] = value;
}


key = "Other_Communities"
value = attributes[key]
if (!IsEmpty(value) && value != "" && value != "NA") {
    var displayName = key; // Default to field name
    // Use alias if available
    if (HasKey(fieldAliases, key)) {
        displayName = fieldAliases[key];
    }
    Push(fieldInfos, { fieldName: key, label: displayName });
    attributeValues[key] = value;
}

key = "Obligation_Year"
value = attributes[key]
if (!IsEmpty(value) && value != "") {
    var displayName = key; // Default to field name
    // Use alias if available
    if (HasKey(fieldAliases, key)) {
        displayName = fieldAliases[key];
    }
    Push(fieldInfos, { fieldName: key, label: displayName });
    attributeValues[key] = value;
}

key = "Project_Status"
value = attributes[key]
if (!IsEmpty(value) && value != "") {
    var displayName = key; // Default to field name
    // Use alias if available
    if (HasKey(fieldAliases, key)) {
        displayName = fieldAliases[key];
    }

    var decodedValue = Decode(value, 1, "Obligated", 2, "Complete", "Status Unknown")
    Push(fieldInfos, { fieldName: key, label: displayName });
    attributeValues[key] = decodedValue;
}

key = "Project_Description"
value = attributes[key]
if (!IsEmpty(value) && value != "") {
    var displayName = key; // Default to field name
    // Use alias if available
    if (HasKey(fieldAliases, key)) {
        displayName = fieldAliases[key];
    }
    Push(fieldInfos, { fieldName: key, label: displayName });
    attributeValues[key] = value;
}

key = "Magazine_Project_Type"
value = attributes[key]
if (!IsEmpty(value) && value != "") {
    var displayName = key; // Default to field name
    // Use alias if available
    if (HasKey(fieldAliases, key)) {
        displayName = fieldAliases[key];
    }
    var decodedValue = Decode(value, 1, "Sidewalks and Pedestrian and Bicycle Safety Projects", 2, "Trails and Regional Active Transportation Corridor projects", 3, "Streetscapes, placemaking, and downtown walkability projects", "Type Unknown")
    Push(fieldInfos, { fieldName: key, label: "Project Type" });
    attributeValues[key] = decodedValue;
}

key = "SEMCOG_TAP_Award"
value = attributes[key]
if (!IsEmpty(value) && value != "" && value != 0) {
    var displayName = key; // Default to field name
    // Use alias if available
    if (HasKey(fieldAliases, key)) {
        displayName = fieldAliases[key];
    }
    Push(fieldInfos, { fieldName: key, label: displayName });
    value = Concatenate(['$', value], '', "#,###")
    attributeValues[key] = value;
}

key = "MDOT_TAP_Award"
value = attributes[key]
if (!IsEmpty(value) && value != "" && value != 0) {
    var displayName = key; // Default to field name
    // Use alias if available
    if (HasKey(fieldAliases, key)) {
        displayName = fieldAliases[key];
    }
    Push(fieldInfos, { fieldName: key, label: displayName });
    value = Concatenate(['$', value], '', "#,###")
    attributeValues[key] = value;
}

return {
    type: 'fields',
    fieldInfos: fieldInfos,
    attributes: attributeValues
};

 

0 Kudos
1 Solution

Accepted Solutions
timcneil
Esri Contributor

@BillSchlatter ,

I have a suspicion I know what's going on. Try adding the Expects() function to your script, like so: 

timcneil_1-1782327517427.png

 

I opened your map and added it and it seemed to work as expected after I made this change. I just added the fields you had referenced in the includeFields variable since I assumed those were the only ones you wanted returned in the pop-up. If you need additional fields, then just add these into the Expects() function as well. 

Taylor

View solution in original post

7 Replies
Katie_Clark
MVP Regular Contributor

You mention the data is public - are you able to share your web map or at least the data to help with troubleshooting? 

And also, just getting the silly but simple question out of the way...do you have a Text section added to your pop-up? Even if you have the attribute expression configured and it's returning data successfully in the Arcade editor, you still need to apply it to the pop-up.

Screenshot 2026-06-24 135918.jpg

Best,
Katie

If this post helped you, please consider giving a kudos and/or marking as the accepted solution. Thanks!
0 Kudos
BillSchlatter
Regular Contributor

To be clear, this isn't an attribute expression - it's an Arcade block in the pop-up, so it shouldn't need a Text block to be called from.  It's returning roughly half a dozen fields in a format that, as far as I can tell, is supposed to use the same formatting as a standard Field List block. At least, that's how it worked until it mysteriously stopped working. 

Screenshot 2026-06-24 141308.png

Here's the map: https://semcog.maps.arcgis.com/apps/mapviewer/index.html?webmap=9b2302237a894c54b436983968c24d28

And here's the layer in question: https://semcog.maps.arcgis.com/home/item.html?id=31512f80c1764a64b62163e7bcf5caca 

0 Kudos
Katie_Clark
MVP Regular Contributor

Perfect, thanks so much for sharing. A couple things I noticed:

When clicking on features, things seemed to be displaying as "intended". You have both the Fields list (showing 9 out of 29 fields) as well as the arcade expression, so two different tables showed in the pop-ups that look identical in formatting. If you only want the formatted table to show, then delete the field list.

Also, there was a large amount of whitespace at the top of the pop-up, which I realized was related to the Media (image) that you have inserted. I took a look at the output from your "Public Picture URL" attribute expression, and noticed that the output was a URL with spaces in it (the file names for the photos have spaces in them).

I modified the script to handle URL encoding, and then the photos displayed in the pop-ups.

if (IsEmpty($feature.Picture_URL) || $feature.Picture_URL == "") {
    return null
}
var url = Replace($feature.Picture_URL, "\\", "/")
url = Replace(url, "T:/StoryMaps/", "https://maps.semcog.org/")
return Replace(url, " ", "%20")


Hope that helps! 

Best,
Katie

If this post helped you, please consider giving a kudos and/or marking as the accepted solution. Thanks!
BillSchlatter
Regular Contributor

The image URL fix is great, thanks! I haven't even had time to look into why so many images weren't loading properly. 

As for the other part? That's still not working properly for me. Sometimes when I have the full feature list block underneath the Arcade block, it makes the Arcade block work properly, but the moment I remove it or start hiding fields in the feature list block, the Arcade output either shows only a few rows or nothing at all, even with nothing changing in the Arcade script whatsoever. 

0 Kudos
timcneil
Esri Contributor

@BillSchlatter ,

I have a suspicion I know what's going on. Try adding the Expects() function to your script, like so: 

timcneil_1-1782327517427.png

 

I opened your map and added it and it seemed to work as expected after I made this change. I just added the fields you had referenced in the includeFields variable since I assumed those were the only ones you wanted returned in the pop-up. If you need additional fields, then just add these into the Expects() function as well. 

Taylor

BillSchlatter
Regular Contributor

Thank you! I wound up having to use '*' instead of an explicit list of fields for some reason, but that finally seems to have things working. It also explains some of the weird behavior I was seeing before - as I tried configuring other pop-up elements for the layer, the Arcade output would sometimes show some, but not all, of the expected output. It must have been loading the needed data for those other elements, making them available to the Arcade expression. Pop-ups seem like a pretty bizarre place to require something like an Expects statement, but I guess that's what it takes. 

0 Kudos
Katie_Clark
MVP Regular Contributor

Quick note on the image fix, I glanced over the attribute table, looks like some contain ampersands as well so you'll want to encode those too (and check for any other unsafe characters). And if you're still seeing white space after that when you would expect an image, inspect the URL for that feature and make sure it's not just returning a 404.

Seems like the solution from @timcneil will probably fix your original issue 🙂

Best,
Katie

If this post helped you, please consider giving a kudos and/or marking as the accepted solution. Thanks!