Hello all,
So, I have this model, and so far, it runs pretty well (rare anomalous failure here and there).
However, because of updates to the source data I need to get this model to run with an "If Field Exists" logic. Basically, I want the model to iterate though some feature classes, and if a field exists, I want it to calculate another field and continue on with the downline processes. If the field does not exist, I want the model to add the field, then calculate the field and continue on with downline processes. I've set both True and False as preconditions for the processes I want them to carry out, but nothing downline will validate.
And if I try to reconnect the output (i.e. "Selections") to the "Add Field" tool, the model will validate but will generate an empty output because all the Inputs are not current.
I'm lost in the sauce over here. Please help! Thank you!
To clarify, you want to check if FieldA exists, then calculate it if it does, OR add, then calculate it?
Doesn't Field Calculator just create the field if it doesn't already exist?
I was really hoping to find an answer here because I'm having a similar issue with Extract Values to Points. That is, after the first run the field "RASTERVALU" exists and triggers an error for the next run. I would like to check if the field exists and if it does, skip the extract values function and move to the next step.
Initially, and ideally, I was hoping to find a way to make this function just overwrite an existing field (but this does not seem to be an option) since any pre-existing data, in my case, would have been edited by the rest of the modelbuilder code (e.g. Nulls removed).
Ohhh I finally understand.
What you all want is the Calculate Value and the Merge Branch function.
Merge Branch takes whatever branches you feed it and it outputs either True or False. The tough thing with it is that if, for example, you feed it the updated table from "Add Field", but you didn't create an updated table because you didn't need to add the field, you get a False as a result. Which, if you use as a precondition to Calculate Field, you won't be able to calculate the dang field. So we have to get a little creative:
Calculate Value lets you set create a variable of any value you choose. In this case, I use it twice (once for each path) and calculate the same value of True to feed to Merge Branch.
Check the gif below, where I use this to add the field and calculate it, then I change the calculation expression when I run it again and don't have to add the field.
Report when does not exist:
Executing (If Field Exists): FieldExistsIfThenElse C:\Users\local_abaldenweck\Temp\ArcGISProTemp7216\Untitled\Default.gdb\test__ATTACH "Must have at least one field" ATT_NAME2
Start Time: Tuesday, October 15, 2024 10:09:57 AM
Succeeded at Tuesday, October 15, 2024 10:09:57 AM (Elapsed Time: 0.03 seconds)
Executing (Calculate Value (2)): CalculateValue True # Variant
Start Time: Tuesday, October 15, 2024 10:09:57 AM
The process did not execute because the precondition is false.
Succeeded at Tuesday, October 15, 2024 10:09:57 AM (Elapsed Time: 0.00 seconds)
Executing (Add Field): AddField C:\Users\local_abaldenweck\Temp\ArcGISProTemp7216\Untitled\Default.gdb\test__ATTACH ATT_NAME2 "Long (32-bit integer)" # # # # NULLABLE NON_REQUIRED #
Start Time: Tuesday, October 15, 2024 10:09:57 AM
Adding ATT_NAME2 to test__ATTACH...
Succeeded at Tuesday, October 15, 2024 10:09:58 AM (Elapsed Time: 0.25 seconds)
Executing (Calculate Value): CalculateValue True # Variant
Start Time: Tuesday, October 15, 2024 10:09:58 AM
Value = 1
Succeeded at Tuesday, October 15, 2024 10:09:58 AM (Elapsed Time: 0.01 seconds)
Executing (Merge Branch): MergeBranch 1;1
Start Time: Tuesday, October 15, 2024 10:09:58 AM
Succeeded at Tuesday, October 15, 2024 10:09:58 AM (Elapsed Time: 0.00 seconds)
Executing (Calculate Field): CalculateField C:\Users\local_abaldenweck\Temp\ArcGISProTemp7216\Untitled\Default.gdb\test__ATTACH ATT_NAME2 60 "Python 3" # Text NO_ENFORCE_DOMAINS
Start Time: Tuesday, October 15, 2024 10:09:58 AM
Succeeded at Tuesday, October 15, 2024 10:09:58 AM (Elapsed Time: 0.07 seconds)
Report when field does exist (note that I change the calculated value so I can tell the difference)
Executing (If Field Exists): FieldExistsIfThenElse C:\Users\local_abaldenweck\Temp\ArcGISProTemp7216\Untitled\Default.gdb\test__ATTACH "Must have at least one field" ATT_NAME2
Start Time: Tuesday, October 15, 2024 10:11:12 AM
Succeeded at Tuesday, October 15, 2024 10:11:12 AM (Elapsed Time: 0.02 seconds)
Executing (Calculate Value (2)): CalculateValue True # Variant
Start Time: Tuesday, October 15, 2024 10:11:12 AM
Value = 1
Succeeded at Tuesday, October 15, 2024 10:11:12 AM (Elapsed Time: 0.01 seconds)
Executing (Add Field): AddField C:\Users\local_abaldenweck\Temp\ArcGISProTemp7216\Untitled\Default.gdb\test__ATTACH ATT_NAME2 "Long (32-bit integer)" # # # # NULLABLE NON_REQUIRED #
Start Time: Tuesday, October 15, 2024 10:11:12 AM
The process did not execute because the precondition is false.
Succeeded at Tuesday, October 15, 2024 10:11:12 AM (Elapsed Time: 0.00 seconds)
Executing (Calculate Value): CalculateValue True # Variant
Start Time: Tuesday, October 15, 2024 10:11:12 AM
All the inputs are not current.
Succeeded at Tuesday, October 15, 2024 10:11:12 AM (Elapsed Time: 0.00 seconds)
Executing (Merge Branch): MergeBranch 1;1
Start Time: Tuesday, October 15, 2024 10:11:12 AM
Succeeded at Tuesday, October 15, 2024 10:11:12 AM (Elapsed Time: 0.01 seconds)
Executing (Calculate Field): CalculateField C:\Users\local_abaldenweck\Temp\ArcGISProTemp7216\Untitled\Default.gdb\test__ATTACH ATT_NAME2 990 "Python 3" # Text NO_ENFORCE_DOMAINS
Start Time: Tuesday, October 15, 2024 10:11:13 AM
Succeeded at Tuesday, October 15, 2024 10:11:13 AM (Elapsed Time: 0.06 seconds)
A few things to note as you read through:
1) Model builder apparently will try to execute everything, even when it doesn't have to. So you get a couple warnings because it can't do the stuff on the False path, for example.
2) More importantly, this works by feeding it the table and calculating the field. You can do that in this specific case because Add Field returns an updated table, so either way you're feeding Calculate the same thing. This is why I get an "All the inputs not current" warning. Other uses might not work so easily.
See the attached ATBX for the model I used for this.
I hope this helps!