When I update site_p I want the correlating fields in site_a to be updated as well. The relationship class is realated on the field "rpsuid"
Here is my code:
// Step 1: Get the related records from site_a using the correct relationship class name
var relatedRecords = FeatureSetByRelationshipName($feature, "usar_hq.rd_81.Site_P_Site_A", [], false);
// Step 2: Get the first related record
// Explanation: We use the First() function to get the first record from the related records.
var related_record = First(relatedRecords);
// Step 3: Check if a related record is found
// Explanation: We check if related_record is null. If it is, we return null to stop further processing.
if (related_record == null) {
return null;
}
// Debugging: Print the related record
Console("Related record: " + Text(related_record));
// Step 4: Populate fields in the main feature class with fields from the related record
// Explanation: We create an object to return the desired fields from the related record.
return {
"debug_field": related_record.debug_field
// Add more fields as needed
// Uncomment and modify the lines below to include additional fields
// "accountableorganization": related_record.accountableorganization,
// "anotherfield": related_record.anotherfield
};
and I get this error:
Edit operation failed.
Message: Unable to complete operation.
Details: Internal error during object update. Undefined keyword is used in the dictionary return script. [
Rule name: Site_p2a,
Triggering event: Update,
Class name: Site_P,
GlobalID: {AAF25529-13CD-448C-BDC2-C83DE21711EF}], Undefined keyword is used in the dictionary return script. [debug_field]
debug_field does exist in the feature class and service. What am I doing wrong? I am sure it is a stupid syntax error on my part.
Solved! Go to Solution.
I finally figured this out. Something so easy I can't believe I overlooked it. I was applying the attribute rule to that parent feature class in the relationship class. This is incorrect. The attribute rule should be applied to the child feature class. The one where the edits are applied to once the parent feature class is triggered with an update. I was also able to simplify the code. I was making to complex trying to get it to work. Below is the code that ended up doing the trick. I did notice a delay in the updates to the child feature class.
// calculation attribute rule on child
// field: if you want to get only one field from the parent, then chose
// that field. if you want to get multiple fields, leave empty
// triggers: Insert(, update)
// load the related parent features using
// a relationship class between parent and child:
var parent_fs = FeatureSetByRelationshipName($feature, "db.schema.AttributeRuleName")
// return nothing if no parent feature was found
var parent = First(parent_fs)
if(parent == null) { return }
// if you want to return multiple fields:
var att = {
"accountableorganization": parent.accountableorganization,
"coordinatex": parent.coordinatex,
"coordinatey": parent.coordinatey
}
return {"result": {"attributes": att}}
var relatedRecords = FeatureSetByRelationshipName($feature, "usar_hq.rd_81.Site_P_Site_A", ["debug_field"], false);
you'll need to add "debug_field" to the list of fields you want to return in relatedRecords.
With that change I still get the same error. hmm
FeatureSetByRelationshipName doesn't use the name of the relationship class object, it uses the name of the relationship as defined by the forward or backward label in that class. You can try using that name or switching to FeatureSetByRelationshipClass if that's supported in your environment.
Also, I'm not sure you can return an object like that in rules, you need to return an object with a "result" property which in turn has at least one of "attribute" or "geometry" properties. The docs have more info on this.
yeah I updated my code but still get an error.
Invalid Expression
No features found
// Step 1: Get the related records from site_a using the correct relationship class name
var relatedRecords = FeatureSetByRelationshipName($feature, "usar_hq.rd_81.Site_P_Site_A", ["*"], false);
// Step 2: Get the first related record
// Explanation: We use the First() function to get the first record from the related records.
var related_record = First(relatedRecords);
// Step 3: Check if a related record is found
// Explanation: We check if related_record is null. If it is, we return null to stop further processing.
if (related_record == null) {
return null;
}
// Debugging: Print the related record
Console("Related record: " + Text(related_record));
// Step 4: Populate fields in the main feature class with fields from the related record
// Explanation: We create an object to return the desired fields from the related record.
return {
//result is a dictionary
"result": {
"attributes": {
"debug_field": related_record.debug_field
//"accountableorganization": related_record.accountableorganization,
}
}
}
Excellent, that's progress! If your relationship is working, it's possible you don't have any features. You should add a guard by checking if the Count of the relatedRecords is zero and returning early, but if your test data should return a related record then sort out your relationship query before you do that, otherwise you'll bury the true issue. Good luck!
it would be good news if I ran it and got that message but that is in the expression builder. I can't even save it at this point.
ESRI makes it hard to debug in this interface.
I finally figured this out. Something so easy I can't believe I overlooked it. I was applying the attribute rule to that parent feature class in the relationship class. This is incorrect. The attribute rule should be applied to the child feature class. The one where the edits are applied to once the parent feature class is triggered with an update. I was also able to simplify the code. I was making to complex trying to get it to work. Below is the code that ended up doing the trick. I did notice a delay in the updates to the child feature class.
// calculation attribute rule on child
// field: if you want to get only one field from the parent, then chose
// that field. if you want to get multiple fields, leave empty
// triggers: Insert(, update)
// load the related parent features using
// a relationship class between parent and child:
var parent_fs = FeatureSetByRelationshipName($feature, "db.schema.AttributeRuleName")
// return nothing if no parent feature was found
var parent = First(parent_fs)
if(parent == null) { return }
// if you want to return multiple fields:
var att = {
"accountableorganization": parent.accountableorganization,
"coordinatex": parent.coordinatex,
"coordinatey": parent.coordinatey
}
return {"result": {"attributes": att}}
nice - attribute rule are such a pain to debug! I hope ESRI improves the console output/logging.