Defining parameters in a Python toolbox for "FieldMapping"

601
2
02-15-2013 10:11 AM
BillMiller
New Contributor
How do you add FieldMapping to a Python toolbox?

Here is the webpage for Defining parameters in a Python toolbox.
   --- Defining parameters in a Python toolbox ---
   http://resources.arcgis.com/en/help/main/10.1/index.html#/Defining_parameters_in_a_Python_toolbox/00...

Below is how ESRI has documented the parameter for "GEFeatureLayer"
Note: I had to type this as "FeatureLayer" to make it work.

def getParameterInfo(self):
    param0 = arcpy.Parameter(
        displayName="Input Features",
        name="in_features",
        datatype="GPFeatureLayer",
        parameterType="Required",
        direction="Input")


Now I've tried using the same structure to create a parameter for "FieldMapping" which is documented on the following webpage:
   ---- "Defining parameter data types in a Python toolbox" ---
   http://resources.arcgis.com/en/help/main/10.1/index.html#/Defining_parameter_data_types_in_a_Python_...

def getParameterInfo(self):
    param0 = arcpy.Parameter(
        displayName="Field Mapping",
        name="in_mapping",
        datatype="GPFieldMapping",
        parameterType="Required",
        direction="Input")

This just gives me a text box. I've also tried: "FieldMapping", "FieldMappings", and a few others. Nothing works.
Even if I have the correct datatype it's not clear how to assign the field mapping to the parameter.

        fms = arcpy.FieldMappings()

I would think it should be something like the ValueList but I cannot find any documentation on this.
Tags (2)
0 Kudos
2 Replies
NeilDuxbury
New Contributor II
Bill,

Which version of ArcGIS are you using? I think the values for datatype are different between 10.1 and 10.1 SP1 and later. I'm using 10.1 and it is working with a data type of "Field Mappings".

I've added code to the updateParameters method to populate the field mapping parameter. The last step of this is to use the exportToString method of the FieldMappings object and set the resulting string as the parameter's value, e.g.

fieldMappings = arcpy.FieldMappings()
# code to populate fieldMappings
parameters[0].value = fieldMappings.exportToString()


Neil
0 Kudos
BillMiller
New Contributor
Hey thanks for the reply.

This is an old post about 1 year ago and I have completely given up on doing fieldmapping in a python toolbox ".pyt". It's just not worth the effort even if I could get it to work.

The problem did not have anything to do with getting the results as you suggest. It was that ArcGIS-10.1 was not accepting the documented keyword for fieldmapping. I really don't know why I couldn't get fieldmapping to work but it could have been that datatype needed a space. Maybe (datatype="Field Mapping") instead of "FieldMapping".

I also don't understand why Esri documents datatype with a GP prefix in some examples. This GP prefix has never worked for me.
example: datatype="GPFieldMapping"

I will suggest that anyone reading this look at the free online course which helped me understand the Python toolboxes much better.
"Creating Python Toolboxes Using ArcGIS 10.1"


PS: I re-read your post and maybe you're right about how to set that. I just don't know.
0 Kudos