Arcade - FeatureSetByRelationshipName - Calculate Field

262
8
Jump to solution
09-24-2021 09:17 AM
Labels (1)
JoshHarris
Occasional Contributor

I was wondering if it was possible to use FeatureSetByRelationshipName to calculate fields in a related feature class?

What I want to do is carry the values from a field in featureclass1 to an empty field in featureclass2. I can successfully achieve this in a popup in AGOL, but I want to do be able to achieve this in Field Calculator, and then ultimately Python to automate the process.

At the moment, I am using the below expression for the popup, but it doesn't seem to work in Field Calculator:

 

var relatedrecords = FeatureSetByRelationshipName($feature,"[relationship_name]")

for (var f in relatedrecords){
return f.[field_name]
}

TIA

0 Kudos
1 Solution

Accepted Solutions
DougBrowning
MVP Frequent Contributor

Yea you need to filter it for the feature you want.  Like other poster said use First since 1 result will be faster.  Also do not return geo (the false part).

var sql = "PointID = '" + $feature.PointID + "'";
var tbl = Filter(FeatureSetByName($map,"Points", ['FieldNameHere'], false), sql);
return First(tbl).FieldNameHere

View solution in original post

0 Kudos
8 Replies
jcarlson
MVP Regular Contributor

The FeatureSetBy... functions should all work in the Field Calculate profile of Arcade.

Your expression as written is only going to return a value from the first record in the relatedrecords FeatureSet, though; is that your intention?

If so, you can try using First(relatedrecords) instead of a loop.

Also, are you certain that every feature has related records?

- Josh Carlson
Kendall County GIS
0 Kudos
JoshHarris
Occasional Contributor

Hi Josh,

Thanks for the reply. It's a one to many relationship so there will always only be one record to return anyway, and I'm trying to pull the value from the origin to the destination.

As mentioned, this method works perfectly in an AGOL popup, but can't seem to apply the same method in Field Calculator. It's worth mentioning the Field Calculator successfully carries out the calculation, but no values are returned.

0 Kudos
DougBrowning
MVP Frequent Contributor

Whenever I am outside AGOL web map I have had better luck using the By Portal Item one.

https://developers.arcgis.com/arcade/function-reference/data_functions/#featuresetbyportalitem

Hope that works.

0 Kudos
JoshHarris
Occasional Contributor

Thanks for the response Doug. Is this instead of using FeatureSetByRelationshipName? Like mentioned, I'm grabbing a value from the origin and populating the foreign

0 Kudos
DougBrowning
MVP Frequent Contributor

Yes you use it instead of.  It gives a more specific location which works in ArcPro and such. Other than that it works the same.

0 Kudos
JoshHarris
Occasional Contributor

Thanks Doug, not sure if I'm missing something, have tried....

var relatedrecords = FeatureSetByPortalItem(Portal([portal]),[item_id],0,[field_name])

for (var f in relatedrecords){
return f.[field_name]
}

 You're correct, it's pulling through a value, but it's not pulling through the correct related value. It looks just grabbing the first value from the feature set (OID 1)

0 Kudos
DougBrowning
MVP Frequent Contributor

Yea you need to filter it for the feature you want.  Like other poster said use First since 1 result will be faster.  Also do not return geo (the false part).

var sql = "PointID = '" + $feature.PointID + "'";
var tbl = Filter(FeatureSetByName($map,"Points", ['FieldNameHere'], false), sql);
return First(tbl).FieldNameHere

View solution in original post

0 Kudos
JoshHarris
Occasional Contributor

Thanks Doug, have undertaken the following and is working successfully!

var sql = "GlobalID = '" + $feature.RELGlobalID + "'";
var layer = FeatureSetByPortalItem(Portal('https://www.arcgis.com'), [item_id], 0,[field_name],false)
var tbl = Filter(layer, sql)

return First(tbl).[field_name]