Arcade for loop returns multiple of same results

548
11
12-07-2023 07:19 AM
ZachBodenner
MVP Regular Contributor

Hello,

I have an Arcade expression that has worked perfectly in other applications but is returning some odd results after I repurposed it in another map. Here's the expression:

var parcelPID = $feature.PID;
var relatedForms = FeatureSetByName($map, "UBTEST - Epgdb.engineering.UB test2")

var filterStatement = "g2_UB_PID = @parcelPID"

var ubRecords = Filter(relatedForms, filterStatement)
var ubRecordsSorted = OrderBy(ubRecords, 'RowNumber DESC')

var popupString = ''

for (var f in ubRecordsSorted){
    popupString += 
        `${f.Rownumber}<br>
        <b>AccountNum: </b>${f.g2_AccountNumber}<br>
        <b>PID: </b>${f.g2_UB_PID}<br>
        <b>Meter #: </b>${f.g5_MeterNumber}<br>
				<b>Meter Type: </b>${f.m3_MeterType}<br>
        <b>Install Date: </b>${f.g5_DateInstalled}<br>
        <b>Serial: </b>${f.m3_SerialNumber}<br>
        <br>
        `
}

return { 
	type : 'text', 
	text : popupString
}

What it should do is search the table UB_Test_2 for all the rows that have the same g2_UB_PID and return meter information. Here's an example of the test 2 table. The identified parcel is the parent feature, and the two records shown in the table both have the same PID:

ZachBodenner_0-1701962215270.png

 

However, running the expression above, only one of those two records is returned, and it is retuned twice:

ZachBodenner_1-1701962268810.png

 

This persists across all features, where the number of possible related records is returned, but only information from one of the records populates all the returns. As I mentioned, essentially this same expression (different datasets and just different fields used in the filter and return, with the same expression structure) works just fine. 

0 Kudos
11 Replies
jcarlson
MVP Esteemed Contributor

Does it do the same thing if you use FeatureSetByRelationshipName? Or is there not a real relationship in place, just a shared field?

Instead of constantly appending to a single string, try creating an array and using Concatenate:

var popup_arr = []

for (var f in ubRecordsSorted) {
  Push(
    popup_arr,
    `your string goes here`
  )
}

return {
  type: 'text',
  text: Concatenate(popup_arr, '<br>')
}

Any change?

- Josh Carlson
Kendall County GIS
0 Kudos
ZachBodenner
MVP Regular Contributor

Not a real relationship, just a shared field. Curiously your expression did the same thing, but with a different row, even though I left the Sort variable alone: 

var parcelPID = $feature.PID;
var relatedForms = FeatureSetByName($map, "UBTEST - Epgdb.engineering.UB test2")

var filterStatement = "g2_UB_PID = @parcelPID"

var ubRecords = Filter(relatedForms, filterStatement)
var ubRecordsSorted = OrderBy(ubRecords, 'RowNumber DESC')

//var popupString = ''

/*for (var f in ubRecordsSorted){
    popupString += 
        `${f.Rownumber}<br>
        <b>AccountNum: </b>${f.g2_AccountNumber}<br>
        <b>PID: </b>${f.g2_UB_PID}<br>
        <b>Meter #: </b>${f.g5_MeterNumber}<br>
				<b>Meter Type: </b>${f.m3_MeterType}<br>
        <b>Install Date: </b>${f.g5_DateInstalled}<br>
        <b>Serial: </b>${f.m3_SerialNumber}<br>
        <br>
        `
}*/
var popup_arr = []

for (var f in ubRecordsSorted) {
  Push(
    popup_arr,
    `${f.Rownumber}<br>
        <b>AccountNum: </b>${f.g2_AccountNumber}<br>
        <b>PID: </b>${f.g2_UB_PID}<br>
        <b>Meter #: </b>${f.g5_MeterNumber}<br>
				<b>Meter Type: </b>${f.m3_MeterType}<br>
        <b>Install Date: </b>${f.g5_DateInstalled}<br>
        <b>Serial: </b>${f.m3_SerialNumber}<br>
        <br>`
  )
}
return {
  type: 'text',
  text: Concatenate(popup_arr, '<br>')
}



 

ZachBodenner_0-1701970744849.png

 

0 Kudos
KenBuja
MVP Esteemed Contributor

What do you see if you put in "return ubRecordsSorted" at line 8 and run this in the editor? Do you get the expected records?

0 Kudos
ZachBodenner
MVP Regular Contributor

No, nothing in the result at all

ZachBodenner_0-1701976331486.png

 

0 Kudos
jcarlson
MVP Esteemed Contributor

Throw in some Console commands.

var parcelPID = $feature.PID;
Console(`Getting related forms for Parcel PID ${parcelPID}`)

var relatedForms = FeatureSetByName($map, "UBTEST - Epgdb.engineering.UB test2")
var filterStatement = "g2_UB_PID = @parcelPID"

var ubRecords = Filter(relatedForms, filterStatement)
Console(`Identified ${Count(ubRecords)} related forms`)

var ubRecordsSorted = OrderBy(ubRecords, 'RowNumber DESC')

for (var f in ubRecordsSorted) {
  Console(`Getting attributes for Row Number ${f.Rownumber}`)
}

What do you see in the Console tab?

- Josh Carlson
Kendall County GIS
0 Kudos
ZachBodenner
MVP Regular Contributor

I did have a couple tries at the console and using your expression there, got a somewhat unhelpful response of :

Getting related forms for Parcel PID 3411622340001

Identified 0 related forms

That does make sense for that particular PID, but is a little unhelpful to test whether or not the code is working. There isn't a way to specify the record that the Arcade element uses to test is there?

0 Kudos
jcarlson
MVP Esteemed Contributor

If you're working with popups, it tends to pick something in your map extent.

- Josh Carlson
Kendall County GIS
0 Kudos
KenBuja
MVP Esteemed Contributor

Can you run it in the code editor? This will show the returned FeatureSet and the console items that Josh also suggested

editor.png

You might have to put in a specific parcelPID to test it

var parcelPID = "01824000034"

 

0 Kudos
ZachBodenner
MVP Regular Contributor

Ah yeah good idea on strong-arming the variable. But it does appear that it just wants to return identical rows:

var parcelPID = '1811622110028';
Console(`Getting related forms for Parcel PID ${parcelPID}`)

var relatedForms = FeatureSetByName($map, "UBTEST - Epgdb.engineering.UB test2")
var filterStatement = "g2_UB_PID = @parcelPID"

var ubRecords = Filter(relatedForms, filterStatement)
Console(`Identified ${Count(ubRecords)} related forms`)

var ubRecordsSorted = OrderBy(ubRecords, 'RowNumber DESC')

for (var f in ubRecordsSorted) {
  Console(`Getting attributes for Row Number ${f.Rownumber}`)
}

Returns:

Getting related forms for Parcel PID 1811622110028

Identified 3 related forms

Getting attributes for Row Number 2150

Getting attributes for Row Number 2150

Getting attributes for Row Number 2150

0 Kudos