Select to view content in your preferred language

Get array of attributes from filtered or related table

476
1
Jump to solution
11-30-2023 11:28 AM
Labels (3)
ZachBodenner
MVP Regular Contributor

Hello, 

I have an arcade expression that I've used to establish an array of fields and aliases, and then run them through a loop and omit fields that contain an empty or null value, and using the remainder to construct an html table. I can't remember where I got this code (somewhere on this foum) but it works great:

 

var fieldsArray = ['field1','field2','field_etc'];
var labelsArray = ['alias1','alias2','alias etc'];
var arrayLength = Count( fieldsArray );
//create table header
var returnString = "<h2>Selected Populated Fields</h2><table>";
 
Expects( $feature, 'field1','field2','field_etc')
/*loops through a number of times eual to the length of the array (that's the i<arrayLength part), incrementing by one each time (i++). If the attribute if the field array is not empty 
(!IsEmpty), then the value of the the variable returnString is equal the value in the fieldsArray equal to the index value of the array (fieldsArray[i]) plus the additional portion of the
return string enclosed in the curly brackets. If it is empty, then the value of returnString   
*/
for (var i = 0; i < arrayLength; i++ ) {
  if ( !IsEmpty( $feature[ fieldsArray[i] ] ) ){
    returnString = returnString + "<tr><td>" + labelsArray[i] + "</td><td>" + $feature[ fieldsArray[i] ] + "<" + TextFormatting.ForwardSlash + "td><" + TextFormatting.ForwardSlash + "tr>";
  }}
 
returnString = returnString + "<" + TextFormatting.ForwardSlash + "table>";
 
//returns returnString
return { 
   type : 'text', 
            text : returnString 

 

 

I've been trying to extend this and I need a little help. So what I want to do is run a filter statement that will get data from a table that shares an identical field (let's just call it field1 for this example). Instead of using the feature's attributes to construct the table using the form loop, I want to use the related table's attributes to construct the table. So what I've attempted to do is to run a filter and then replace the fieldsArray with a list of fields from the related table. Here's an abbreviated version for simplicity:

 

 

var f1= $feature.field1;
var relatedForms = FeatureSetByName($map, "Related Table")

//create a filter statement 

var filterStatement = "f1= @related_field1"

//a  form equals the table run through the filter
 
var landInfo = First(Filter(relatedForms, filterStatement))
var fieldsArray = ['related_field1' ,'related_field2' , 'related_field_etc']
var labelsArray = ['related alias 1' ,'related alias 2', 'related alias etc']

var arrayLength = Count( fieldsArray );
var returnString = `<table>`;

Expects( landInfo, 'related_field1' ,'related_field2' , 'related_field_etc')

for (var i = 0; i < arrayLength; i++ ) {
  if ( !IsEmpty( landInfo[ fieldsArray[i] ] ) ){
    //returnString = returnString + "<tr><td>" + labelsArray[i] + "</td><td>" + $feature[ fieldsArray[i] ] + "</td></tr>";
    returnString = `${returnString}<tr><td style="border-bottom: 1px solid #cccccc;border-right: 1px solid #cccccc;width:50%;padding-left:2%"><b>${labelsArray[i]}</b></td><td style="border-bottom: 1px solid #cccccc;width:50%;padding-left:2%">${landInfo[ fieldsArray[i] ]}</td></tr>`;
  }}
 
returnString = returnString + "</table>";
 
//returns returnString
return { 
   type : 'text', 
   text : returnString 
}

 

 

but I'm running into the error: Cannot read properties of undefined (reading 'toString'). I'm not exactly sure what this means or if I've missed something about gathering the array from the related table.

0 Kudos
1 Solution

Accepted Solutions
ZachBodenner
MVP Regular Contributor

I see that I screwed up my filter. It should instead read:

var filterStatement = "f1= @related_field1"

Hooray for solving our own problems!

View solution in original post

0 Kudos
1 Reply
ZachBodenner
MVP Regular Contributor

I see that I screwed up my filter. It should instead read:

var filterStatement = "f1= @related_field1"

Hooray for solving our own problems!

0 Kudos