Select to view content in your preferred language

Arcade - return a related record only if it passes filter

1253
10
Jump to solution
11-22-2023 06:36 AM
ZachBodenner
MVP Regular Contributor

Hello,

I have a set of data (Trees) with a related table (Maintenance Performed on Trees), both added to a web map. The related table contains records of tree removals, chemical treatments, inspections, etc.  I want to set up an Arcade expression to get related records only if they satisfy the criteria of "Maintenance_Performed" = "Removal". The easy solution would be to set a filter on the table in the web map structure, but the users need all of the records for purposes beyond just this expression. However, the table can contain multiple related records, and my expression is currently having trouble returning only removal records:

var asset_id = $feature.GlobalID
var relatedTable = FeatureSetByName($map, "Tree Inventory Form",['Maintenance_Performed','Date','TreeInv_Rel']);
var sortedTable = OrderBy(relatedTable, 'Date DESC')
//var onlyRemoved = Filter(sortedTable,'Maintenance_Performed = Removal')
var relatedRecord = First(Filter(sortedTable, "TreeInv_Rel = @asset_id"))

console ("Related Record:",relatedRecord)

var recordDate = relatedRecord.Date
var maintPerformed = relatedRecord.Maintenance_Performed
console ("Record Date: ",recordDate,TextFormatting.NewLine,"Maintenance performed: ",maintPerformed)

if(relatedRecord == null) { 
  return 
  }

else if (!isEmpty(recordDate) && maintPerformed == 'Removal'){
  return Text(recordDate,'MMM DD, YYYY')
  }

else if(isEmpty(recordDate) && maintPerformed == 'Removal'){
  return ' an unrecorded date'
  }

else {
  return
}

 

I've tried this a couple ways: the way that the expression is shown above is trying to set the return based on Maintenance_Performed as a variable. I think this doesn't work though because the filter isn't necessarily accessing the right record. For example, I have a tree with two records: record one on 12/15/2021 is Removal, and record 2 on 7/28/2022 is a stump removal. If I access the most recent record, the filter pulls the stump grind record even though I want the removal record. 

My next thought was to try another filter so that only "Maintenance_Performed = Removal" passes the filter. That's the "//var onlyRemoved = Filter(sortedTable,'Maintenance_Performed = Removal')" line, but when I uncomment that out and change the next variable to use onlyRemoved, I get the error "Unknown Error."

I'm close, can anyone get me over the hump?

0 Kudos
10 Replies
ZachBodenner
MVP Regular Contributor

That's so finnicky, especially since everywhere else Arcade is quote-type-agnostic. Thanks for the help as always Ken, you're a pro!

0 Kudos