ArcPy Script: Field Mappings Parameter and Object in Python Toolbox help

3850
2
07-28-2014 08:20 AM
BrianLai1
New Contributor

So I'm trying to create a Field Mappings input parameter for the ArcPy Spatial Join Analysis tool, but it spits up an error.  When I try to create a Field Mapping object in the execute portion of my test script, I can successfully add Field Maps objects to the Field Mappings object.  Below is an example of the error I get when trying to update the fields in updateParameters():

ValueError: FieldMappings:

AddFieldMap input not field map object

###################################################

This is the code:

def getParameterInfo(self):

  targetfeatures= arcpy.Parameter(

       displayName = "",

       name = "",

       datatype = "Feature Class",

       parameterType = "Required",

       direction = "Input")

 

  fieldmapping= arcpy.Parameter(

       displayName = "",

       name = "",

       datatype = "Field Mappings",

       parameterType = "Required",

       direction = "Input")

  fieldmapping.value = str('Empty')                #If I don't include this line, I get an error: AttributeError: 'NoneType' object has no attribute 'addFieldMap'

  parameters = [targetfeatures, fieldmapping]

  return parameters

def updateParameters(self, parameters): #optional

  joinFeatures = parameters[0].value

  joinfieldmap = arcpy.FieldMap()

  joinfields = arcpy.ListFields(joinFeatures)

  

  for joinfield in joinfields:

       joinfieldname = unicodedata.normalize('NFKD', joinfield.name).encode('ascii','ignore')

       try:

            joinfieldmap.addInputField(joinFeatures, joinfieldname)

       except:

            pass

  parameters[1].value.addFieldMap(joinfieldmap)

  

  return

0 Kudos
2 Replies
DanPatterson_Retired
MVP Emeritus

is it because you are missing an extra bracket in this line?

fieldmapping.value = str('Empty')

should it be

fieldmapping.value = str('Empty') )

0 Kudos
BrianLai1
New Contributor

That bit works just fine.  My field mappings parameters does have its default value set to 'Empty'.  The problem is in updating the parameter once I input a feature class.  What I want it to do is pull fields from the feature class and display them in my field mappings parameter.  Sorry I should I have mentioned that.

0 Kudos