Why doesn't the Field Mappings control show the Add Input Field in the context menu of a Python Toolbox?

334
0
09-17-2019 07:54 AM
BogdanMorosanu1
New Contributor II

I’m trying to build a Python Toolbox which uses a Field Mappings control (GPFieldMapping parameter). For some reason I don’t manage to get the same behaviour of the Field Mappings control as in the standard geoprocessing tools: Append, Merge …. On my tool, the context menu doesn’t show the Add Input Field command as it does in the Append tool (see print screen below). Any clues of why this might happen?

field maapings control context menu

Please find below my code:

import arcpy

class Toolbox(object):
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = "Toolbox"
        self.alias = ""

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


class TestFieldMappings(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Test Field Mappings"
        self.description = ""
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""
        prm_base_feature = arcpy.Parameter(displayName = 'Base Feature Class',
                                           name = 'BaseFeatureClass',
                                           datatype = 'DETable',
                                           parameterType = 'Required',
                                           direction = 'Input')

        prm_test_feature = arcpy.Parameter(displayName = 'Test Feature Class',
                                           name = 'TestFeatureClass',
                                           datatype = 'DETable',
                                           parameterType = 'Required',
                                           direction = 'Input')

        prm_field_mappings = arcpy.Parameter(displayName = 'Field Mappings',
                                              name = 'FieldMappings',
                                              datatype = 'GPFieldMapping',
                                              parameterType = 'Optional',
                                              direction = 'Input')


        params = [prm_base_feature,
                  prm_test_feature,
                  prm_field_mappings]        
        # return parameters array          
        return params

    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."""

        if parameters[0].altered and parameters[1].altered:
            if not (parameters[0].hasBeenValidated and parameters[1].hasBeenValidated):

                tmp_fms = arcpy.FieldMappings()  # Create empty FieldMappings object

                base_fields = [f for f in arcpy.ListFields(parameters[0].valueAsText) \
                                if f.type not in ['Geometry', 'Raster']]
                test_fields = [f for f in arcpy.ListFields(parameters[1].valueAsText)
                                if f.type not in ['Geometry', 'Raster']]            

                # loop base fields                
                for f in base_fields:
                    tmp_fm = arcpy.FieldMap()
                    tmp_fm.outputField = f

                    # loop test fields
                    for g in test_fields:

                        # if name matches, add the input field
                        if f.name == g.name:
                            tmp_fm.addInputField(parameters[1].valueAsText, g.name)

                    # add the field map
                    tmp_fms.addFieldMap(tmp_fm)
                parameters[2].value = tmp_fms.exportToString() 

        return

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

    def execute(self, parameters, messages):
        """The source code of the tool."""
        return

Notes: I managed to make the Add Input Field show on my tool by using the initializing script of the Field Mappings in the getParameterInfo routine. This is of no use as I want the Field Mappings to be updated each time the input is changed

0 Kudos
0 Replies