Updating multiple fields using arcade

230
0
12-19-2023 05:02 AM
RPGIS
by
Occasional Contributor III

Hi,

 

I am not sure if this is possible or not but I am trying to update a related table from another feature based on changes in the specified date field. Originally I was specifying all of the fields that needed to be inserted. Rather than specifying all of the fields directly; I would like to get all of the attributes based on matching fields.

Here is what I have thus far.

//_______""" Functions for modifying the related table """_________
// Get matching field names
function GetMatchingFields( FieldListA, FieldListB ){
    var MatchingFields = [ ]
    for ( var i in FieldListA ){
        var fieldname = FieldListA[i]
        if ( Includes( FieldListB , fieldname ) ){
            Push( MatchingFields, fieldname )
            }
        }
    return MatchingFields
    }

// Get field names from input features
function GetFieldNames( InputFeature ){
    var FieldNames = [ ]
    var FeatureInfo = Schema(InputFeature).fields
    for ( var i in FeatureInfo ){
        Push( FieldNames , FeatureInfo[ i ].name )
        }
    return FieldNames
    }

//_______""" Specify Variables """_________
var FeatureClassName = 'Insert Featureclass Name'
var TableName = 'Insert Table Name'
var FeatureID = $feature.OBJECTID
var InspDate = $feature.InspectionDate
var IDField = 'OBJECTID'
var DateField = 'InspectionDate'
var Order = DateField + ' DESC'

// Order the features by the date field descending
var History = OrderBy( FeatureSetByName( $datastore, TableName ) , Order )
var Hydrants = $featureset

// Get matching field names found in both features
var MatchingFields = GetMatchingFields( GetFieldNames( Hydrants ), GetFieldNames( History ) )

// If the date has a count greater than 1 then populate the list with unique dates
var DateCount = Count( Filter( Hydrants, DateField + ' = @InspDate' ) )
while ( DateCount != 0 ){
    var DateChange = DateAdd( InspDate , DateCount, 'seconds')
    var DateCheck = Count( Filter( Hydrants, DateField + ' = @DateChange' ) )
    if ( DateCheck == 0 ){
        var InspDate = DateChange
        break
        }
    else { var DateCheck = DateCheck - 1 }
    }
return InspDate

// Filter feature by criteria
var FilteredFeature = First( Filter( History, DateField + ' = @InspDate' ) )

// Insert records into the related table if the date does not exist in the related table
var Values = First( Filter( FeatureSetByName($datastore, FeatureClassName , MatchingFields ), DateField + ' = @InspDate' ) )
if ( InspDate > First( History )[ DateField ] ){
    Console( 'Inserting record from @FeatureClassName into @TableName' )
    return {
         "result": InspDate,
         "edit": [ {  
             // Specify the related table
             "className" : TableName, 
             //the type of edit, in this case we want to add so we say `adds`, its an array since we can make many inserts
             "adds" : [ {
                 // Add the current feature data based on the object id of the filtered data
                 "attributes": Values
                 } ]
            } ]
        }
    }
else if ( InspDate == FilteredFeature[ DateField ] ) {
    Console( 'Updating record from @FeatureClassName into @TableName' )
    return {
         "result": InspDate,
         "edit": [ {  
             // Specify the related table
             "className" : Table, 
             //the type of edit, in this case we want to add so we say `adds`, its an array since we can make many inserts
             "updates" : [ {
                 // Specify the object id of the feature
                 IDField : FilteredFeature[ IDField ],
                 // Add the current feature data based on the object id of the filtered data
                 "attributes": Values
                 } ]
            } ]
        }
    }    

 

I am not sure if I correctly wrote this script to do just that or if there is a simpler solution that already exists. Any help would be greatly appreciated.

0 Kudos
0 Replies