Script tool with drop down list of multi values using ToolValidator

469
13
Jump to solution
10-14-2021 06:43 PM
Aнастасия0328
New Contributor II

Hello,

I need an idea on how to enable a drop down list in a script tool so that I can select multiple attribute values from a selected field.

Based on a code from ArcGIS blog, "Generating a choice list from a field", I managed to create a script tool with ToolValidator function to make a a drop down list.

A0328_0-1634261251130.png

But I cannot select multiple values with this code only. I can check the box of "Multiple values" in Parameters section. This makes multiple values windows but even if I select multiple values, the values won't be included as inputs. So, I feel the script in validation section needs to be modified but I am struggling with that.

Thanks in advance.

 

0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

I am not sure the purpose of the last 2 lines (25, 26.  If you are providing the list of values to the tool from the field would that be in the initialize script?

Also if you are using a search cursor getting one value at a time,  there is no need to take a set of the returned value and sort it each time.  Get all the values, take its set, convert it to a list, then sort it

d = [2, 5, 5, 2, 6, 1, 3, 5, 4]  # emulate a searchcursor
# use a set comprehension
out = sorted(list({str(val) for val in d}))
out
['1', '2', '3', '4', '5', '6']

Beyond that, I can't pinpoint anything else


... sort of retired...

View solution in original post

0 Kudos
13 Replies
DanPatterson
MVP Esteemed Contributor

There are discussions here

Solved: Create Script Tool with Sortable Field Multi-Value... - Esri Community

and here

Solved: Using script tool parameters to update a multiple ... - Esri Community

My preference is to use "Custom tools" and do minor validation within the validation code contained in the tool.  You get a better link (without coding) and a visual representation of the dialog parameters that isn't available in "python tools" ... kind of a misnomer since both of them are "python" at their root in the new toolbox structure introduces in Pro 2.8 and evident in beta 2.9.

See the help topics within this general area

Comparing custom and Python toolboxes—ArcGIS Pro | Documentation


... sort of retired...
0 Kudos
Aнастасия0328
New Contributor II

Hi Dan, thanks for your advice.

I had already created the script tool in the custom toolbox and applied the validation code that I found it from the blog. But this way does not give me multiple values selection from the selected field unfortunately.

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

the blog was from 2011 and for ArcMap and the code link was no good, so I can only guess.

The parameter would have to be multivalue

Working with multivalue inputs—ArcGIS Pro | Documentation

so that would have to be set up in your script as either a list or a semicolon separated string


... sort of retired...
0 Kudos
Aнастасия0328
New Contributor II

Thanks for your help again.

I tick a box for Multiple Values in parameter. The data type is String. I think prepared a script to take the input as multiple values but it still does not work. 

My script for the multivalue input is below:

BName = arcpy.GetParameterAsText(2)
BName_list = BName.split(";")

0 Kudos
DanPatterson
MVP Esteemed Contributor

Add an

arcpy.AddMessage(BName_list)

to see what it returns.  It should return a list of strings if it is semicolon delimited


... sort of retired...
0 Kudos
Aнастасия0328
New Contributor II

My script is:

InputFeatureClass = arcpy.GetParameterAsText(0)
InputField = arcpy.GetParameterAsText(1)
InputValue = arcpy.GetParameterAsText(2)
InputValues = InputValue.replace("'","")
InputValue_list = InputValues.split(";")
arcpy.AddMessage("Input Values: {0}".format(InputValue_list))

My parameters - data type is string and ticked Multiple Values:

A0328_0-1634601270611.png

Interface of the script tool allows me to select multiple values:

A0328_1-1634601346380.png

But even if I added the checked values, input ends up like this - it won't be multiple value for some reason.

A0328_2-1634601455589.png

 

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

As I indicated earlier, I prefer custom toolboxes.

Here is on of my tools that accepts multiple values... to get a complete lists of possible selections, I just click on the little arrow beside the input parameter name (class fields in the case) and you end up with a list of checkboxes so you don't have to select one at a time.

multivalue_parameter.png

try clicking on your arrow beside "Value"


... sort of retired...
0 Kudos
Aнастасия0328
New Contributor II

I think I created a script tool in same steps that are described here - "Creating a script tools in a custom tool box".

Created like this below

A0328_0-1634613742120.png

Do you mean this is not a "custom toolbox" that you mentioned?

As you can see the second picture above, I did open the drop -down list of values and checked multiple values and clicked "Add". However, it ends up with like the third picture, which is only one value is selected and I cannot add and change it. 

I don't know what I am missing...

0 Kudos
DanPatterson
MVP Esteemed Contributor

The multivalue selection returns a ";" delimited list and this is how I split it in the code

cls_flds = cls_flds.split(";")

multivalue_parameter01.png

There is no extra validation code, just what you see.  in my case "Field" was the case with multiple values desired


... sort of retired...
0 Kudos