Update Parameters in Toolbox after refreshing selection

298
5
03-04-2024 08:56 AM
BrennanSmith1
New Contributor III

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.

0 Kudos
5 Replies
AlexanderDanielPratama
Esri Contributor

Hi, is it possible to share your script in the validation tool?

This is my dummy script

In the parameters, I have this configuration

AlexanderDanielPratama_0-1709699339872.png

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. 

AlexanderDanielPratama_1-1709699382286.png

Last is running the script. The filtered_value will show the value from the parameter feature layer that has been selected in my case.

AlexanderDanielPratama_3-1709699645920.png

Hope it helps you,

Cheers

 

 

 

 

0 Kudos
BrennanSmith1
New Contributor III

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.  

BrennanSmith1_0-1709731863801.png

 

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

 

0 Kudos
BrennanSmith1
New Contributor III

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

  • Add a new GPBoolean parameter, named something like "Click here to force update selection"
  • Compare the ID value to its own filter list, and update if necessary
    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
        

 

AlfredBaldenweck
MVP Regular Contributor

I would vote for an idea to make that refresh button call updateParameters().

0 Kudos
BrennanSmith1
New Contributor III
0 Kudos