Select to view content in your preferred language

Returning 2nd, 3rd....attribute from related table with Arcade

1024
2
Jump to solution
09-21-2021 01:57 PM
Labels (1)
by Anonymous User
Not applicable

After scouring the forums and anything else (since I am just learning this), I have gotten to this place with the below code.  I need to return the attribute called "filepath" of the 2nd related record, 3rd related record, etc.  I have to do a separate expression for each one, so I can display it is as a URL and Photo in the pop up.  I got the first record to work in another expression, but I'm clearly not doing something right here, since this doesn't return anything when I try to get the 2nd or 3rd value.   
--------------------------------------------------------------------------------------------------

var index = 0;
var related_table = OrderBy(FeatureSetByRelationshipName($feature,"%vw_EventAttachments"), "AttachmentID");

var filter_query = "EGUID = '" + $feature["EGUID"] + "'";
var related_data_filtered = Filter(related_table, filter_query);
var related_data_filtered_count = Count(related_data_filtered);
var output = "";

for (var row in related_data_filtered) {

if (related_data_filtered_count > 0)

{
index++;

if (index == 2)

return (related_data_filtered).Filepath;
}

else { output = " ";}

return output;

}

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

You are on the right track, your for loop is just a little wonky.

What you're doing is this:

  • increase index
  • check if it is 2
    • yes: return Filepath (and you try to return it from the feature set as opposed to returning it from row)
    • no: set output to " " and return that

So the code gets to the first row, sees that index = 1 != 2 and returns " ".

 

Copy this function and change the return_index for each instance.

 

// the number of the row from which you want to return the AttachmentID
// 1 for first, 2 for second, ...
var return_index = 2;

var related_table = OrderBy(FeatureSetByRelationshipName($feature,"%vw_EventAttachments"), "AttachmentID");

var filter_query = "EGUID = '" + $feature["EGUID"] + "'";
var related_data_filtered = Filter(related_table, filter_query);
var related_data_filtered_count = Count(related_data_filtered);

// return early if you want to get an attachment that doesn't exist
if(return_index > related_data_filtered_count) {
  return "";
}

var index = 1;
for (var row in related_data_filtered) {
  if(index == return_index) {
    return row.Filepath;
  }
  index++;
}

 

 


Have a great day!
Johannes

View solution in original post

2 Replies
JohannesLindner
MVP Frequent Contributor

You are on the right track, your for loop is just a little wonky.

What you're doing is this:

  • increase index
  • check if it is 2
    • yes: return Filepath (and you try to return it from the feature set as opposed to returning it from row)
    • no: set output to " " and return that

So the code gets to the first row, sees that index = 1 != 2 and returns " ".

 

Copy this function and change the return_index for each instance.

 

// the number of the row from which you want to return the AttachmentID
// 1 for first, 2 for second, ...
var return_index = 2;

var related_table = OrderBy(FeatureSetByRelationshipName($feature,"%vw_EventAttachments"), "AttachmentID");

var filter_query = "EGUID = '" + $feature["EGUID"] + "'";
var related_data_filtered = Filter(related_table, filter_query);
var related_data_filtered_count = Count(related_data_filtered);

// return early if you want to get an attachment that doesn't exist
if(return_index > related_data_filtered_count) {
  return "";
}

var index = 1;
for (var row in related_data_filtered) {
  if(index == return_index) {
    return row.Filepath;
  }
  index++;
}

 

 


Have a great day!
Johannes
by Anonymous User
Not applicable

Thank you soooo much! It works great.  

0 Kudos