Looking for some help with an Arcade expression to use as an Attribute Rule. Sometimes we copy and paste features within and/or different feature class and need a few fields that have records to show as Null in the new feature. I came up with the following expression, which verifies as valid but returns an error code when I copy/paste the feature. I have Insert trigger checked.
// Set fields to null if they have values
if (!IsEmpty("M_AssetNum") || !IsEmpty("M_InstallYear") || !IsEmpty("M_Material")) {
return {
"M_AssetNum": Null,
"M_InstallYear": Null,
"M_Material": Null
};
}
// If the fields are already empty, do nothing
return {};
This is the error message it returns after trying to paste.
Solved! Go to Solution.
Your code is not checking if the VALUES in those fields are not empty, your code is checking if those specific strings/text values are not empty. It would be like writing !IsEmpty("here is a big long text string").
You want to be checking "$feature.M_InstallYear", where "$feature" is a token representing the row (or a subset thereof, depending), and you're asking for the value that is in the M_InstallYear field.
Also, you're trying to return three results, so you need to specify those fields in your return results dictionary, I believe.
You also don't need to return the blank brackets. Just don't return anything if you don't need to perform a calculation. Alternatively, you could just always set these three values to NULL, without even doing the "if" statement. I can't imagine you're saving that much processing overhead by checking if you need to first, but someone else might know more about that.
// Specifically list fields to be used in this AR.
// Not always clear on when this is necessary (don't think it is here), but useful to know about.
Expects($feature, "M_AssetNum", "M_InstallYear", "M_Material")
if (!IsEmpty($feature.M_AssetNum) || !IsEmpty($feature.M_InstallYear) || !IsEmpty($feature.M_Material)) {
return {
"result": {
"attributes": {
"M_AssetNum": null,
"M_InstallYear": null,
"M_Material": null
}
}
}
}
Your code is not checking if the VALUES in those fields are not empty, your code is checking if those specific strings/text values are not empty. It would be like writing !IsEmpty("here is a big long text string").
You want to be checking "$feature.M_InstallYear", where "$feature" is a token representing the row (or a subset thereof, depending), and you're asking for the value that is in the M_InstallYear field.
Also, you're trying to return three results, so you need to specify those fields in your return results dictionary, I believe.
You also don't need to return the blank brackets. Just don't return anything if you don't need to perform a calculation. Alternatively, you could just always set these three values to NULL, without even doing the "if" statement. I can't imagine you're saving that much processing overhead by checking if you need to first, but someone else might know more about that.
// Specifically list fields to be used in this AR.
// Not always clear on when this is necessary (don't think it is here), but useful to know about.
Expects($feature, "M_AssetNum", "M_InstallYear", "M_Material")
if (!IsEmpty($feature.M_AssetNum) || !IsEmpty($feature.M_InstallYear) || !IsEmpty($feature.M_Material)) {
return {
"result": {
"attributes": {
"M_AssetNum": null,
"M_InstallYear": null,
"M_Material": null
}
}
}
}
Thank you @VinceE. Your code worked as expected. Much appreciated.