I need to calculate certain field in every feature class within a file geodatabase in case this field exists. If it doesn't, I want iterator to skip appropriate feature class. I'm building a model, but cannot get it work.
I know, that in ArcGIS Pro one can use "If field exists" model tool inside the builder. But I'm working in ArcGIS 10.3 (standard licence). And in my case the solution would be "Calculate value" tool (as long as I'm not good in Python). The code is:
#Expression:
hasField(r"<path>.gdb\%Name%")
#CodeBlock:
import arcpy,os
def hasField(fc):
arcpy.env.workspace = os.path.dirname(fc)
fieldList = [field.name for field in arcpy.ListFields(fc)]
if "GLOBALID" in fieldList:
b = False
else:
b = True
return b
"GLOBALID" is a field name. I get errors, running code.
ERROR 000539: Error running expression: hasField(r".gdb\AdmBorder") Traceback (most recent call last): File "", line 1, in File "", line 4, in hasField File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy_init_.py", line 1131, in ListFields return gp.listFields(dataset, wild_card, field_type) File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\geoprocessing_base.py", line 344, in listFields self._gp.ListFields(*gp_fixargs(args, True))) IOError: ".gdb\AdmBorder" does not exist
So I cannot get "True" and "False" branches, to continue the model. The code for calculation ("Calculate field" tool): #Expression:
ID()
#CodeBlock:
def ID():
import uuid
return '{' + str(uuid.uuid4()) + '}'
When I changed the expression in the "Expression" window of the "Calculate Value" block to the destination of the first (in a row) iterated feature dataset: hasField(r"C:\Users\Vadim\Downloads\GML\esri\PetrovskySS.gdb\Adme\%Name%") And after running model got the error ""The process did not execute because the precondition is false" and "A column was specified that does not exist. Failed to execute (Calculate Field)". How should I change connections between blocks in the model and how to modify the expression in the "Calculate Value" block? All the featureclasses are in the feature datasets.
<path>
is mapping to an empty string. You can focus the Question on this, because the ID assignment code is irrelevant (though best practice in Python is to not use leading uppercase or all-caps in function names).