Using self.params[] in ToolValidator

154
0
05-14-2020 03:59 PM
AndrewLaws
New Contributor

I'm creating a script tool for ArcMap that will update a table from a file geodatabase. I'm having the first parameter (self.params[0]) autopopulate a dropdown. There may be multiple of the same entry in the first parameter. Therefore, in the  def updateParemeters I've been unable to get the self.params[1] list to become the entries in the field that have the self.params[0]. I've tried SelectLayersbyAttribute and SearchCursors and can't figure it out. Thank you for your help!

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()

  def initializeParameters(self):
    """Refine the properties of a tool's parameters.  This method is
    called when the tool is opened."""
    clients = "C:\geodata\Brush_Management_Plan.gdb\ClientInfo"
    rows = arcpy.SearchCursor(clients)
    self.params[0].filter.list = sorted(list(set(c.getValue('Client') for c in rows)))
    del rows
    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].altered:
  	table = "C:\geodata\Brush_Management_Plan.gdb\ClientInfo"
	client_field = 'Client'
	contract_field = 'Contract_Num'
	expression = u'{} = 2'.format(arcpy.AddFieldDelimiters(table, client_field))
	with arcpy.da.SearchCursor(table, [client_field, contract_field],
                           where_clause=expression) as cursor:
	    for row in cursor:
	        self.params[1].filter.list = sorted(list(set(row[1])))
    return

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