Using Arcade to overwrite a value from main feature attributes with a value from a related table

286
1
12-07-2023 09:40 AM
Labels (1)
CSchwartz614
New Contributor

I'm very new to Arcade and I'm struggling with this expression in a Field Maps smart form. None of these feature layers are hosted. I want to be able to pass information back and forth between our water meter feature class and their related maintenance tables. In this expression I'm trying to read the New meter ID from the most recent maintenance table record, and use that value to overwrite the existing (old) meter ID in the feature class attributes. 

//get related inspections

var maintrecords = FeatureSetByRelationshipName($feature, "W_SensusMeter_Maint_2023");

 

//order by date descending

var lastRec = First(OrderBy(maintrecords,"InspectionDate DES"))

 

var cnt = Count(maintrecords);

 

//the new meter id from maintenance records

var mxNewID = lastRec['MX_New_Meter_ID']

 

//the old id from the previous meter

var oldID = $feature.meter_id

 

//replace old id in main feature with new id value from latest maintenance rec

var IDSwap = Replace($feature.meter_id,oldID, mxNewID)

if (cnt >1) {

  return IDSwap

}

 

This expression works in the console, but when it doesn't work in Field Maps or Map Viewer. I used a similar expression to pass information from the main feature into the maintenance table and it works correctly. Can someone tell me what I'm doing wrong?

0 Kudos
1 Reply
gargarcia
New Contributor III

I have not done this with an arcade expression via forms, but I have done it with a hosted notebook I scheduled to run every so often. I used it to fill in a date field from the date of the last inspection. It wasn't critical we have that filled in in real time, so a scheduled notebook worked good enough.

# Initialize the FeatureLayer objects
hydrant_layer = FeatureLayer(hydrant_layer_url)
flushing_table = FeatureLayer(flushing_table_url)

# Create a dictionary to store the most recent flush date for each hydrant
latest_flush_dates = {}

# Query the flushing table and find the most recent flush date for each hydrant
flushing_records = flushing_table.query()
for record in flushing_records.features:
    HydID = record.attributes['HydID']
    flush_date = record.attributes['FlushDate']

    if HydID not in latest_flush_dates or flush_date > latest_flush_dates[HydID]:
        latest_flush_dates[HydID] = flush_date

# Query the hydrant layer and update the LastFlushDate field
hydrant_records = hydrant_layer.query()
for record in hydrant_records.features:
    FACILITYID = record.attributes['FACILITYID']
    if FACILITYID in latest_flush_dates:
        # Update only if the new date is more recent
        if record.attributes['LastFlushDate'] is None or latest_flush_dates[FACILITYID] > record.attributes['LastFlushDate']:
            record.attributes['LastFlushDate'] = latest_flush_dates[FACILITYID]
            # Update the feature layer
            hydrant_layer.edit_features(updates=[record])

 

0 Kudos