I need to define a required field parameter, and I'd like it to work with both existing and new fields, i.e. regardless of whether the field already exists.

The problem is, if I enter a new (non-existing field), I get error 000728: Field ... does not exist within table.

This must be due to the parameterDependencies property, but without it I would not get a dropdown allowing to select an existing field. The direction parameter does not seem to matter (Input or Output).

Basically, I'd like to be able to use the drop-down field selection when the field exists, and enter the name directly for a new field. This sounds pretty basic, I must be missing something simple. Could this be done?

Here is a sample *.pyt code to illustrate what I am trying to do.

import arcpy

class Toolbox(object):
    def __init__(self):
        self.label = "[Test] Select existing or new field"

        # List of tool classes associated with this toolbox
        self.tools = [Test_SelectExistingOrNewField]

class Test_SelectExistingOrNewField(object):
    def __init__(self):
        self.label = "Select existing or new field"
        self.description = ""

    def getParameterInfo(self):
        # Feature class where an existing field resides, or a new field will be added to
        p0 = arcpy.Parameter(
                        displayName   = "Input feature class",
                        name          = "fc",
                        datatype      = "DEFeatureClass",
                        parameterType = "Required",
                        direction     = "Input")

        # Name of the output field in the `normals` feature class which will contain the final (conflated) Z values.
        p1 = arcpy.Parameter(
                        displayName   = "Existing or new field name in the input feature class",
                        name          = "fld",
                        datatype      = "Field",
                        parameterType = "Required",
                        direction     = "Output")
        p1.parameterDependencies = [p0.name]
        p1.value = 'AField'

        return [p0, p1]

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""

    def execute(self, parameters, messages):
  • Make two parameters, one for existing, one for new, and then just handle it in the code – Midavalo May 13 '16 at 22:27
  • I think that would work, but would be quite confusing for users to have 2 parameters for the same purpose. I am hoping there may be a simpler and more elegant way ... – A. Naumov May 16 '16 at 15:44
  • If you include a test Python Toolbox (*.pyt) rather than just this small part of it then you may attract a potential answerer to give it a test and experiment with modifications. – PolyGeo May 17 '16 at 9:42
  • Thanks for the suggestion, I modified the post to include a complete *.pyt code. – A. Naumov May 26 '16 at 17:04

Similar response to Midavalo on the 13th May except add a Boolean parameter so the user can select an existing field or enter a new field. i.e. Add two additional input parameters

  1. Boolean parameter to process an existing field from an input feature class or a new field
  2. New field

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.