Field mappings data type parameter in Geoprocessing Tool

1345
8
10-28-2019 10:47 AM
deleted-user-NvcfpBOWaKwr
Occasional Contributor

I have a script that I'm trying to migrate over to an ArcGIS pro toolbox as a script tool. The script is using the fieldmappings python object however I would like to use the Field Mappings Datatype within the script tool's parameters to control the field mapping. ( I have substituted the fieldmappings python object for the one within the tool's parameters.)

The issue I'm having is that only the input tables show up within the field mapping section for the tool. None of the fields show up for the output table. Just to reiterate, It does not give me the option to choose which fields in table 1 match  fields in table 2. 

I have not been able to find any good documentation on how to correctly use the Field Mapping datatype. Does anyone have a working example of how to set this up correctly? 

8 Replies
DanPatterson_Retired
MVP Emeritus

Fields and Field mapping need to specify a dependency in order to see the field lists.

A pic might be best

In this example, Input features are a parameter as are the fields to sort.  In order to get the list of fields to choose from in the tool, a Dependency for the sort fields had to be the input features (feature class)

0 Kudos
deleted-user-NvcfpBOWaKwr
Occasional Contributor

Thanks for getting back to me Dan. I have the dependencies set. So whats happening, I've included pictures, the fields for the output table are showing up as they should be. However in an attempt to reconcile sources by clicking the source tab you should be able to see the field from the other table to allow you to match. It's only showing the output fields again. 

It should look like this (From the Append Tool)

Here are my parameters for the script tool

I can't seem to figure out how to set the correct table for use in the source tab. 

DanPatterson_Retired
MVP Emeritus

line 5 in the last dialog. output field, derived.  If memory serves derived parameters don't show

Setting script tool parameters—Geoprocessing and Python | ArcGIS Desktop 

0 Kudos
vtey_esriau
New Contributor III

Still no solution to this? Do you need to use the validation to set the initial parameter value.

0 Kudos
RossVolkwein1
New Contributor III

Also looking for an answer to this, what's the right path, validation or in the properties?

0 Kudos
MattHowe
Occasional Contributor

I'm also finding it difficult to find a way of listing the source fields in the field map parameter. The target fields are fairly straight forward but not clear how to be able to do the actual mapping.

 

vtey_esriau
New Contributor III

Sharing what works for me.

def getConfig(self):
        mapping = {
                "fieldMap": {
                    "xxx":{
                        "Name": "xxx",
                        "transformation":None #Transformation isn't used for testing
                    },
                    "xxx":{
                        "Name":"xxx",
                        "transformation":"addHahaToTest"
                    },
                    "xxx":{
                        "Name":"xxx",
                        "transformation":None
                    }
                }
            }
        return mapping
    

    def updateParameters(self):
        # Modify parameter values and properties.
        # This gets called each time a parameter is modified, before 
        # standard validation.
        #See https://gis.stackexchange.com/questions/324510/creating-field-mapping-parameter-in-arcpy-script for listfield example if required.
        #inputTable = self.params[1].value #default how pro does it
        #self.params[2].value = str('Empty')  #default how pro does it
        #self.params[2].value.addTable(inputTable)  #default how pro does it
        if self.params[0].altered:
            inputTable = self.params[0].value
            fms_param = self.params[1]
          
            fms_param.value = str('Empty')
            
            fms_param.value.addTable(inputTable) #this is the only way i can get to populate the drop down
            initialFieldCount = fms_param.value.fieldCount
            self.params[2].value = initialFieldCount
           
            inputField_names = [field.name for field in arcpy.ListFields(inputTable)] #list comprehension
                        
            config = self.getConfig()
            
            firstKey = None
            for key,value in config["fieldMap"].items():        
                
                blockNumber_fm = arcpy.FieldMap()
                default_input_field = value['Name'] if value['Name'] in inputField_names else inputField_names[0]
                blockNumber_fm.addInputField(inputTable, default_input_field)            
                blockNumber_name = blockNumber_fm.outputField
                blockNumber_name.name = key #this can be set to anything.
                blockNumber_fm.outputField = blockNumber_name                                            
                fms_param.value.addFieldMap(blockNumber_fm)
                                
                if firstKey == None:
                    firstKey = key              
            for i in range(initialFieldCount):
                fms_param.value.removeFieldMap(0)  
            
        return
0 Kudos
Rohanrajan
New Contributor

Is this solved? Facing the same problem in ArcMap append tool too.

0 Kudos