Access field type of variable to pass to Add field in Model Builder

3709
4
08-24-2015 02:33 PM
AWuenschel
New Contributor II

I am creating a user tool in model builder. I want to let the user select from a list of fields, what ID field should be for the analysis (here after called 'User Defined ID field'). I then want to add a field to an intermediate file, use Calculate Field to populate it with the same values from the 'User Defined ID field', and then reference it in later Dissolve operations. The issue is that when I use the Add Field tool in Model Builder I have to enter the field type (Text, Boolean, long, etc.) and that will be contingent upon the type of field that the 'User Defined ID field' is. I could have the Field Type be a model parameter but there are enough things for the user to enter as is. Is there a way I could pass the 'Field Type' from the Field Type of 'User Defined ID field'? Thanks in advance!

Ami

0 Kudos
4 Replies
DanPatterson_Retired
MVP Emeritus

ID fields are integer (long or short depending on the range....not necessarily specific to ArcMap, but describing the range of possible values)

0 Kudos
AWuenschel
New Contributor II

Thanks. I guess I meant ID fields in the more generic sense. I just meant a field that identifies the analysis unit. For instance, one possibility would be to use watersheds, and it may be more informative for the user to use the watershed name than an integer ID. The data output the user gets will be summarized by the 'ID' they use.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Easy in scripts...there must be some way within modelbuilder to access a selected field type from the environment variables as suggested in this link which refers to a standalone parameter in modelbuilder

Data types for geoprocessing tool parameters—Help | ArcGIS for Desktop

the help is for 10.3 since the 10.2 files are having 'issues'

0 Kudos
curtvprice
MVP Esteemed Contributor

The Calculate Value tool can be used to get the field type. You need to write a little python function.

expression:

GetFieldType(r"%Input table%", r"%Input field%")

code block:

import arcpy
def GetFieldType(tbl, field):
  flds = arcpy.ListFields(tbl, field)
  ftype = flds[0].type
  return ftype

You can then connect the output of the Calculate Value tool to the data type input for Add Field.

Unfortunately the above code will return "Single" instead of "LONG" - I am leaving it to you to map the field types to the keywords for Add Field.

You will also have to build to appropriate expression for Calculate Field to copy the data over. This is most easily done using another Calculate Value tool.

0 Kudos