Combine Arcade statements for pop-up

986
2
Jump to solution
02-18-2022 08:46 AM
erica_poisson
Occasional Contributor III

Hi - 

I am trying to customize some pop-ups to show basic information from related tables. My hosted feature layer is set up so that there is a point layer with a series of 1:M relationships to related tables. Relationships are all based on GlobalID/GUID. There is a public version of the hosted feature layer here.

I have two separate Arcade statements working properly, however it was not until I started configuring the pop-up that I realized these really should be combined into one expression so that the information returned in the pop-up makes sense. 

I am unsure of how to combine these. I want to to return either of the statements below based on the "Purpose" from the Road Infrastructure point layer. 

Return this expression if Pupose = Stream Crossing-B:

 

 

// Access 'Bridge Inspections' table as a FeatureSet
var portal = Portal("https://www.arcgis.com")
var bridges = FeatureSetByPortalItem(portal,
    "ae306d66da414b7f83f1c7db4284a632", 2, ['Inspection_Date',
    'Condition_Overall', 'Inspector_Initials'])

// Filter related features by using a common attribute
var GLOBALID = $feature.GlobalID
var filterStatement = 'Bridge_GlobalID = @GlobalID'

// Related features as a variable
var relatedData = Filter(bridges, filterStatement)

// Sort related features by newest to oldest
var relatedDataSorted = OrderBy(relatedData, 'Inspection_Date DESC')

// Build the pop-up string by iterating through all related features
var popupString1 = '';

if (Count(relatedDataSorted)>0) {
    // Get the first feature in the sorted FeatureSet
    var f = First(relatedDataSorted);
    
    // Build the pop-up string
    popupString1 = DefaultValue(f.Condition_Overall, 'No Condition Recorded') + " " + Text(f.Inspection_Date, 'MM/DD/YYYY') + " by " + DefaultValue(f.Inspector_Initials, 'No Inspector Recorded')

}       

DefaultValue(popupString1, 'No bridge inspections on record')

 

 

 

Return this expression if Purpose = anything but Stream Crossing-B:

 

 

// Access 'Culvert Inspections' table as a FeatureSet
var portal = Portal("https://www.arcgis.com")
var culverts = FeatureSetByPortalItem(portal,
    "ae306d66da414b7f83f1c7db4284a632", 1, ['Inspection_Date',
    'Condition_Overall', 'Inspector_Initials'])

// Filter related features by using a common attribute
var GLOBALID = $feature.GlobalID
var filterStatement = 'Culvert_GlobalID = @GlobalID'

// Related features as a variable
var relatedData = Filter(culverts, filterStatement)

// Sort related features by newest to oldest
var relatedDataSorted = OrderBy(relatedData, 'Inspection_Date DESC')

// Build the pop-up string by iterating through all related features
var popupString1 = '';

if (Count(relatedDataSorted)>0) {
    // Get the first feature in the sorted FeatureSet
    var f = First(relatedDataSorted);
    
    // Build the pop-up string
    popupString1 = DefaultValue(f.Condition_Overall, 'No Condition Recorded') + " " + Text(f.Inspection_Date, 'MM/DD/YYYY') + " by " + DefaultValue(f.Inspector_Initials, 'No Inspector Recorded')

}       

DefaultValue(popupString1, 'No culvert inspections on record')

 

 

 

This is what the pop-up looks like now with the two separate Arcade expressions - you can see how it doesn't make any sense. I'd like to only show the proper expression based on Purpose:

erica_tefft_0-1645203125622.png

 

 

I know I cannot use Arcade to call another expression, and I am guessing that I need to use some sort of if statement to get this working, but I really do not even know where to start. 

Any help would be very welcome!

Erica
0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

Easiest way:

if($feature.Purpose == "Stream Crossing-B") {
  // bridge code except last line
  return DefaultValue(popupString1, "No bridge inspection on record")
}  else {
  // culvert code except last line
  return DefaultValue(popupString1, "No culvert inspection on record")
}

 

Better way: The code for bridges and culverts is the same, only the actual data is different. So you have to abstract the code a little:

// Access the inspection table as a FeatureSet
var portal = Portal("https://www.arcgis.com")
var portal_item = "ae306d66da414b7f83f1c7db4284a632"
var layer = IIF($feature.Purpose == "Stream Crossing-B", 2, 1)
var fields = ['Inspection_Date', 'Condition_Overall', 'Inspector_Initials']
var inspection_table = FeatureSetByPortalItem(portal, portal_item, layer, fields)

// Build the filter statement
var GlobalID = $feature.GlobalID
var filterStatement = IIF($feature.Purpose == "Stream Crossing-B", "Bridge", "Culvert") + "_GlobalID = @GlobalID"

// Related features as a variable
var relatedData = Filter(inspection_table, filterStatement)

// Sort related features by newest to oldest
var relatedDataSorted = OrderBy(relatedData, 'Inspection_Date DESC')

// Build the popup string
if(Count(relatedDataSorted) == 0) {
  return 'No culvert inspections on record'
}
var f = First(relatedDataSorted);
return DefaultValue(f.Condition_Overall, 'No Condition Recorded') + " " + Text(f.Inspection_Date, 'MM/DD/YYYY') + " by " + DefaultValue(f.Inspector_Initials, 'No Inspector Recorded')

Have a great day!
Johannes

View solution in original post

0 Kudos
2 Replies
JohannesLindner
MVP Frequent Contributor

Easiest way:

if($feature.Purpose == "Stream Crossing-B") {
  // bridge code except last line
  return DefaultValue(popupString1, "No bridge inspection on record")
}  else {
  // culvert code except last line
  return DefaultValue(popupString1, "No culvert inspection on record")
}

 

Better way: The code for bridges and culverts is the same, only the actual data is different. So you have to abstract the code a little:

// Access the inspection table as a FeatureSet
var portal = Portal("https://www.arcgis.com")
var portal_item = "ae306d66da414b7f83f1c7db4284a632"
var layer = IIF($feature.Purpose == "Stream Crossing-B", 2, 1)
var fields = ['Inspection_Date', 'Condition_Overall', 'Inspector_Initials']
var inspection_table = FeatureSetByPortalItem(portal, portal_item, layer, fields)

// Build the filter statement
var GlobalID = $feature.GlobalID
var filterStatement = IIF($feature.Purpose == "Stream Crossing-B", "Bridge", "Culvert") + "_GlobalID = @GlobalID"

// Related features as a variable
var relatedData = Filter(inspection_table, filterStatement)

// Sort related features by newest to oldest
var relatedDataSorted = OrderBy(relatedData, 'Inspection_Date DESC')

// Build the popup string
if(Count(relatedDataSorted) == 0) {
  return 'No culvert inspections on record'
}
var f = First(relatedDataSorted);
return DefaultValue(f.Condition_Overall, 'No Condition Recorded') + " " + Text(f.Inspection_Date, 'MM/DD/YYYY') + " by " + DefaultValue(f.Inspector_Initials, 'No Inspector Recorded')

Have a great day!
Johannes
0 Kudos
erica_poisson
Occasional Contributor III

Hi @JohannesLindner -

Thank you for the second expression - it works perfectly and is so short/elegant. I really appreciate your help with this.

Erica
0 Kudos