<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Grab the domain's values when exporting attachments in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/grab-the-domain-s-values-when-exporting/m-p/1143930#M63751</link>
    <description>&lt;P&gt;For your second question, since the table is created in the 'memory' workspace, it is sent to the digital abyss when there is no more reference/ need for it and garbage collector does its thing.&amp;nbsp; Or when the script ends and python releases the memory.&lt;/P&gt;</description>
    <pubDate>Tue, 15 Feb 2022 03:52:15 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2022-02-15T03:52:15Z</dc:date>
    <item>
      <title>Grab the domain's values when exporting attachments</title>
      <link>https://community.esri.com/t5/python-questions/grab-the-domain-s-values-when-exporting/m-p/1143261#M63731</link>
      <description>&lt;P&gt;Hey all,&lt;/P&gt;&lt;P&gt;I'm using field values as input for naming exported attachments, and I'm running into an issue with domains.&lt;/P&gt;&lt;P&gt;Namely, if the field uses a coded value domain, it will grab the code and not the value.&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I grab the value?&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.esri.com/t5/python-questions/accessing-coded-domain-values-using-python/m-p/18101/highlight/true#M1432" target="_blank" rel="noopener"&gt;I found this post here&lt;/A&gt;&amp;nbsp;but I'm not sure how to put it together; I was able to create the initial dictionary of codes and values, but I'm not sure how to connect that to my fields afterwards.&lt;/P&gt;&lt;P&gt;The code runs as :&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Look up the RELKEY from the Attachment Table in the Feature Class&lt;/LI&gt;&lt;LI&gt;Grab the Name from the Feature Class (End here if no domain or if the domain is a range.)&lt;/LI&gt;&lt;LI&gt;Use the Name from the Feature Class to Lookup the Coded value in the Domain Table&lt;/LI&gt;&lt;LI&gt;Grab the Value for the Code from the Domain Table&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="AlfredBaldenweck_0-1644606698839.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/33753i3A9A6BA8056839BA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="AlfredBaldenweck_0-1644606698839.png" alt="AlfredBaldenweck_0-1644606698839.png" /&gt;&lt;/span&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;SPAN&gt;I have steps 1,2, and 4, but I can't figure out how to make step 3 work.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Code sample&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;'''Rough order of how I think it should go? 
Step 1 is definitely supposed to be at the bottom'''

'''Step 4''' # Mostly lifted directly from that example I link
testDom = arcpy.ListFields(inFC)
for field in testDom:
    if field.domain != "": #I only care about domains if there is one. Also need to specify that it's a coded value domain somehow.
        cvdTable = arcpy.DomainToTable_management(myGDB, field.domain, 'in_memory/cvdTable', 'codeField', 'descriptionField')
        d = {}  #Create an empty dictionary.
        rows = arcpy.SearchCursor(cvdTable)  #Create a cursor to loop through the table holding the domain and code info 
        for row in rows:   # Loop through each row in the table. 
            d[row.codeField] = row.descriptionField # For each row in the table populate the key with the code field and the value from the description field   

'''Step 2'''
primeNames = dict()
with arcpy.da.SearchCursor(FeatureClass, [relPairO, nameField1]) as cursor:
    for row in cursor:
        primeNames[row[0]] = row[1] #Every time you check this dictionary for the relation key, it will direct you to the right entry in the primary naming field.



'''Step 1'''
with arcpy.da.SearchCursor(inTable,['DATA', 'ATT_NAME', 'ATTACHMENTID', relPairA]) as cursor:
    for row in cursor:
        relPairKey = row[3] 
        fieldValue = primeNames[relPairKey] # Value of specified field in feature class

&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, this is a total beginner question, but in the example that I linked, what happens to that domain table? Is it automatically deleted?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Fri, 11 Feb 2022 19:28:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/grab-the-domain-s-values-when-exporting/m-p/1143261#M63731</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2022-02-11T19:28:31Z</dc:date>
    </item>
    <item>
      <title>Re: Grab the domain's values when exporting attachments</title>
      <link>https://community.esri.com/t5/python-questions/grab-the-domain-s-values-when-exporting/m-p/1143929#M63750</link>
      <description>&lt;P&gt;Looks like you are creating the code:value dictionary in the first cursor, but since its scope is under the for loop, it is destroyed when the loop finishes.&amp;nbsp; Move the d = {} above the for loop so it persists and can be used in other parts of your code.&lt;/P&gt;&lt;P&gt;To get the code value, it would be a simple g.get('code (key) here', &amp;lt;default value here if you want it to return a value when the code is not in the keys&amp;gt;)&lt;/P&gt;</description>
      <pubDate>Tue, 15 Feb 2022 03:47:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/grab-the-domain-s-values-when-exporting/m-p/1143929#M63750</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-02-15T03:47:37Z</dc:date>
    </item>
    <item>
      <title>Re: Grab the domain's values when exporting attachments</title>
      <link>https://community.esri.com/t5/python-questions/grab-the-domain-s-values-when-exporting/m-p/1143930#M63751</link>
      <description>&lt;P&gt;For your second question, since the table is created in the 'memory' workspace, it is sent to the digital abyss when there is no more reference/ need for it and garbage collector does its thing.&amp;nbsp; Or when the script ends and python releases the memory.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Feb 2022 03:52:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/grab-the-domain-s-values-when-exporting/m-p/1143930#M63751</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-02-15T03:52:15Z</dc:date>
    </item>
    <item>
      <title>Re: Grab the domain's values when exporting attachments</title>
      <link>https://community.esri.com/t5/python-questions/grab-the-domain-s-values-when-exporting/m-p/1144420#M63773</link>
      <description>&lt;P&gt;Good to know, thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 16 Feb 2022 00:31:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/grab-the-domain-s-values-when-exporting/m-p/1144420#M63773</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2022-02-16T00:31:38Z</dc:date>
    </item>
    <item>
      <title>Re: Grab the domain's values when exporting attachments</title>
      <link>https://community.esri.com/t5/python-questions/grab-the-domain-s-values-when-exporting/m-p/1144421#M63774</link>
      <description>&lt;P&gt;I ended up making a table from the domain, then joining to the original FC.&lt;/P&gt;&lt;P&gt;Below is my code sample, with the addition of lines #6-13, which validate that the domain is a Code Value Domain (checked in line #16).&lt;/P&gt;&lt;P&gt;This validation is necessary; if you use a Range domain with this workflow otherwise, it will return "Min Value", "Max Value", or nothing.&lt;/P&gt;&lt;P&gt;I have a little more stuff to do on this project, but the end is very near.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks everyone!&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def grabDomain (workspace, inFC, namefield, relPairO): 
    '''Grabs the Coded Value of a coded Value Domain'''
    ''' Otherwise just grabs the field's value'''

    names = dict()
    
    rawDomain = arcpy.da.ListDomains(workspace) #list of all domains in the GDB
    valDomain = [] #list of valid domains
    for dom in rawDomain:
        if dom.domainType != "Range": #add Coded Value Domains to the valid list
            valDomain.append(dom.name)
            
    listFlds = arcpy.ListFields(inFC, namefield)
    for field in listFlds:
        if (field.domain != "") and (field.domain in valDomain): #For retreiving Coded values
            #Create an in-memory table representing the domain.
            codeTable = arcpy.DomainToTable_management(workspace, field.domain, 'in_memory/codeTable', "codeField", "descriptionField")
            inFC2 = arcpy.management.AddJoin(inFC, namefield, codeTable, "codeField") # Join said table to your Feature Class.
            
            # Okay this next part is kinda tricky
            # perform your search cursor on the joined feature class, but use the ORIGINAL FC to find the field names.
            with arcpy.da.SearchCursor(inFC2, [f"{inFC}.{relPairO}", f"{inFC}.{namefield}", f"codeTable.descriptionField"]) as cursor:
                for row in cursor:
                    names[row[0]] = row[2]
    
        else: #Otherwise just grabs the code
            with arcpy.da.SearchCursor(inFC, [relPairO, namefield]) as cursor:
                for row in cursor:
                    names[row[0]] = row[1] #Every time you check this dictionary for the relation key, it will direct you to the right entry in the primary naming field.
    return names&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Feb 2022 00:37:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/grab-the-domain-s-values-when-exporting/m-p/1144421#M63774</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2022-02-16T00:37:40Z</dc:date>
    </item>
  </channel>
</rss>

