Pulling information from a table into a parameter in a script tool

4776
16
07-26-2018 09:12 AM
VishalShah2
Occasional Contributor II

Hello,

I am working on putting together a custom tool that helps populate information into four fields within each feature class in the database. The script is pretty straight forward and has no issues. My issue is I have to create 22 tools to do the same thing by each feature class and creating a value list in the parameters section of the script tool would be too cumbersome. Is it possible to have one table that can be automatically pulled right into a drop-down and the only thing I'd have to do is update that table whenever it needs to be updated?

16 Replies
DanPatterson_Retired
MVP Emeritus

Are you referring to using a "value list" for selection? or is it more complicated than that?

Perhaps you could sketchup an example of what you mean

0 Kudos
RandyBurton
MVP Alum

I could see the approach you want as being workable.  I would look at the updateParameters section of the ToolValidator class.  Although it doesn't take your desired approach, this thread on Conditional Drop Down Lists - Tool Validator may give you some ideas in using updateParameters.  Unfortunately, the blog posting mentioned in the first post is no longer available (or not yet available) in the new ESRI blogs.

I imagine you would have your users select the appropriate row in your table based on a value in the first column.  Then other column values in the selected row would populate several of your tool's option selections.

Can you describe how your table would be organized and how you would use the values from it in your tool?

RandyBurton
MVP Alum

Perhaps something like this for the ToolValidator class (script tool, not python toolbox):

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

    fc = r'C:\Path\To\Test.gdb\TableTest' # our table with set-up data
    fld = 'Selection'  # field named 'Selection'
    self.params[0].filter.list = [str(val) for val in sorted(set(row[0] for row in arcpy.da.SearchCursor(fc, fld)))]
    self.params[0].value = self.params[0].filter.list[0] # on init, set to first value in list

    return

  def updateParameters(self):

    if self.params[0].value: # an option has been selected
      fc = r'C:\Path\To\Test.gdb\TableTest' # our table with set-up data
      flds = ( 'Option1', 'Option2', 'Option3','Option4' ) # fields with values we want
      wClause = "Selection = '{}'".format(self.params[0].value) # match field 'Selection'
      values = next(arcpy.da.SearchCursor(fc, flds, where_clause=wClause))
      self.params[1].value = values[0] 
      self.params[2].value = values[1]
      self.params[3].value = values[2]
      self.params[4].value = values[3]         

    return

  def updateMessages(self):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""
    return‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
VishalShah2
Occasional Contributor II

Hi Randy,

I tried to mimic your code but added in my field names in the def updateParameters(self): portion of the script but got an error. The field names in the table that I was to use are AsBuiltSource, AsBuiltCompany, and ConstructionCompany. The other field I need to update is a date field and thus do not need to include that bit in the Validation. As for how the parameters are laid out in my script, they are in this order: AsBuiltSource, AsBuiltCompany, AsBuiltDate, and ConstructionCompany.

How would you set this up in the validation? Sorry about the delay in responding, I was out on vacation for a good bit and then swamped with some work.

0 Kudos
VishalShah2
Occasional Contributor II

The error that I get is that in line 14, the attribute column is not found. I'm assuming because there is no field with that name. What would I substitute 'Selection' out with?

0 Kudos
RandyBurton
MVP Alum

Hi Vishal,

It is possible that I don't completely understand your work flow and what you want to accomplish.  What I pictured was a table with a row of options.  The tool would query the table and populate a dropdown with a list of options.  When an option was selected, other values from that row would populate other sections of the tool.

Here's a simplified version of the table I had pictured:

Sample table

Here's what the tool would look like that would access the table:

Tool example

When the tool is initialized, the Selection box is populated with the contents of the table's Selection column, with the first row's data being the default selection.  The options associated with the Selection are displayed in the tools other boxes.  For example, when "Three" is made as the selection, the other option boxes in the tool would be populated with C1...C4.

If this is not what you had in mind, could you provide some examples of the workflow and table layout you were envisioning?  Thanks.

VishalShah2
Occasional Contributor II

Randy,

I do like how you actually used the Selection field to auto populate the other 4 parameters. Using that logic, do those 4 parameters have to be visible then? I would assume that if they get auto populated from a table, then that should be the correct input and the user wouldn't have to make any corrections. So something like this

User selects One in the Selection parameter and then the other 4 parameters (not visible to the user), get populated with A1, A2, A3, and A4 respectively? And then used in the script tool.

As for my workflow that I originally intended for, there would only be 4 parameters. So the tool window will look like this:

Then there would be the following table in the database:

The idea is to get the tool (using validation) to link the table and the 3 fields (AsBuiltType, AsBuilt Company, and ConstCompany; AsBuiltDate will be a manual entry) to be linked to Document Type, Document Company, and Construction Company. So this is the order they'd be paired (field name - Parameter): AsBuiltType - Document Type, AsBuiltCompany - Document Company, and ConstCompany - Construction Company. The idea is that the drop down menu (value list) will get populated with the values in that field. This is so that when a new company's name is added to the table, the drop down menu doesn't have to be manually updated, it'll get auto populated through tool validation. Is this possible?

Just a disclaimer, I have never worked with tool validation so I'm new to it and any insight is greatly appreciated.

0 Kudos
RandyBurton
MVP Alum

Thanks for the additional description of your project. To see if I am understanding correctly…

Regarding your question about making the other 4 parameters hidden. I think there is an option for the parameters to be hidden. Showing them could help the user be certain the correct ‘selection’ was made, but it does take space on the tool form.

Regarding the date, Document Creation Date would be a manual entry. So it would not depend on any selection. Would you want it to default to today’s date, the AsBuiltDate, or be left blank?

I am still a bit fuzzy about the selection part. Would the user be presented with ‘Option 1’, ‘Option 2’, etc? If they select ‘Option 1’, then they would get AsBuiltType = Type1, AsBuiltCompany = Company1, and ConstCompany = Company1. I am assuming that whatever ‘Option 1’ is, it would be descriptive enough for your user to choose the best one.

And once a selection is made and a date entered, these parameters will be used to populate a new in a feature class or another table?

0 Kudos
VishalShah2
Occasional Contributor II

The Document Creation Date would be left blank for the user to input the date that can be found on the document they have.

I don't think they'd have a selection. The idea is to point each parameter to a field in the table. So with Document Type parameter pointing to the AsBuildType field, when the user hit's the drop down menu, they'd be presented with the options of Type 1, Type 2, and Type 3. That's because those are the only options. But say the table has over a 1,000 entries with a total of 5 different types. That's pretty easy to manage. AsBuiltCompany could be in the 100s for an option. I'd like for the drop down list for Document Company to auto populate with the values within the AsBuiltCompany field.

Once all the parameters are entered and the user hits Ok, these will populate the appropriate fields within each feature class (10 to be exact right now, may grow).

Hopefully that helped clarify any confusions or questions you may have had.

0 Kudos