Hi,
I feel what I am attempting to do is pretty straightforward, but I cannot find an example of it.
I have a related inspections table that I am looking to join to the parent table using a GUID/GLOBALID. From there I want to get a string value from a field (ID) from the parent table record and paste it into any related records in the inspections table.
The code I have accomplishes this (partly) but only for one related record in the inspections table. If there are multiple inspections for that single feature, the remaining related records will have a null value for ID.
Can someone point me the right direction? Thank you!
Here is What I Have so Far:😗
Hi @ScottLehto4
You can achieve this using a Python dictionary. Two examples in the code commented below, one for a single attribute and one for multiple attributes.
from arcgis import GIS
from arcgis.features import FeatureLayer
## connect to agol/portal
agol = GIS("home")
## the parent table - contains unique GlobalID
valve_url = ""
valve_lyr = FeatureLayer(valve_url)
## the related table - contain many related records to parent table
inspections_url = ""
inspections_lyr = FeatureLayer(inspections_url)
## a dictionary to store the glabalid as key, and attributes as the value
valve_dict = {}
################################################################################
## eg 1 - update one field
## query the valve dataset and populate the dictionary
valve_fset = valve_lyr.query()
## iterate through features and add the GlobalID as key and ASSETID as value
for f in valve_fset:
valve_dict[f.attributes["GlobalID"]] = f.attributes["ASSETID"]
## update the related table
inspections_fset = inspections_lyr.query()
## for each feature in the related table
for f in inspections_fset:
pguid = f.attributes["PARENT_GLOBALID"]
## if the PARENT_GLOBALID is a key in the dictionary
if pguid in valve_dict:
## the field you want to update in related table
f.attributes["ASSETID"] = valve_dict[pguid]
## print to identify success/failures with update
print(inspections_lyr.edit_features(updates=inspections_fset))
################################################################################
## eg 2 - update multiple fields
## query the valve dataset and populate the dictionary
valve_fset = valve_lyr.query()
## iterate through features and add the GlobalID as key and a list containing ASSETID and VALVETYPE as the value
for f in valve_fset:
valve_dict[f.attributes["GlobalID"]] = [f.attributes["ASSETID"], f.attributes["VALVETYPE"]]
## update the related table
inspections_fset = inspections_lyr.query()
## for each feature in the related table
for f in inspections_fset:
pguid = f.attributes["PARENT_GLOBALID"]
## if the PARENT_GLOBALID is a key in the dictionary
if pguid in valve_dict:
## the fields you want to update in related table
## get first value in list from dictionary entry
f.attributes["ASSETID"] = valve_dict[pguid][0]
## get second value in list from dictionary entry
f.attributes["VALVETYPE"] = valve_dict[pguid][1]
## print to identify success/failures
print(inspections_lyr.edit_features(updates=inspections_fset))