Create Script Tool with Sortable Field Multi-Value Input

2531
5
Jump to solution
03-21-2018 11:00 AM
JohnStreeb
Occasional Contributor

I want to create a script tool in a regular ArcMap toolbox (not python tool box if possible) and have a multi-value field input parameter that allows you to sort the input fields, just like the "Attributes of Interest" input in the "Similarity Search" GP Tool in the built-in Spatial Stats Tools toolbox.  I can get a regular multi-value field input, but that only gives me all of the fields (that meet the filter requirements) in a pre-populated list, with check-boxes.  What my script tool needs however, is to get a list of field names from the user in a user specified order.  

The "Similarity Search" GP tool has 2 inputs at the end of that tool that do exactly what I am wanting, and I've compared their settings with mine and I see nothing different, but they have a sortable multi-field input.  Additionally, if I copy that script tool into my own toolbox and delete the entire validation code section (code on the Validation tab of the script tool properties dialog), when I re-open that tool it still has the sortable multi-field input desired, so it does not appear to be validation code specific.  However, if I change any of the parameter display names in that script tool's properties, it suddenly reverts to the regular multi-field select box.

So, my question is, how can I get my script tool to replicate the Similarity Search GP tool's multi-field input?  Any thoughts or help is welcome!

Thanks

I'd ideally like:

Instead of 

1 Solution

Accepted Solutions
JohnStreeb
Occasional Contributor

So, after a decent bit of researching, testing, and troubleshooting... I was not able to figure out any way to get a multi-value field type input parameter for a script tool in ArcMap/Catalog to allow the fields to be added in a user specified order and/or sorted as desired.  However, I did finally discover an annoyingly simple work-around, so I'm sharing in case others find it useful.  So, here are the steps.

  1. Create the script tool with the default multi-value field input that can't be sorted.
  2. Create a model with model builder (I recommend putting the model in the same toolbox as the script tool and setting it to use relative paths).
  3. Then, in model builder, go to Insert > Create Variable. In the dialog box select the "Field" data type, and make sure to check the box for "Multivalue". Click OK.  If you open this variable, it should have the desired style that allows you to manually add fields in a desired order and sort them.
  4. Add the script tool to the model and use the "connect" tool in model builder to add the newly created model-builder variable as the input to the script tool's multi-value field type input parameter.
  5. Next right click on the script tool and make a variable from any of the other parameters the script tool may have. 
  6. Finally rename any of these variables as desired and right click all of them and mark them as Model Parameters. Save and close.

Essentially, for some reason the multi-value field type input parameter used for a script tool and the multi-value field type input parameter used for a model variable are 2 completely different input styles, with the model builder version allowing the desired functionality.  So you have to create the variable in model builder and use that as the script's input (no, just adding the script tool to model builder and making a variable from the script tool's parameter will not solve it).  Basically you make a model builder wrapper for your script tool, and you can get a sortable multi-value field type input parameter.

Or as noted in another comment, if you are able to, you can switch to ArcGIS Pro, which allows this functionality by default.

Hope it helps someone.

View solution in original post

5 Replies
RandyBurton
MVP Alum

There is some discussion about this in the thread: Conditional Drop Down Lists - Tool Validator.   Specifically mentioned is this blog Generating a choice list from a field which you should check out as I think it basically does what you want.  Specifically, examine the Tool Validator code discussed in the blog.

A second blog article that may also be of interest: Generating a multivalue choice list

JohnStreeb
Occasional Contributor

Maybe I'm missing something on one of the linked posts, but those look to be talking about how to provide a custom sorted list to the user to pick from, those make sense.  What I'm stuck on is the fact that I need to user to provide field names in an order they specify.  Behind the scenes, the tool is going to take the fields, in the order the user specifies, and do some field value manipulation, including concatenating the values of all of the provided fields, and then compare it to some other data and create an output list of records that don't match.  So I don't need help getting a list of field names to present to the user, I can figure that out if I have to use a text field input instead of a field input type for example.  But I haven't figured out how to get it so the user can provide an multi-value input (as I don't know how many fields they will input) and let the user specify the order the values get added in (as show in the screen clipping below from one of ESRI's script tools, where it gives users a drop down to add field names one at a time and lets them sort with up-down arrows even, rather than just presenting them a list of field names as check-boxes in an order I have to specify.

0 Kudos
DanPatterson_Retired
MVP Emeritus

multivalue inputs... the default now in PRO is to show them in alphabetical order.  Fortunately, if you select one, then you have the option to add the others in the order that you want.

I can't remember if the behaviour was the same as in ArcMap, but the new behaviour is useful if you want either alphabetical or user-selected order.  

Keep this in mind should you be moving to PRO soon.

0 Kudos
JohnStreeb
Occasional Contributor

Unfortunately, I have not figured out how to get the behavior to be the same in ArcMap, which I am stuck using for the immediately foreseeable future (I expect at least a year or two still, due to 3rd party tools, and waiting on an ArcPro equivalent of the Attribute Assistant...).  But that is definitely good to see that at least the ArcPro behavior allowS for custom input order! Thank you for the information, and maybe one day when we get to switch our main production environment to Pro I will be able to re-do these tools using this functionality.  Until then, hopefully I can still figure out something that works in ArcMap.

0 Kudos
JohnStreeb
Occasional Contributor

So, after a decent bit of researching, testing, and troubleshooting... I was not able to figure out any way to get a multi-value field type input parameter for a script tool in ArcMap/Catalog to allow the fields to be added in a user specified order and/or sorted as desired.  However, I did finally discover an annoyingly simple work-around, so I'm sharing in case others find it useful.  So, here are the steps.

  1. Create the script tool with the default multi-value field input that can't be sorted.
  2. Create a model with model builder (I recommend putting the model in the same toolbox as the script tool and setting it to use relative paths).
  3. Then, in model builder, go to Insert > Create Variable. In the dialog box select the "Field" data type, and make sure to check the box for "Multivalue". Click OK.  If you open this variable, it should have the desired style that allows you to manually add fields in a desired order and sort them.
  4. Add the script tool to the model and use the "connect" tool in model builder to add the newly created model-builder variable as the input to the script tool's multi-value field type input parameter.
  5. Next right click on the script tool and make a variable from any of the other parameters the script tool may have. 
  6. Finally rename any of these variables as desired and right click all of them and mark them as Model Parameters. Save and close.

Essentially, for some reason the multi-value field type input parameter used for a script tool and the multi-value field type input parameter used for a model variable are 2 completely different input styles, with the model builder version allowing the desired functionality.  So you have to create the variable in model builder and use that as the script's input (no, just adding the script tool to model builder and making a variable from the script tool's parameter will not solve it).  Basically you make a model builder wrapper for your script tool, and you can get a sortable multi-value field type input parameter.

Or as noted in another comment, if you are able to, you can switch to ArcGIS Pro, which allows this functionality by default.

Hope it helps someone.