I am working on a field maps form for a group of field inspectors who inspect 3 types of facilities. Each facility is stored in our map as seperate layers though they have (mostly) the same fields with some slight changes between the three for different info needs. The important thing is that they all have the same "FAC_ID" field. I am trying to have our form autopopulate the "Facility Name" line to avoid human error. The field to pull from is "FAC_NAME" for two of the related records and the other uses "LEASE_NAME". I created an expression to pull all three, create a new dictionary and turn that back into a feature service. See below:
var facilityfs = FeatureSetByRelationshipName($feature, "TX_ALL_FACILITIES", ['FAC_NAME', 'FAC_ID'], false)
var wellfs = FeatureSetByRelationshipName($feature, "TX_GMH_OILGAS_WELLS", ['LEASE_NAME','FAC_ID'], false)
var pipelinfs = FeatureSetByRelationshipName($feature, "TX_PIPELINE_POINTS_ALL", ['FAC_NAME', 'FAC_ID'], false)
// Create empty feature array and feat object for output
var features = [];
var feat;
// Iterate facilityfs
for (var f in facilityfs) {
// Create feature with aggregated values
feat = {
'attributes': {
'FACILITYID': f['FAC_ID'],
'FACILITYNAME': f['FAC_NAME']
}
};
// Push feature into array
Push(features, feat);
};
// Iterate wellfs
for (var w in wellfs) {
// Create feature with aggregated values
feat = {
'attributes': {
'FACILITYID': w['FAC_ID'],
'FACILITYNAME': w['LEASE_NAME']
}
};
// Push feature into array
Push(features, feat);
};
// Iterate pipelinefs
for (var p in pipelinfs) {
// Create feature with aggregated values
feat = {
'attributes': {
'FACILITYID': p['FAC_ID'],
'FACILITYNAME': p['FAC_NAME']
}
};
// Push feature into array
Push(features, feat);
};
// Create dict for output FeatureSet
var out_dict = {
'fields': [
{'name': 'FACILITYID', 'type': 'esriFieldTypeString'},
{'name': 'FACILITYNAME', 'type': 'esriFieldTypeString'},
],
geometryType: '',
'features': features
};
// Convert dictionary to feature set.
var allfs = FeatureSet(Text(out_dict))
return allfs
The code works but it only gives me one result (which seems odd but I'm new)

And so I tried to move on to the next step (I think) which would be to pull from matching FAC_IDs (now FACILITYID in the combined fs) to return the FACILITYNAME in the combined fs. So I take out the return allfs and replace it with this:
if (!IsEmpty(allfs)) {
return allfs['FACILITYNAME']
} else {
return null
}
And I get this error:
"Test execution error: Execution error - Cannot access value using a key of this type. Verify test data."
I have tried looking around for similar situations but I have not found a solution. Does anyone know where I messed up? Any help would be greatly appreciated!