Assigning field name based on user input

521
3
01-22-2018 06:51 AM
TomPiggott
New Contributor II

Hi,

I am trying to create a model where I add multiple new fields to a feature class and to do some other geoprocessing tasks. Is there a way to choose what the field names will be based on user input? I have one field that I add to the feature class which depends on what the data represents and I was wondering if there is a way where I can when running the model select one of two options to what the field should be called.  I also wanted to do it so that I could select whether it will add the WAperiod field. I am pretty new to this so I can imagine its something very simple

0 Kudos
3 Replies
DanPatterson_Retired
MVP Emeritus

As for providing options as to which field name to choose, you can check the 'branching' section of modelbuilder to see how 'if-else' conditions are implemented.  

0 Kudos
curtvprice
MVP Esteemed Contributor

It's pretty easy to create a string parameter value filter to set the choices to a limited list of strings. When the model tool is run, it will present a picklist of keywords (which can be the same as the list of field names you want).

Value list filter—Help | ArcGIS Desktop 

Model parameters—ArcGIS Pro | ArcGIS Desktop 

If you want the model to figure out the name without the user having to know, branching your model (as Dan suggests) is an option, but I think it's easier to use Calculate Value to calculate your field name. Either approach will require some scripting.

# Calculate Value Expression
GetFieldName(r"%table%")
# Code Block
def GetFieldName(tbl):
  # use field name to figure out what sort of table, 
  # if KEYFIELD is not there, returns empty (false)
  # and field name is assigned "FIELDNAME2"
  if arcpy.ListFields(tbl, "KEYFIELD"):
    fname = "FIELDNAME1"
  else:
    fname = "FIELDNAME2"
  return fname
# Data Type
String‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
TomPiggott
New Contributor II

Brilliant! thanks for the help guys 

0 Kudos