AnsweredAssumed Answered

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

Question asked by lai34 on Jul 28, 2014
Latest reply on Jul 28, 2014 by lai34

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

Outcomes