ArcGIS Pro Attribute Rule to pass a value from child to parent

1203
5
Jump to solution
04-26-2022 12:24 AM
mmarkovic
New Contributor III
 

I have an ArcGIS geodatabase with a parent feature class related to a child stand alone table containing the various inspections made on each parent feature. Primary key is GlobalID and foreign key is GUID.

In ArcGIS Pro, I'm trying to edit an attribute rule to automatically populate a LAST_DATE_INSPECTION field in the parent feature class with the most recent inspection (DATE_INSPECTION field in the child table).

The code I did is only updating the parent LAST_DATE_INSPECTION field with the first inserted inspection date of the child table, not the most recent date. Looks like my filter isn't working.

MarkovicMaja_0-1650957959037.png

 


 

 

    // calculation attribute rule on child table, field DATE_INSPECTION
    // triggers: Insert, Update, Delete

    //Foreign key in child table
    var fk = $feature.GUID

    //Load all inspections of the feature
    var related_inspection = FeatureSetByName($datastore, "CHILD_TABLE_NAME",['GUID'],false)

    //Filter the most recent inspection date of the feature
    var last_inspection = First(Filter(related_inspection, 'GUID = @fk'))

    //Return nothing if no date found
    if(IsEmpty(last_inspection)){
    return ;
    }

    //Updating DATE_INSPECTION field in parent feature class
    return {
    "result": fk,
         "edit": [{
            "className" : "PARENT_FC_NAME",
               "updates": [{
                 //selecting the parent feature to update
                 "GlobalID" : last_inspection.GUID,
                 //Update the LAST_DATE_INSPECTION field in parent feature class 
                 "attributes" : {'LAST_DATE_INSPECTION': last_inspection.DATE_INSPECTION
    }
                           }]
                  }]    
    };

 

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

You're missing a step.

You return the first entry of the filtered feature set, which is the first inspection for that asset.

What you want to do:

  • load the feature set and filter it
  • sort the related features by date, descending
  • return the first feature

 

//Foreign key in child table
var fk = $feature.GUID

//Load all inspections of the feature
var inspections = FeatureSetByName($datastore, "CHILD_TABLE_NAME",['GUID'],false)
var related_inspections = Filter(inspections, 'GUID = @fk')

// (if you have a relationship class, you can also do it like this)
var related_inspections = FeatureSetByRelationshipName($feature, "RelationshipName", ["GUID"], false)

// (also, you have a trigger on delete. if you delete an inspection, it will
// still be in the table when the rule is executed, so you should filter it out:)
if($editcontext.editType == "DELETE") {
    var pk = $feature.PrimaryKeyOfChildTable
    related_inspections = Filter(related_inspections, "PrimaryKeyOfChildTable <> @pk")
}

//Filter the most recent inspection date of the feature
var last_inspection = First(OrderBy(related_inspections, "DATE_INSPECTION DESC"))

 


Have a great day!
Johannes

View solution in original post

5 Replies
JohannesLindner
MVP Frequent Contributor

You're missing a step.

You return the first entry of the filtered feature set, which is the first inspection for that asset.

What you want to do:

  • load the feature set and filter it
  • sort the related features by date, descending
  • return the first feature

 

//Foreign key in child table
var fk = $feature.GUID

//Load all inspections of the feature
var inspections = FeatureSetByName($datastore, "CHILD_TABLE_NAME",['GUID'],false)
var related_inspections = Filter(inspections, 'GUID = @fk')

// (if you have a relationship class, you can also do it like this)
var related_inspections = FeatureSetByRelationshipName($feature, "RelationshipName", ["GUID"], false)

// (also, you have a trigger on delete. if you delete an inspection, it will
// still be in the table when the rule is executed, so you should filter it out:)
if($editcontext.editType == "DELETE") {
    var pk = $feature.PrimaryKeyOfChildTable
    related_inspections = Filter(related_inspections, "PrimaryKeyOfChildTable <> @pk")
}

//Filter the most recent inspection date of the feature
var last_inspection = First(OrderBy(related_inspections, "DATE_INSPECTION DESC"))

 


Have a great day!
Johannes
PDNPAGIS
New Contributor

Hello,

I'm looking to replicate this but for a simple Yes/No attribute field called 'Action_reqd'. All steps seem to be working and the code validates fine, however when updating a child record, I'm getting the "Update table row failed. Invalid column value [Invalid column value] [Action_reqd]"

PDNPAGIS_0-1684485504899.png

I'm not sure how to resolve - any pointers gratefully recevived 🙂

 

 

0 Kudos
JohannesLindner
MVP Frequent Contributor

Please post the rule your code.

JohannesLindner_0-1677736512957.pngJohannesLindner_1-1677736529803.png


Have a great day!
Johannes
0 Kudos
PDNPAGIS
New Contributor

Hello, thanks for the reply - my code is as per below

Parent table = Asset_Car_Park

Child table = Asset_Site_External_Inspection

I'm trying to update the parent table Action_reqd field with the Action_reqd record from the latest inspection survey in the related child table

 

//Foreign key in child table
var fk = $feature.parentglobalid
if (IsEmpty(fk))
    return fk;

fk = Upper(fk);

//Load all inspections of the feature
var inspections = FeatureSetByName($datastore, "Spatial.PDNPA_GEO.Asset_Site_External_Inspection",['parentglobalid', 'Action_reqd'],false)
var related_inspections = Filter(inspections, 'parentglobalid = ')

// (also, you have a trigger on delete. if you delete an inspection, it will
// still be in the table when the rule is executed, so you should filter it out:)

if($editcontext.editType == "DELETE") {
    var pk = $feature.GlobalID
    related_inspections = Filter(related_inspections, "GlobalID <> ")
}

//Filter the most recent inspection date of the feature
var last_inspection = First(OrderBy(related_inspections, "Inspection_date"))

    //Updating Action_reqd field in parent feature class
    return {
    'result': fk,
         'edit': [{
            'className' : "Spatial.PDNPA_GEO.Asset_Car_Park",
               'updates': [{
                 //selecting the parent feature to update
                 'GlobalID' : last_inspection.parentglobalid,
                 //Update the Action_reqd field in parent feature class 
                 'attributes' : {"Action_reqd": last_inspection.Action_reqd
    }
                           }]
                  }]
};

 

 

0 Kudos
mmarkovic
New Contributor III

Wonderful ! It works perfectly! Thank you for the "delete trigger" script, very useful

0 Kudos