[GIS] Filtered list in ArcGIS toolbox

arcpypython-toolboxtool-validation

My goal is to grab a bunch of feature classes, select the first feature class, list the fields, and return that list to parameter 1 as a filtered list.

My ArcGIS toolbox is setup with 2 parameters.

The first parameter is a multivalue featureclass

The second parameter is a string with filter set to: "Value List" which is empty. I'm hoping to populate this. When I run the following code:

def updateParameters(self):

    if self.params[0].value: #when user inputs feature classe(s)

      fcs = self.params[0].value.value # getting list of fcs as string?

      spl = fcs.split(";") #splitting list of fcs

      fields = arcpy.ListFields(spl[0]) #getting list of fields from 1st featureclass

      self.params[1].filter.list = fields #return list of fields to param 1

When I input the fcs into param 0, I get this error:

ERROR
updateParameters Execution Error: Runtime error : 'ValueTable' object has no attribute 'value'

Best Answer

The code you require is:

  def updateParameters(self):
    """Modify the values and properties of parameters before internal
    validation is performed.  This method is called whenever a parameter
    has been changed."""
    if self.params[0].value:
        fcs = self.params[0].value.exportToString()
        spl = fcs.split(";")
        desc = arcpy.Describe(spl[0])
        fields = desc.fields # A list of of Field objects
        list = []
        for f in fields:
            list.append(f.name) # Transfer name to new list
        self.params[1].filter.list=list
    return

The help file suggests you DO NOT use ListFields but use Describe instead.

Related Question