Arcade expression to list values from field in related table

2050
2
Jump to solution
05-24-2023 10:43 AM
Labels (1)
LJackson29
Occasional Contributor III

Hi -

I am still new to Arcade.  I have created the below script in AGO Map Viewer Forms that joins to a related table with a one to many relationship based upon Project ID, and pulls the Phase IDs into a field in the Projects layer with each ID separated by ", ". The script is working, but it adds a comma and space after the last PHASE ID (which makes sense based on the code), but I would prefer not to have the comma and space at the end. I have tried adding Replace and Right functions to remove them, but haven't been successful. Any recommendations of how to alter the code to remove the comma and space at the end or prevent them from being added?

Thanks!

Leila

 

var related_table = FeatureSetById($datastore, /* SNP_PHASE */ "0");

var filter_query = "PROJECTID = '" + $feature["PROJECTID"] + "'";

var related_data_filtered = Filter(related_table, filter_query);

var related_data_filtered_count = Count(related_data_filtered);

var output = "";

if (related_data_filtered_count > 0) {for (var related_data_row in related_data_filtered) {output += related_data_row.PH_PHASEID  + TextFormatting.NewLine;}}

        else {output = "No Related Records.";}

            return output;

 

0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

Instead of building an output string, use an array together with the Concatenate function.

var related_table = FeatureSetById($datastore, /* SNP_PHASE */ "0");

var filter_query = "PROJECTID = '" + $feature["PROJECTID"] + "'";

var related_data_filtered = Filter(related_table, filter_query);

var related_data_filtered_count = Count(related_data_filtered);

var output = []

if (related_data_filtered_count > 0) {
  for (var related_data_row in related_data_filtered) {
    Push(output, related_data_row.PH_PHASEID)
  }
} else {
  return "No Related Records."
}

return Concatenate(output, ', ')

This will insert the comma and space only between items in your output array, nothing at the end.

- Josh Carlson
Kendall County GIS

View solution in original post

2 Replies
jcarlson
MVP Esteemed Contributor

Instead of building an output string, use an array together with the Concatenate function.

var related_table = FeatureSetById($datastore, /* SNP_PHASE */ "0");

var filter_query = "PROJECTID = '" + $feature["PROJECTID"] + "'";

var related_data_filtered = Filter(related_table, filter_query);

var related_data_filtered_count = Count(related_data_filtered);

var output = []

if (related_data_filtered_count > 0) {
  for (var related_data_row in related_data_filtered) {
    Push(output, related_data_row.PH_PHASEID)
  }
} else {
  return "No Related Records."
}

return Concatenate(output, ', ')

This will insert the comma and space only between items in your output array, nothing at the end.

- Josh Carlson
Kendall County GIS
LJackson29
Occasional Contributor III

@jcarlson That worked perfectly - many thanks!

0 Kudos