Select to view content in your preferred language

Attribute Rule to Populate Child Field Based on Parent Field Contingency

144
5
Jump to solution
a week ago
ChristopherBowering
Frequent Contributor

I have a point feature class (parent) and a related table (child) in an Enterprise Geodatabase (v 11.4).  They have a user-defined relationship class.  Upon child record creation, I would like a field in the child table to populate based on 1 of 2 parent fields using conditionality.  If Parent Field 1 contains a value, child field will populate from Parent Field 1.  If Parent Field 1 is empty, the child field will populate from Parent Field 2.  

The attribute rule below is set to the child field to-be-populated.  It passes validation and is able to be saved as an attribute rule.  However, upon attempting to add a record to the child table, an error says "invalid column value [parent field 1 field name]".

If anyone can tweak this or provide a better code to accomplish my goal, I'd appreciate it!

// Define the relationship name between the child and parent feature classes/tables
var Relationship = "RELATIONSHIP CLASS NAME"; // Replace with your actual relationship name

// Get the related parent feature(s)
var RelatedParentFeatures = FeatureSetByRelationshipName($feature, Relationship);

// Get the first related parent feature (assuming a one-to-one or one-to-many relationship where you want the first related parent)
var ParentFeature = First(RelatedParentFeatures);

// Check if a parent feature exists and if the specific parent field is empty
if (IsEmpty(ParentFeature) || IsEmpty(ParentFeature.PARENT FIELD 1)) {
    // If the parent feature or the parent field is empty, populate the child field with a default value or null
    return "PARENT FIELD 2"
} else {
    // If the parent field has a value, populate the child field with the parent's field value
    return ParentFeature.PARENT FIELD 1;
}

 

@JohannesLindner 

0 Kudos
1 Solution

Accepted Solutions
MikeMillerGIS
Esri Frequent Contributor

I do not think FeatureSetByRelationshipName exist

 

// Must use a string literal, do not use a variable
var relatedFS = FeatureSetByRelationshipClass($feature, "RELATIONSHIP CLASS NAME", ["field1","field2"], false);

var ParentFeature = First(relatedFS);
if (ParentFeature == null){
  return
}
if (ParentFeature.field1 == null){
  return ParentFeature.field2; 
}
return ParentFeature.field1

View solution in original post

0 Kudos
5 Replies
MikeMillerGIS
Esri Frequent Contributor

Need to pass in the list of fields to FeatureSetByRelationship

https://developers.arcgis.com/arcade/function-reference/featureset_functions/

 

also PARENT FIELD 1 cannot be a valid field name

0 Kudos
ChristopherBowering
Frequent Contributor

Hi Mike.  Those "Parent..." field names are not actual field names.  Nor is "RELATIONSHIP CLASS NAME" the name of my actual relationship class.  They are just placeholders that correspond to my written description to make it easier for a reader to reference.  

Doesn't the FeatureSetByRelationshipName supersede the FeatureSetByRelationship function?  I didn't think a list of fields were necessary if the relationship and specific fields involved were individually called out.  Could you indicate how that function would be used in my example?  Thanks!

0 Kudos
MikeMillerGIS
Esri Frequent Contributor

I do not think FeatureSetByRelationshipName exist

 

// Must use a string literal, do not use a variable
var relatedFS = FeatureSetByRelationshipClass($feature, "RELATIONSHIP CLASS NAME", ["field1","field2"], false);

var ParentFeature = First(relatedFS);
if (ParentFeature == null){
  return
}
if (ParentFeature.field1 == null){
  return ParentFeature.field2; 
}
return ParentFeature.field1
0 Kudos
ChristopherBowering
Frequent Contributor

FeatureSetByRelationshipName (https://developers.arcgis.com/arcade/function-reference/featureset_functions/#featuresetbyrelationsh... is a function I use often when transferring attributes between related datasets.  I've just never tried it with an IF statement so I know I'm missing something.

Maybe the ByRelationshipClass function will be better in this situation!  I will give it a go.

0 Kudos
ChristopherBowering
Frequent Contributor

That worked!  One of the parent fields contained empty - not null - values so once I edited the attribution to "<null", that code functioned correctly.

Conversely, on the parent feature class, I have a rule set up which would update the child field if the parent field attribute changed.  This works fine when it's one field in the parent to one field in the child.  The code is below.

However, now I'd like it to say...if Parent Field 1 changes, update Child Field or if Parent Field 2 changes, update (same) Child Field.  This would allow the child field to update based on whichever of the two parent fields gets update (could ostensibly go back and forth).  I am not sure how I would set this up.

if ($originalfeature.PARENT FIELD 2 == $feature.PARENT FIELD 2) {
    return
}

var fs = FeatureSetByRelationshipName($feature, "RELATIONSHIP CLASS NAME");

var updates = []
for(var f in fs) {
    if(f.GlobalID == $feature.GlobalID) { continue }
    if(f.CHILD FIELD == $feature.PARENT FIELD 2) continue;
    var u  = {
        globalID: f.GlobalID,
        attributes: {
            "CHILD FIELD": $feature.PARENT FIELD 2
            }
        }
    Push(updates, u)
}

return {
    edit: [{
        className: "RELATED DATASET NAME",
        updates: updates
        }]
}
0 Kudos