Arcade Popup Filter by Feature Variable

964
4
Jump to solution
02-16-2023 09:17 AM
MeleKoneya
Occasional Contributor III
I am trying to to filter a table in the new Map Viewer and show the filtered records in a popup.   I have a fire incident map feature with associated units in the table.
 
This code only works if I put in the incident_number explicitly.
 
 
var incident = $feature.incident_number
incident = 23069675
var u = FeatureSetByName($map, "PFD CAD Calls - Units", ['incident_number', 'unit_id'])
var codeStatement = 'incident_number = @incident';
Console(incident)
var us = Filter(u, codeStatement)
Console(Count(us))
var popupString = 'Units:';
for(var x in us)
{
   popupString += Text(x.unit_id)
   
}
return {
   popupString
}
Tags (1)
0 Kudos
1 Solution

Accepted Solutions
MeleKoneya
Occasional Contributor III

I was able to get this working.   Not sure why I was having issues before.

var incident = $feature.incident_number
Console(incident)
if(incident == null) { return "No Incident number!" }
var u = FeatureSetByName($map, "Unit Status", ['incident_number', 'unit_id'])
var codeStatement = 'incident_number = @incident'
Console(codeStatement)
var us = Filter(u, codeStatement)
OrderBy(us, 'unit_id ASC')
Console(Count(us))
  var Units = ""
for(var x in us)
{
 
    Units += Text(x.unit_id) + TextFormatting.NewLine
   
}
return {
   Units
}

 

View solution in original post

0 Kudos
4 Replies
JohannesLindner
MVP Frequent Contributor

To post code:

JohannesLindner_0-1674589814049.pngJohannesLindner_1-1674589827880.png

 

Try doing a null check:

var incident = $feature.incident_number
if(incident == null) { return "No Incident number!" }

var u = ...

Have a great day!
Johannes
MeleKoneya
Occasional Contributor III

I was able to get this working.   Not sure why I was having issues before.

var incident = $feature.incident_number
Console(incident)
if(incident == null) { return "No Incident number!" }
var u = FeatureSetByName($map, "Unit Status", ['incident_number', 'unit_id'])
var codeStatement = 'incident_number = @incident'
Console(codeStatement)
var us = Filter(u, codeStatement)
OrderBy(us, 'unit_id ASC')
Console(Count(us))
  var Units = ""
for(var x in us)
{
 
    Units += Text(x.unit_id) + TextFormatting.NewLine
   
}
return {
   Units
}

 

0 Kudos
JohannesLindner
MVP Frequent Contributor

You had issues before because you didn't have the null check.

When IncidentNumber is null, codeStatement will be "incident_number = ". That is an invalid sql statement, so the Filter() function will fail.

When the Arcade expression is validated, it uses default values (I think, not too sure about that. @jcarlson ?), so the expression didn't get validated because it raised an error, until you set incident to a non-null value.

With the null check I suggested, you return a default response if the IncidentNumber is null. When you return from a script, the rest of the script isn't executed anymore, so Filter() doesn't get a chance to raise an error, so the expression validates.


Have a great day!
Johannes
0 Kudos
jcarlson
MVP Esteemed Contributor

I think it depends on where you test the expression. In Pro, I often get the green check mark for a valid expression, but there's actually invalid data in the table that throws an error somewhere further down. But I can test the same expression in a web context and get the error identified right away, so I would guess there is something going on with default values. Hard to fully observe it, though.

- Josh Carlson
Kendall County GIS