Conditional Drop Down Lists - Tool Validator

3532
17
Jump to solution
03-07-2018 05:23 PM
AmandaMacDonald-Creevey
New Contributor II

I'm trying to use the ToolValidator function to update the content of the drop down lists. I've followed the steps from https://blogs.esri.com/esri/arcgis/2011/08/25/generating-a-choice-list-from-a-field/  but need to take it a step further and populate the 'HA' drop down list with the values in the 'HarvestArea' field that start with the 'ForestID'. 

Any help on how to format this in the ToolValidator script (see below) would be much appreciated! 

import arcpy
class ToolValidator(object):
  """Class for validating a tool's parameter values and controlling
  the behavior of the tool's dialog."""


  def __init__(self):
    """Setup arcpy and the list of tool parameters."""
    self.params = arcpy.GetParameterInfo()
    self.fcfield = (None, None)

  def initializeParameters(self):
    """Refine the properties of a tool's parameters.  This method is
    called when the tool is opened."""

    return

  def updateParameters(self):
    """Modify the values and properties of parameters before internal
    validation is performed.  This method is called whenever a parmater
    has been changed."""

    if self.params[0].value and self.params[1].value:
      fc, col = str(self.params[0].value), str(self.params[1].value)
      if self.fcfield != (fc, col):
        self.fcfield = (fc, col)
        self.params[2].filter.list = [str(val) for val in
                                        sorted(
                                          set(
                                            row.getValue(col)
                                            for row in arcpy.SearchCursor(fc,
                                                                          fields=col)
                                              )
                                           )
                                      ]
        if self.params[2].value not in self.params[2].filter.list:
          self.params[2].value = self.params[2].filter.list[0]

  def updateMessages(self):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""

    return
1 Solution

Accepted Solutions
RandyBurton
MVP Regular Contributor

I looked at the blog post mentioned in your post.  I believe you need to add an additional parameter for the name of the second field.  The following code gives you an idea of what to add/modify in the validator section. Once you have the value of the first field, it will be used in a where clause to filter the results of the second field selection.  Note that the code does not check for field types, so the where clause may not be properly formed.

Dual Value List

import arcpy
class ToolValidator(object):
  """Class for validating a tool's parameter values and controlling
  the behavior of the tool's dialog."""


  def __init__(self):
    """Setup arcpy and the list of tool parameters."""
    self.params = arcpy.GetParameterInfo()
    self.fcfld_1 = (None, None)
    self.fcfld_2 = (None, None)

  def initializeParameters(self):
    """Refine the properties of a tool's parameters.  This method is
    called when the tool is opened."""

    return

  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 and self.params[1].value and self.params[2].value and self.params[3].value: # get second field and value list
        fc, c_2 = str(self.params[0].value), str(self.params[3].value)
        if self.fcfld_2 != (fc, c_2):
            self.fcfld_2 = (fc, c_2)
            wc = "{} = {}".format(self.params[1].value, str(self.params[2].value)) # where clause does not check field type
            self.params[4].filter.list = [str(val) for val in sorted(set(row.getValue(c_2) for row in arcpy.SearchCursor(fc,fields=c_2, where_clause=wc)))]
        if self.params[4].value not in self.params[4].filter.list:
            self.params[4].value = self.params[4].filter.list[0]

    elif self.params[0].value and self.params[1].value: # get first field and value list
        fc, c_1 = str(self.params[0].value), str(self.params[1].value)
        if self.fcfld_1 != (fc, c_1):
            self.fcfld_1 = (fc, c_1)
            self.params[2].filter.list = [str(val) for val in sorted(set(row.getValue(c_1) for row in arcpy.SearchCursor(fc,fields=c_1)))]
        if self.params[2].value not in self.params[2].filter.list:
            self.params[2].value = self.params[2].filter.list[0]

    return

  def updateMessages(self):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""

    return

Hope this helps.

View solution in original post

17 Replies
RandyBurton
MVP Regular Contributor

Xander Bakker‌ has a blog post on this topic with a good code example: Implementing cascading drop down lists in a toolbox using validation with Python.

RandyBurton
MVP Regular Contributor

I looked at the blog post mentioned in your post.  I believe you need to add an additional parameter for the name of the second field.  The following code gives you an idea of what to add/modify in the validator section. Once you have the value of the first field, it will be used in a where clause to filter the results of the second field selection.  Note that the code does not check for field types, so the where clause may not be properly formed.

Dual Value List

import arcpy
class ToolValidator(object):
  """Class for validating a tool's parameter values and controlling
  the behavior of the tool's dialog."""


  def __init__(self):
    """Setup arcpy and the list of tool parameters."""
    self.params = arcpy.GetParameterInfo()
    self.fcfld_1 = (None, None)
    self.fcfld_2 = (None, None)

  def initializeParameters(self):
    """Refine the properties of a tool's parameters.  This method is
    called when the tool is opened."""

    return

  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 and self.params[1].value and self.params[2].value and self.params[3].value: # get second field and value list
        fc, c_2 = str(self.params[0].value), str(self.params[3].value)
        if self.fcfld_2 != (fc, c_2):
            self.fcfld_2 = (fc, c_2)
            wc = "{} = {}".format(self.params[1].value, str(self.params[2].value)) # where clause does not check field type
            self.params[4].filter.list = [str(val) for val in sorted(set(row.getValue(c_2) for row in arcpy.SearchCursor(fc,fields=c_2, where_clause=wc)))]
        if self.params[4].value not in self.params[4].filter.list:
            self.params[4].value = self.params[4].filter.list[0]

    elif self.params[0].value and self.params[1].value: # get first field and value list
        fc, c_1 = str(self.params[0].value), str(self.params[1].value)
        if self.fcfld_1 != (fc, c_1):
            self.fcfld_1 = (fc, c_1)
            self.params[2].filter.list = [str(val) for val in sorted(set(row.getValue(c_1) for row in arcpy.SearchCursor(fc,fields=c_1)))]
        if self.params[2].value not in self.params[2].filter.list:
            self.params[2].value = self.params[2].filter.list[0]

    return

  def updateMessages(self):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""

    return

Hope this helps.

View solution in original post

AmandaMacDonald-Creevey
New Contributor II

Perfect that's exactly what I was trying to do - thanks so much!

0 Kudos
AmandaMacDonald-Creevey
New Contributor II

Hi again (Randy Burton), I'm trying to cascade these filter lists another step but I think I'm getting caught up on the where clause (python beginner here!)? I want to use the result from the second filter list (Harvest Area) to then determine the results of the third filter list (Setting).

import arcpy
class ToolValidator(object):
  """Class for validating a tool's parameter values and controlling the behavior of the tool's dialog."""

  def __init__(self):
    """Setup arcpy and the list of tool parameters."""
    self.params = arcpy.GetParameterInfo()
    self.fcfld_1 = (None, None)
    self.fcfld_2 = (None, None)
    self.fcfld_3 = (None, None)

    return

  def initializeParameters(self):
    """Refine the properties of a tool's parameters.  This method is
    called when the tool is opened."""

    return

  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."""


    forestfld = "Forest"
    hafld = "HarvestArea"
    settingfld = "Setting"
   
    if self.params[0].value and forestfld and self.params[1].value and hafld and self.params[2].value and settingfld: # get settings
        fc2, c_3 = str(self.params[0].value), str(settingfld)
        if self.fcfld_3 !=(fc2, c_3):
            self.fcfld_3 = (fc2, c_3)
            wc1 = "{} = {}".format(str(hafld), str(self.params[2].value))
            self.params[3].filter.list = [str(val) for val in sorted(set(row.getValue(c_3) for row in arcpy.SearchCursor(fc2,fields=c_3, where_clause=wc1)))]
        if self.params[3].value not in self.params[3].filter.list:
            self.params[3].value = self.params[3].filter.list[0]

    if self.params[0].value and forestfld and self.params[1].value and hafld: # get harvest area
        fc1, c_2 = str(self.params[0].value), str(hafld)
        if self.fcfld_2 !=(fc1, c_2):
            self.fcfld_2 = (fc1, c_2)
            wc = "{} = {}".format(str(forestfld), str(self.params[1].value))
            self.params[2].filter.list = [str(val) for val in sorted(set(row.getValue(c_2) for row in arcpy.SearchCursor(fc1,fields=c_2, where_clause=wc)))]
        if self.params[2].value not in self.params[2].filter.list:
            self.params[2].value = self.params[2].filter.list[0]
       
    if self.params[0].value and forestfld: # get forest
        fc, c_1 = str(self.params[0].value), str(forestfld)
        if self.fcfld_1 !=(fc, c_1):
            self.fcfld_1 = (fc, c_1)
            self.params[1].filter.list = [str(val) for val in sorted(set(row.getValue(c_1) for row in arcpy.SearchCursor(fc,fields=c_1)))]
        if self.params[1].value not in self.params[1].filter.list:
            self.params[1].value = self.params[1].filter.list[0]
   
    return

  def updateMessages(self):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""

    return

I'm also getting this error (see attached) where the format of the Harvest Area is 485-384-09. 

 

Any help would be much appreciated!

0 Kudos
RandyBurton
MVP Regular Contributor

The error message is suggesting that the string "203-001-01" is trying to be converted to an integer.  Perhaps the tool's parameter has the wrong data type selection or perhaps the where clause in the validation section is changing it.

I haven't had a chance to examine your validation code yet.  But I do have a some questions so that I may understand what you are wanting to accomplish.

It looks like you are trying to select rows from the feature class where Forest, HarvestArea and Setting contain specific values.  Are these fields all text fields, or are some integer or other data types?  Do all input feature classes contain these three fields (ie. they are always the same field names and data types)?  Can you share a few rows of sample data from your feature? 

AmandaMacDonald-Creevey
New Contributor II

All of the fields (Forest, HarvestArea and Setting) are text fields. The input feature class will be the same every time, so yes same field names and field types.

Aim is for the end user to select the forest (i.e. 403), they will then get the list of harvest areas within the 403 forest, then once a harvest area is selected (i.e. 403-048-03) then they can select a setting from the next list (i.e. 1, 2 or 3).

Hope that answers your questions.

0 Kudos
RandyBurton
MVP Regular Contributor

As Debugging a ToolValidator class suggests, I placed the ToolValidator in a script that could be run inside an IDE where I could use print statements for testing purposes.  There may be additional checks you will want to add to the code, such as a test that the 3 fields you are checking exist in the feature.  I used the following code:

# HELP at http://desktop.arcgis.com/en/arcmap/latest/analyze/creating-tools/debugging-a-toolvalidator-class.ht...

import arcpy

# Load the toolbox and get the tool's parameters, using the tool
#  name (not the tool label).
#
arcpy.ImportToolbox(r"C:\Path\To\Toolbox.tbx") # toolbox location
params = arcpy.GetParameterInfo("SelectAttributes") # name of script (not tool label)

# Set required parameters
#
params[0].value = r"C:\Path\To\geodatabase.gdb\Settings" # feature layer

# ToolValidator class block
# ----------------------------------------------------------------
class ToolValidator(object):

    def __init__(self):
        import arcpy
        self.params = arcpy.GetParameterInfo()

    def initializeParameters(self):
        # (initializeParameters code here)
        return

    def updateParameters(self):
        if self.params[0].value: # feature has been selected

            # may want to insert some code to insure that selected feature has fields 'Forest', 'HarvestArea' and 'Setting'

            fc, c_1 = str(self.params[0].value), 'Forest' # Check field 'Forest'
            self.params[1].filter.list = [str(val) for val in sorted(set(row.getValue(c_1) for row in arcpy.SearchCursor(fc,fields=c_1)))]
            if self.params[1].value not in self.params[1].filter.list:
                self.params[1].value = self.params[1].filter.list[0]
                print self.params[1].filter.list[0] ### Debug ###
                print len(self.params[1].filter.list) ### Debug ###

            fc, c_2 = str(self.params[0].value), 'HarvestArea' # Check field 'HarvestArea'
            wc2 = "Forest = '{}'".format(str(self.params[1].value)) # where assumes string values for fields
            self.params[2].filter.list = [str(val) for val in sorted(set(row.getValue(c_2) for row in arcpy.SearchCursor(fc,fields=c_2,where_clause=wc2)))]
            if self.params[2].value not in self.params[2].filter.list:
                self.params[2].value = self.params[2].filter.list[0]
                print self.params[2].filter.list[0] ### Debug ###
                print len(self.params[2].filter.list) ### Debug ###

            fc, c_3 = str(self.params[0].value), 'Setting' # Check field 'Setting'
            wc3 = "Forest = '{}' AND HarvestArea = '{}'".format(str(self.params[1].value), str(self.params[2].value)) # where assumes string values for fields
            self.params[3].filter.list = [str(val) for val in sorted(set(row.getValue(c_3) for row in arcpy.SearchCursor(fc,fields=c_3,where_clause=wc3)))]
            if self.params[3].value not in self.params[3].filter.list:
                self.params[3].value = self.params[3].filter.list[0]
                print self.params[3].filter.list[0] ### Debug ###
                print len(self.params[3].filter.list) ### Debug ###

        return

    def updateMessages(self):
          # (updateMessages code here)
          return
# ----------------------------------------------------------------
# Call routine(s) to debug
#
validator = ToolValidator()
validator.updateParameters()
validator.updateMessages()‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Once the code tested without errors I pasted it  (lines 17 to 59 minus the print lines 36, 37, 44, 45, 52 and 53) into the validation section of the script tool.  As my testing environment may be a bit different from yours, my script tool code used was:

import arcpy

inFC = arcpy.GetParameterAsText(0) # input feature class (Data Type 'Feature Layer')
forestVal = arcpy.GetParameterAsText(1) # selected value of Forest field (Data Type 'String')
harvestVal = arcpy.GetParameterAsText(2) # selected value of HarvestArea field (Data Type 'String')
settingVal = arcpy.GetParameterAsText(3) # selected value of Setting field (Data Type 'String')

forestFld = "Forest"
harvestFld = "HarvestArea"
settingFld = "Setting"

whereClause = "{} = '{}' AND {} = '{}' AND {} = '{}'".format(forestFld,forestVal,harvestFld,harvestVal,settingFld,settingVal)

arcpy.SelectLayerByAttribute_management(inFC, "NEW_SELECTION", where_clause=whereClause)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Note in the where clauses, since we are using text values, the format used was:

WHERE Field = 'some text'

You my need to adjust this for your SQL version and/or to escape certain characters.

Hope this helps and best of luck with your project.

RandyBurton
MVP Regular Contributor

I modified the code to do some error checking (basically to make sure the fields exist in the selected feature and to adjust for fields of either type string or number).  I also added some messages in the updateMessages code.

For the ToolValidator section:

class ToolValidator(object):

    def __init__(self):
        import arcpy
        self.params = arcpy.GetParameterInfo()

    def initializeParameters(self):
        # (initializeParameters code here)
        return

    def updateParameters(self):

        if self.params[0].value: # feature has been selected
            useFields = [u'Forest', u'HarvestArea', u'Setting'] # names of fields used by tool
            desc = arcpy.Describe(self.params[0].value) # information about input feature
            fieldNames = { f.name: f.type for f in desc.fields } # dictionary used to keep field names and types together

            if set(useFields).issubset(fieldNames.keys()) : # all fields found in input feature

                fc, c_1 = str(self.params[0].value), 'Forest' # Check field 'Forest'
                self.params[1].filter.list = [str(val) for val in sorted(set(row.getValue(c_1) for row in arcpy.SearchCursor(fc,fields=c_1)))]
                if self.params[1].value not in self.params[1].filter.list:
                    self.params[1].value = self.params[1].filter.list[0]

                fc, c_2 = str(self.params[0].value), 'HarvestArea' # Check field 'HarvestArea'
                if fieldNames['Forest'] == u'String':
                    wc2 = "Forest = '{}'".format(str(self.params[1].value)) # field is string type
                else:
                    wc2 = "Forest = {}".format(str(self.params[1].value)) # field assumed to be a number field
                self.params[2].filter.list = [str(val) for val in sorted(set(row.getValue(c_2) for row in arcpy.SearchCursor(fc,fields=c_2,where_clause=wc2)))]
                if self.params[2].value not in self.params[2].filter.list:
                    self.params[2].value = self.params[2].filter.list[0]

                fc, c_3 = str(self.params[0].value), 'Setting' # Check field 'Setting'
                if fieldNames['HarvestArea'] == u'String':
                    wc3 = "{} AND HarvestArea = '{}'".format(wc2, str(self.params[2].value)) # field is string type
                else:
                    wc3 = "{} AND HarvestArea = {}".format(wc2, str(self.params[2].value)) # field assumed to be a number field
                self.params[3].filter.list = [str(val) for val in sorted(set(row.getValue(c_3) for row in arcpy.SearchCursor(fc,fields=c_3,where_clause=wc3)))]
                if self.params[3].value not in self.params[3].filter.list:
                    self.params[3].value = self.params[3].filter.list[0]

                # output set to completed whereClause
                if fieldNames['Setting'] == u'String':
                    self.params[4].value = "{} AND Setting = '{}'".format(wc3, str(self.params[3].value)) # field is string type
                else:
                    self.params[4].value = "{} AND Setting = {}".format(wc3, str(self.params[3].value)) # field assumed to be a number field

            else: # at least one of the field names does not exist in feature; using the parameter to hold an error message

                if u'Forest' not in fieldNames.keys():
                    self.params[1].value = "ERROR: Field 'Forest' not in selected feature class."
                if u'HarvestArea' not in fieldNames.keys():
                    self.params[2].value = "ERROR: Field 'HarvestArea' not in selected feature class."
                if u'Setting' not in fieldNames.keys():
                    self.params[3].value = "ERROR: Field 'Setting' not in selected feature class."

        return

    def updateMessages(self):

        self.params[0].clearMessage()
        self.params[1].clearMessage()
        self.params[2].clearMessage()
        self.params[3].clearMessage()
        if self.params[0].value is not None: # set error message if field not in feature so user can correct problem

            if self.params[3].value is not None:
                if self.params[3].value.startswith("ERROR:"):
                    # self.params[3].value = None # clear error message in parameter value, if desired
                    self.params[0].setErrorMessage("Input FC '{}' does not contain field '{}'".format(self.params[0].value, 'Setting'))
                    self.params[3].setErrorMessage("Field '{}' is not in Input FC '{}'".format('Setting', self.params[0].value))
                   
            if self.params[2].value is not None:
                if self.params[2].value.startswith("ERROR:"):
                    # self.params[2].value = None # clear error message in parameter value, if desired
                    self.params[0].setErrorMessage("Input FC '{}' does not contain field '{}'".format(self.params[0].value, 'HarvestArea'))
                    self.params[2].setErrorMessage("Field '{}' is not in Input FC '{}'".format('HarvestArea', self.params[0].value))

            if self.params[1].value is not None:
                if self.params[1].value.startswith("ERROR:"):
                    # self.params[1].value = None # clear error message in parameter value, if desired
                    self.params[0].setErrorMessage("Input FC '{}' does not contain field '{}'".format(self.params[0].value, 'Forest'))
                    self.params[1].setErrorMessage("Field '{}' is not in Input FC '{}'".format('Forest', self.params[0].value))
                   
        return
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

For the script tool, I used the following:

import arcpy

inFC = arcpy.GetParameterAsText(0) # input feature class (Input, Data Type 'Feature Layer')
forestVal = arcpy.GetParameterAsText(1) # selected value of Forest field (Input, Data Type 'String')
harvestVal = arcpy.GetParameterAsText(2) # selected value of HarvestArea field (Input, Data Type 'String')
settingVal = arcpy.GetParameterAsText(3) # selected value of Setting field (Input, Data Type 'String')
whereClause = arcpy.GetParameterAsText(4) # set by ToolValidator updateParameters (Output, Derived, Data Type 'String')

arcpy.AddMessage("Where clause used: {}".format(whereClause))
arcpy.SelectLayerByAttribute_management(inFC, "NEW_SELECTION", where_clause=whereClause)

n = arcpy.GetCount_management(inFC)
arcpy.AddMessage("Number of records selected: {}".format(n))‍‍‍‍‍‍‍‍‍‍‍‍‍
AmandaMacDonald-Creevey
New Contributor II

This was just what I was after - I've finally finished the tool that I was trying to create. Thanks so much for your help, much appreciated!

0 Kudos