Hi all! I am trying to create a custom pop up in Map Viewer with the Arcade content element that will return related records from another layer (there is no relationship class, I am just using a filter statement). My challenge is that the relationship is 1:M and I would like to have the pop up display data from more than one record as a field list (i.e. return type "fields", as opposed to text or a pop up string).
Is it possible to return a field list type for more than one record in an arcade element? Here's the basic code I am using that just gets the first related record, how can I modify it to return all related records? Thanks in advance for your ideas!
//Get count sublayer
var CountTable = FeatureSetByPortalItem(Portal("[url]"),"[itemID]", 0, ['*'],false)
// Filter related features by using a common attribute
Expects($feature, '*');
var siteID = $feature.SiteID
var filterStatement = 'SiteID = @SiteID' // Related features as a variable
var relatedData = Filter(CountTable, filterStatement)
//Sort relatedData by date - I'd like to not just use the first record here if possible
var relatedDataSorted = First(OrderBy(relatedData,'ObsDate'))
//Get a list of the fields in the counts table
var countFields = Schema(relatedDataSorted).fields;
//Console("list of fields "+countFields)
//define custom functions
//function to list the name of a field
function getNames(field){
return field.name;
}
//function to collect field metadata info
function getFieldsMetaData() {
var field_meta = {};
for (var f in countFields) {
field_meta[Lower(countFields[f].name)] = {
"alias": countFields[f].alias,
"domain":HasKey(countFields[f],"domain"),
"fieldType": countFields[f].type,
"name" : countFields[f].name
}
}
return field_meta;
}
//applies some basic formatting for dates and domains
function formatField(fieldMetaData) {
if (fieldMetaData.domain == true) {
return DomainName(countFields, fieldMetaData.name)
}
if (fieldMetaData.fieldType == "esriFieldTypeDate") {
return Text(countFields[fieldMetaData.name], 'MMM DD YYYY hh:mm')
}
return countFields[fieldMetaData.name]
}
//function to filter the counts fields to only ones we want to show in the pop up, including no fields with null values.
function filterValidFields(fieldName){
// skip fields that are not needed, must be listed in lower case
var skipNames = [
"objectid",
"siteid"
];
return !includes(skipNames, lower(fieldName)) //&& !IsEmpty(relatedDataSorted[fieldName]) && !IsNan(relatedDataSorted[fieldName]);
}
//get a filtered list of fields to show in the pop up
var validFields = Filter(Map(countFields, getNames), filterValidFields);
//build the pop up table
var attributes = {};
var fieldInfos = [];
var fieldMetaInfo = getFieldsMetaData();
for (var f in validFields){
var fieldName = validFields[f];//count fields
var fieldMeta = fieldMetaInfo[Lower(fieldName)]//lowercase field name
var fieldAlias = fieldMeta.alias
Push(fieldInfos, { fieldName: fieldAlias})
attributes[fieldAlias] = relatedDataSorted[fieldName]
}
return {
type: "fields",
fieldInfos: fieldInfos,
attributes: attributes
};