AnsweredAssumed Answered

Pro:How to check if domain exists, if not, do something else skip

Question asked by tpcolson on Jun 10, 2018
Latest reply on Jun 14, 2018 by Dan_Patterson

I'm attempting to use py to check if a domain exists, if not, create it, else do nothing. After consulting How do I check if a domain already exists? , arcgis 10.0 - Using arcpy.Exists for domains? - Geographic Information Systems Stack Exchange , and esri geodatabase - Checking if domain already exists using ArcPy? - Geographic Information Systems Stack Exchange I came up with the attached code. If neither domain exists, it fails with:

Start Time: Sunday, June 10, 2018 2:28:13 PM
Running script Add Extended Core Fields...
Failed script Add Extended Core Fields...
Traceback (most recent call last):
File "C:\Temp\PRO_FOLDER_STRUCTURE\EXTENDED_CORE_FIELDS.py", line 24, in <module>
arcpy.CreateDomain_management(gdb_name, "DOM_YES_NO_UNK_NPS2016", "Is the feature physically observable", "TEXT", "CODED")
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 1332, in CreateDomain
raise e
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 1329, in CreateDomain
retval = convertArcObjectToPythonObject(gp.CreateDomain_management(*gp_fixargs((in_workspace, domain_name, domain_description, field_type, domain_type, split_policy, merge_policy), True)))
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing\_base.py", line 496, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000192: Invalid value for Domain Name
Failed to execute (CreateDomain).
Failed to execute (AddExtendedCoreFields).
Failed at Sunday, June 10, 2018 2:28:14 PM (Elapsed Time: 1.52 seconds)

However it creates the domain!

 

If I run it again knowing the domain exists, same error message. 

 

import os
import errno
import arcpy
#Get user input
gdb_name = arcpy.GetParameterAsText(0)
fc_name = arcpy.GetParameterAsText(1)
isOBSERVABLEchecked = arcpy.GetParameterAsText(2)
isISEXTANTchecked = arcpy.GetParameterAsText(3)





#Create the Optional OBSERVABLE domain

if str(isOBSERVABLEchecked) == 'true':
        existingDomains = arcpy.da.ListDomains(gdb_name)
        isthere = False
        for domain in existingDomains:
                if domain.name == 'DOM_YES_NO_UNK_NPS2016':
                        isthere = True
                        break
                if isthere == False:
                        arcpy.CreateDomain_management(gdb_name, "DOM_YES_NO_UNK_NPS2016", "Is the feature physically observable", "TEXT", "CODED")
                        YESNODict = {"Unknown":"Unknown", "Yes": "Yes", "No": "No"}
                        for code in YESNODict:       
                                arcpy.AddCodedValueToDomain_management(gdb_name, "DOM_YES_NO_UNK_NPS2016", code, YESNODict[code])
                else:
                        arcpy.AddMessage("DOM_YES_NO_UNK_NPS2016 already exists")
                       


#Create the Optional ISEXTANT domain
if str(isISEXTANTchecked) == 'true':
        existingDomains = arcpy.da.ListDomains(gdb_name)
        isthere = False
        for domain in existingDomains:
                if domain.name == 'DOM_YES_NO_UNK_NPS2016':
                        isthere = True
                        break
                if isthere == False:
                                arcpy.CreateDomain_management(gdb_name, "DOM_ISEXTANT_NPS2016", "Indicates feature or features that are no longer in existence or partially in existence.", "TEXT", "CODED")
                                ISEXTANTDict = {"Unknown":"Unknown", "True": "True", "False": "False", "Partial": "Partial", "Other": "Other"}
                                for code in ISEXTANTDict:       
                                        arcpy.AddCodedValueToDomain_management(gdb_name, "DOM_ISEXTANT_NPS2016", code, ISEXTANTDict[code])

                else:
                        arcpy.AddMessage("DOM_ISEXTANT_NPS2016 already exists")

 

As best as I can tell, I have an illegal loop condition that keeps trying to create the first domain whether it exists or not, and the failure stems from the domain name already exists. 

Outcomes