I am trying to write a Calculation style Attribute Rule to restrict entries into a field to be unique values within the field. As an example, more than one row in the given table can't have a value of "TEST" in the given field--entries must be unique.
The below achieves this (might not handle NULLs, but I can sort that out). What is constantly irritating to me is how I cannot figure out how to use the variables defined on the first few lines in the "FeatureSetByName()" and "Expects()" function calls? Updating the FeatureSetByName() call with the fcName and fldName variables will work when I hit "OK" in the AR code editor, but then fail when I try to save the rule. Updating Expects() to use the fldName variable throws an error right in the editor.
Why do these functions work this way? If I change the name of my field, I have to go in and retype it 3 different times in the code? Why can I not just use one single variable for those names? Is this some Arcade/JavaScript idiosyncrasy I just don't understand?
Thanks in advance to anyone who can sort me out on this. The point is to create a rule that can be used in different locations, with minimal updates to only the INPUTS section. Esri's example (link in the code below) hard-codes things, which is silly from a reusability standpoint.
// INPUTS
var fcName = "TEST";
var fldName = "Name_Text";
// These two lines must be written with the actual strings, not the variables above.
var features = FeatureSetByName($datastore, "TEST", ["Name_Text"], false);
Expects($feature, "Name_Text");
// MAIN
// If any values in the field match the current row's value (and the IDs are different),
// this is a duplicate, and an error.
// Largely copied from:
// https://support.esri.com/en-us/knowledge-base/how-to-identify-a-duplicate-field-value-using-an-attrib-000029088
for (var i in features) {
if ((i[fldName] == $feature[fldName]) && (i.OBJECTID != $feature.OBJECTID)) {
return {"errorMessage": "ERROR: value not unique within field!"}
}
}
// If error above not found, return results dict with value as-is.
var fldUpdates = Dictionary()
fldUpdates[fldName] = $feature[fldName]
Console({"result": {"attributes": fldUpdates}})
return {"result": {"attributes": fldUpdates}}