Arcade expression to insert a record into a related table?

286
2
09-21-2023 07:26 AM
RPGIS
by
Occasional Contributor III

Hi,

I have been trying to figure out how to create an attribute rule to add a record in a related table based on certain criteria. I am not sure, since I am still learning quite a bit about arcade, how to go about it. I typically program in python, but I am trying to reduce the scripting that would be needed outside of a database when a simple attribute rule could accomplish a similar task.

 

// Function for defining whether or not a fieldname exists in another feature
function ValidateFieldNames( InputFieldsA, InputFieldsB ){
    var AFields = [ ]
    var BFields = [ ]
    for ( var i = 'name' in InputFieldsA ){
        return Push( AFields, InputFieldsA[ i ] )
    }
    for ( var i = 'name' in InputFieldsB ){
        return Push( BFields, InputFieldsB[ i ] )
    }
    for ( var Fieldname in BFields ){
        var FieldnameIndex = IndexOf( AFields, InputFieldsB[ Fieldname ] )
        if ( Includes( AFields, InputFieldsB[ Fieldname ] ) == False ){
            return Erase( AFields, FieldnameIndex )
        }
    }
}

// Current date
var CurrentYear = Year( Date( Now() ) )
// Inspection year and date
var InspectedYear = $feature.FieldYear
var InspectedDate = $feature.FieldDate
// Inspection record table
var InspectionRecords = FeatureSetByName( $datastore, "Featureclass", ['*'] )
Console( InspectionRecords )
// Get array of recorded inspection year and get only unique years in descending order
var RecordedDates = FeatureSetByName( $datastore, "Table", ['InspectionDate'] )
var RecordedYears = Max( Year( RecordedDates ) )

// Get the matching fieldnames that exist in both features
var InspectionRecordSchema = Schema( InspectionRecords ).fields
var MatchingFieldnames = ValidateFieldNames( InspectionRecords, $feature )

// Loop through the update items to create a concatenated text value of items
// to create a single text line of dictionary values
var UpdateItems = NULL
for ( var field in MatchingFieldnames ){
    var ConcatText = Concatenate( field, ' : ', $feature[ field ] )
    iif ( UpdateItems == NUll, UpdateItems == ConcatText, Concatenate( UpdateItems, ',', ConcatText ) )
    }
var UpdateItems = Dictionary( UpdateItems )

// If the current inspection year in the inspected hydrant layer
// is not in the inspection records table. Then add the record to
// the inspection table
if ( CurrentYear == InspectedYear && InspectedYear >= RecordedYears ){
    return {
         'result' : InspectedDate,
         'edit': [
           {
               'classname' : "Featureclass",
               'adds' : [
                   {
                   'objectID': $feature.ObjectID,
                   'attributes' :  UpdateItems
                   }
                ]
            }
        ]
    }
}

 

Any help with this would be greatly appreciated?

2 Replies
RPGIS
by
Occasional Contributor III

Never mind. I finally figured out a solution which works really well.

0 Kudos
weithy
by
New Contributor II

Here's an updated version of the ValidateFieldNames function:

function ValidateFieldNames(InputFieldsA, InputFieldsB) {
var AFields = []
var BFields = []
for (var i in InputFieldsA) {
Append(AFields, InputFieldsA[i])
}
for (var i in InputFieldsB) {
Append(BFields, InputFieldsB[i])
}
for (var Fieldname in BFields) {
if (!Includes(AFields, InputFieldsB[Fieldname])) {
var FieldnameIndex = IndexOf(AFields, InputFieldsB[Fieldname])
Remove(AFields, FieldnameIndex)
}
}
}

the rest of the code snippet you provided seems to be using the ValidateFieldNames function and performing additional operations related to date calculations, querying feature sets, and constructing an edit payload. However, it's difficult to provide further guidance without more context or a specific question or requirement you have.

0 Kudos