Select to view content in your preferred language

Automatically calculate sum from related table for Field Maps

196
1
11-06-2024 09:22 AM
Labels (1)
Caroliende_Vries
Occasional Contributor

I have a point feature layer with a related table (one to many).
In this related table I have fields for Name, Amount and GUID.
What I would like is to calculate the Sum of the amounts entered and have that added to the attribute table of my feature layer.
I made a Sum field in my feature layer and with calculate in AGOL was able to do this manually using this arcade expression:

var relatedRecords = FeatureSetByRelationshipName($feature, "RelTable");

var totalAmount = 0;

for (var record in relatedRecords) {
if (!IsEmpty(record.Amount)) {
totalAmount += record.Amount;
}
}

return totalAmount;

------------------------------
However I would like this to be done automatically each time a new entry is added or removed from the related table (this will be done mostly in Field Maps).
I tried to make a python script to achieve this but so far haven't been able to get it to update the feature layer. 
Can anyone help me out? I'm very new to writing code.

0 Kudos
1 Reply
aaron_tob
Emerging Contributor

I have a similar use case, and agree that manually running a Python script to keep a field updated can be tedious. If you’re using ArcGIS Enterprise, you can try setting up an Attribute Rule with a calculation expression in Pro. With attribute rules you could write an Arcade script similar to the one you shared. But this is only available in Enterprise and not AGOL.

If you’re not using Enterprise, a Python script is probably the way to go. You can schedule the script to run periodically (by using a task scheduler) to calculate and update the sum in the feature layer automatically. It might be best to do this using ArcGIS API for Python to target the hosted feature layer directly, and windows task schedule to trigger the script to run at the desired interval. Would you be able to share the Python you're using?

Here's a post you might find helpful:
https://community.esri.com/t5/arcgis-online-blog/easy-how-to-symbology-using-related-records/ba-p/89...

 

0 Kudos