<?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 Can I set up a field parameter without checking it is in the feature class? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/can-i-set-up-a-field-parameter-without-checking-it/m-p/267552#M20601</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I've got a script which does some calculations and stores them into fields. I've designed it so that if the fields don't exist they will be created but if they do exist they will be overwritten. When I set up the tool in Arc I tell it that the field parameters are of type field and are derived from the feature class being input. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; The problem is, when I run the tool if I enter new field names it stops me with an error because the fields do not exist. I do still want the combo provided from the field type since there are cases where the field will exist though. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; I'm thinking it might be a matter of adjusting the validation logic but I've no experience doing that.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; Any ideas? Thanks in advance.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 19 Feb 2013 00:31:21 GMT</pubDate>
    <dc:creator>AllanMills</dc:creator>
    <dc:date>2013-02-19T00:31:21Z</dc:date>
    <item>
      <title>Can I set up a field parameter without checking it is in the feature class?</title>
      <link>https://community.esri.com/t5/python-questions/can-i-set-up-a-field-parameter-without-checking-it/m-p/267552#M20601</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I've got a script which does some calculations and stores them into fields. I've designed it so that if the fields don't exist they will be created but if they do exist they will be overwritten. When I set up the tool in Arc I tell it that the field parameters are of type field and are derived from the feature class being input. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; The problem is, when I run the tool if I enter new field names it stops me with an error because the fields do not exist. I do still want the combo provided from the field type since there are cases where the field will exist though. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; I'm thinking it might be a matter of adjusting the validation logic but I've no experience doing that.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; Any ideas? Thanks in advance.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Feb 2013 00:31:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/can-i-set-up-a-field-parameter-without-checking-it/m-p/267552#M20601</guid>
      <dc:creator>AllanMills</dc:creator>
      <dc:date>2013-02-19T00:31:21Z</dc:date>
    </item>
    <item>
      <title>Re: Can I set up a field parameter without checking it is in the feature class?</title>
      <link>https://community.esri.com/t5/python-questions/can-i-set-up-a-field-parameter-without-checking-it/m-p/267553#M20602</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Allan,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Can you post your script and the error messages you get?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Feb 2013 18:28:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/can-i-set-up-a-field-parameter-without-checking-it/m-p/267553#M20602</guid>
      <dc:creator>MikeMacRae</dc:creator>
      <dc:date>2013-02-19T18:28:46Z</dc:date>
    </item>
    <item>
      <title>Re: Can I set up a field parameter without checking it is in the feature class?</title>
      <link>https://community.esri.com/t5/python-questions/can-i-set-up-a-field-parameter-without-checking-it/m-p/267554#M20603</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;It's not the script that is throwing errors. It's when I try to set it up as a geoprocessing tool in Arc. One of the parameters is a feature class and three others are fields for that feature class - two of which might not exist yet.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Feb 2013 19:31:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/can-i-set-up-a-field-parameter-without-checking-it/m-p/267554#M20603</guid>
      <dc:creator>AllanMills</dc:creator>
      <dc:date>2013-02-19T19:31:07Z</dc:date>
    </item>
    <item>
      <title>Re: Can I set up a field parameter without checking it is in the feature class?</title>
      <link>https://community.esri.com/t5/python-questions/can-i-set-up-a-field-parameter-without-checking-it/m-p/267555#M20604</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;when I run the tool if I enter new field names it stops me with an error because the fields do not exist. I do still want the combo provided from the field type since there are cases where the field will exist though. &lt;BR /&gt;&lt;BR /&gt; I'm thinking it might be a matter of adjusting the validation logic but I've no experience doing that.&lt;BR /&gt;&lt;BR /&gt; Any ideas? Thanks in advance.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The easy way to do this is to provide the field &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;name&lt;/SPAN&gt;&lt;SPAN&gt; as a string parameter (you can give it a default name and make it optional to save user typing).&amp;nbsp; Then your script can see if the field is there and act accordingly. It's best practice to name the string parameter a "field name" instead of a "field" so it's obvious that it's a string. (Like the Add Field tool.) &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You can't have a pick list without writing python validation code to populate the list of picks with the existing field names (using arcpy.Describe().Fields). You'd want to add a user entered field name (if they did that) to your list. This could get complicated and I'm not sure it is worth the effort. However, if you really want to dive into that, the help is here:&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//00150000000t000000"&gt;Customizing script tool behavior&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Feb 2013 21:36:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/can-i-set-up-a-field-parameter-without-checking-it/m-p/267555#M20604</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2013-02-19T21:36:11Z</dc:date>
    </item>
    <item>
      <title>Re: Can I set up a field parameter without checking it is in the feature class?</title>
      <link>https://community.esri.com/t5/python-questions/can-i-set-up-a-field-parameter-without-checking-it/m-p/267556#M20605</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks. I'll see how I go.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Feb 2013 22:10:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/can-i-set-up-a-field-parameter-without-checking-it/m-p/267556#M20605</guid>
      <dc:creator>AllanMills</dc:creator>
      <dc:date>2013-02-19T22:10:24Z</dc:date>
    </item>
    <item>
      <title>Re: Can I set up a field parameter without checking it is in the feature class?</title>
      <link>https://community.esri.com/t5/python-questions/can-i-set-up-a-field-parameter-without-checking-it/m-p/267557#M20606</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I've wanted to do so myself, so I implemented some validation code to do this. In the documentation you would specify that the field must be FLOAT or DOUBLE.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
&amp;nbsp; def updateParameters(self):
&amp;nbsp;&amp;nbsp;&amp;nbsp; """Modify the values and properties of parameters before internal
&amp;nbsp;&amp;nbsp;&amp;nbsp; validation is performed.&amp;nbsp; This method is called whenever a parmater
&amp;nbsp;&amp;nbsp;&amp;nbsp; has been changed."""
&amp;nbsp;&amp;nbsp;&amp;nbsp; if self.params[0].value:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fldName = self.params[1].value
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if not fldName:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # set default value
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fldName = "XFIELD"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # validate field name
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; wk = arcpy.Describe(self.params[0].value).catalogPath&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fldName = arcpy.ValidateFieldName(fldName,wk).upper()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.params[1].value = fldName
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # populate filter (picklist) with the current field name
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # plus existing field names
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; flds = arcpy.Describe(self.params[0].value).Fields
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fldNames = [f.name.upper() for f in flds if f.type not in ["OID","Geometry"]]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.params[1].filter.list = [fldName] + fldNames
&amp;nbsp;&amp;nbsp;&amp;nbsp; return

&amp;nbsp; def updateMessages(self):
&amp;nbsp;&amp;nbsp;&amp;nbsp; """Modify the messages created by internal validation for each tool
&amp;nbsp;&amp;nbsp;&amp;nbsp; parameter.&amp;nbsp; This method is called after internal validation."""
&amp;nbsp;&amp;nbsp;&amp;nbsp; # If field exists, make sure it's a numeric field
&amp;nbsp;&amp;nbsp;&amp;nbsp; fldName = self.params[1].value
&amp;nbsp;&amp;nbsp;&amp;nbsp; if fldName:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; flds = arcpy.Describe(self.params[0].value).Fields
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fldNames = [f.name.upper() for f in flds]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fType = flds[fldNames.index(fldName)].type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # no error means field found - make sure it's a number
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if fType not in ["Double","Single"]: 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.params[1].setIDMessage("Error",889) # invalid field type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; except:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass

&amp;nbsp;&amp;nbsp;&amp;nbsp; return
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:06:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/can-i-set-up-a-field-parameter-without-checking-it/m-p/267557#M20606</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-11T13:06:56Z</dc:date>
    </item>
  </channel>
</rss>

