How can I CalculateField_Management a field that has a Domain based schema?

233
3
Jump to solution
04-02-2019 04:06 PM
John_Spence
Occasional Contributor

I'm having a DOH! moment here am can't seem to pull myself out of the nosedive.  Converting my scripts using arcpy.CalculateField_management from VB to Python and ran into a small hiccup.

Using VB, this is what I did:

arcpy.CalculateField_management(combinedOutFC + '__ATTACH_vw', 'REL_GLOBALID', '[DBName."Domain\!UserSchema".FC_Publish.GlobalID]')

But in Python, that is not cool so I did this:

arcpy.CalculateField_management(combinedOutFC + '__ATTACH_vw', 'REL_GLOBALID', '!GlobalID!', 'PYTHON_9.3'

Here is the caveat:

Prior to this I performed a join:  

arcpy.AddJoin_management(combinedOutFC + "__ATTACH_vw", "REL_GLOBALID", combinedOutFC, "GUIDCopy")

Where I am going wrong with:

arcpy.CalculateField_management(combinedOutFC + '__ATTACH_vw', 'REL_GLOBALID', '!GlobalID!', 'PYTHON_9.3'

Thanks ahead of time for what I know will be "oh yeah" moment of the day for me.    Thank you all for your help over the years.

Oh!!!  Error is looking like this:

error encountered.ERROR 000539: Invalid field FeatureClass.GlobalID
Failed to execute (CalculateField).
PYTHON ERRORS:
Traceback info:
File "Publish.py", line 182, in <module>
arcpy.CalculateField_management(combinedOutFC + '__ATTACH_vw', 'REL_GLOBALID', '!' + combinedOutFC + '.GlobalID!', 'PYTHON_9.3')

Error Info:
ERROR 000539: Invalid field FeatureClass.GlobalID
Failed to execute (CalculateField).
Closing Log File and Exiting Process.

0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor

Your error dump messages do not match your code.

I suspect you need a prefix and "." to access the GlobalID field in the related table, especially when there are unusual paths to access the data sources.  Although a usually helpful way to find the syntax is to use Describe to use the name property to get the prefix. Note my use of Python .format() -- much easier when the syntax gets weird to keep things straight!

tbl = "{}__ATTACH_vw".format(combinedOutFC)
expr = "!{}.GlobalID!".format(arcpy.Describe(tbl).name)‍‍‍‍‍‍

this doesn't always work, specially when working with SDE joins, raster tables, etc. In these cases sometimes I need to resort to printing a debug message with the field names so I can see what syntax Python is looking for to access the joined field:

print([f.name for f in arcpy.ListFields(tbl)])‍‍

View solution in original post

3 Replies
curtvprice
MVP Esteemed Contributor

Your error dump messages do not match your code.

I suspect you need a prefix and "." to access the GlobalID field in the related table, especially when there are unusual paths to access the data sources.  Although a usually helpful way to find the syntax is to use Describe to use the name property to get the prefix. Note my use of Python .format() -- much easier when the syntax gets weird to keep things straight!

tbl = "{}__ATTACH_vw".format(combinedOutFC)
expr = "!{}.GlobalID!".format(arcpy.Describe(tbl).name)‍‍‍‍‍‍

this doesn't always work, specially when working with SDE joins, raster tables, etc. In these cases sometimes I need to resort to printing a debug message with the field names so I can see what syntax Python is looking for to access the joined field:

print([f.name for f in arcpy.ListFields(tbl)])‍‍

View solution in original post

John_Spence
Occasional Contributor

Oh my, that was an approach I had not considered.  Definitely going to give that a shot this morning and will drop you a line with the outcome. 

0 Kudos
John_Spence
Occasional Contributor

So...after further investigation it turns out:

  1. The field is a GlobalID (not modifiable/calculable via this method).  It need to be a GUID to be changeable this way if memory serves.
  2. I'm hanging my head in shame standing in the corner. 

Thank you for your insight as it was incredibly helpful.