I have a python toolbox with a 'GPFeatureLayer' parameter, and a 'GPString' parameter. The string field is a dropdown populated with the unique values of an ID field in my feature layer. If there is a selection on the feature layer, the dropdown value list is correctly populated using just that selection.
If the user updates their selection, the list does not update. Clicking the refresh arrows next to "The input has a selection. Records to be processed: X" does not call updateParameters(). The user must select a new feature layer and the reselect the old feature layer to force the update, or close the tool and reopen it. This is not the desired workflow.
Is there any way to force a parameter update when refreshing the selection? Can ESRI staff perhaps help me find the clearest language to make a new Idea post to ask for this functionality? Something like Geoprocessing Tool Selection Refresh should call updateParameters() in python toolbox. Thanks.
Hi, is it possible to share your script in the validation tool?
This is my dummy script
In the parameters, I have this configuration
Then in the validation, I insert this script. The key is in the conditional of params[0] and has a value. Then, by using arcpy.da.SearchCursor to iterate a column that we desired. Don't forget to put the iteration value to an empty list that has been defined before.
Last is running the script. The filtered_value will show the value from the parameter feature layer that has been selected in my case.
Hope it helps you,
Cheers
My dropdown list is populating correctly. The issue is that if the selection changes while the tool is open, clicking the refresh arrows does not call updateParameters() and does not update the dropdown. Below is a screenshot, and an example .pyt script.
import arcpy
class Toolbox(object):
def __init__(self):
self.label = "Toolbox"
self.alias = "toolbox"
self.tools = [Tool]
class Tool(object):
def __init__(self):
self.label = "Tool"
self.description = ""
self.canRunInBackground = False
def getParameterInfo(self):
# Feauture Layer
fc = arcpy.Parameter(
displayName="Define Feature Layer",
name="fc",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input")
# Drop down of OIDs from feature layer
oids = arcpy.Parameter(
displayName="Select OID",
name="oids",
datatype="GPString",
parameterType="Required",
direction="Input")
params = [fc,oids]
return params
def updateParameters(self, parameters):
#if feature layer is defined
if parameters[0].altered and not parameters[0].hasBeenValidated:
#get list of OIDs
oidfield = arcpy.Describe(parameters[0].value).OIDFieldName
oidlist = [row[0] for row in arcpy.da.SearchCursor(parameters[0].value, oidfield)]
#and populate the dropdown
parameters[1].filter.list = oidlist
parameters[1].value = oidlist[0]
return
def execute(self, parameters, messages):
return
I was able to craft a workaround. I still think that clicking the refresh icon should call updateParameters(), but for anyone else encountering this issue in the meantime
def updateParameters(self, parameters):
#get list of OIDs
oidfield = arcpy.Describe(parameters[0].value).OIDFieldName
oidlist = [row[0] for row in arcpy.da.SearchCursor(parameters[0].value, oidfield)]
#whenever feature layer is defined, populate list of IDs
if parameters[0].altered and not parameters[0].hasBeenValidated:
#and populate the dropdown
parameters[1].filter.list = oidlist
parameters[1].value = oidlist[0]
#make sure that list of IDs is always up to date, and replace any bad values
if parameters[1].value not in oidlist:
parameters[1].filter.list = oidlist
if parameters[1].value not in parameters[1].filter.list:
parameters[1].value = parameters[1].filter.list[0]
return
I would vote for an idea to make that refresh button call updateParameters().
I created the Idea here, thank you.