AnsweredAssumed Answered

Query related record from feature

Question asked by lp_azr on Jul 9, 2020

Hello everyone,

I´m trying to query the related records from features of a feature layer and I´m kind of stuck. There is a sample (arcgis-appstudio-samples/Listed Related Features at v4.2 · Esri/arcgis-appstudio-samples · GitHub ) that pretty much does that, but I had to restructure it a bit and unfortunately I don´t get any related records.

 

So I have a feature layer that is hosted on ArcGIS Online. When viewing it in the web viewer the related records are shown. In my qml code I query the feature once I click on it which works fine.

The code posted below is basically the code from the sample with some tweaks. The console.log() in line 14 returns and object, so I guess that means the feature does have a related table. But the length of the relatedFeatureQueryList is zero (I specifically checked the feature with objectid 23 and it has a related record). The code with the .connect(function()) doesn´t get executed at all.

 

Edit: I also tried to modify the List Related Features as little as possible, only replacing the map content. But no luck. The feature gets highlighted and zoomed to but the attributeView is blank...

 

I know this is probably not much to work with, but any help is appreciated.

 

Thanks in advance!

 

Connections {
    target: featureLayer
    onSelectFeaturesStatusChanged: {
        if (featureLayer.selectFeaturesStatus === Enums.TaskStatusErrored) {
            var errorString = "Error: %1".arg(featureLayer.error.message);
            msgDialog.text = errorString;
            msgDialog.open();
            console.log(errorString);
        } else if (featureLayer.selectFeaturesStatus === Enums.TaskStatusCompleted) {
           
            var featureQueryResult = featureLayer.selectFeaturesResult;
            var relatedFeatureQueryResultList = featureQueryResult.iterator.features[0].featureTable.queryRelatedFeaturesResults

            console.log(featureQueryResult.iterator.features[0].featureTable.relatedTables)
            //qml: [object Object]
            console.log(featureQueryResult.iterator.features[0].attributes.attributeValue("objectid"))
            //qml: 23
            console.log(relatedFeatureQueryResultList.length)
            //qml: [object Object]

            featureQueryResult.iterator.features[0].featureTable.queryRelatedFeaturesStatusChanged.connect(function() {
                if (featureQueryResult.iterator.features[0].featureTable.queryRelatedFeaturesStatus !== Enums.TaskStatusCompleted)
                    return;

                var relatedFeatureQueryResultList = featureQueryResult.iterator.features[0].featureTable.queryRelatedFeaturesResults;

                // iterate over returned RelatedFeatureQueryResults
                for (var i = 0; i < relatedFeatureQueryResultList.length; i++) {
                    console.log(relatedFeatureQueryResultList.length)

                    // iterate over Features returned
                    var iter = relatedFeatureQueryResultList[i].iterator;
                    while (iter.hasNext) {
                        var feat = iter.next();
                        var displayFieldName = feat.featureTable.layerInfo.displayFieldName;
                        var serviceLayerName = feat.featureTable.layerInfo.serviceLayerName;
                        var displayFieldValue = feat.attributes.attributeValue(displayFieldName);

                        // add the related feature info to a list model
                        var listElement = {
                            "displayFieldName": displayFieldName,
                            "displayFieldValue": displayFieldValue,
                            "serviceLayerName": serviceLayerName
                        };
                        relatedFeaturesModel.append(listElement);
                    }
                }
            });
        }
    }
}

              

Outcomes