Select to view content in your preferred language

Getting field map to update at runtime?

1022
1
11-26-2019 07:13 AM
lah
by
Regular Contributor

Hi all, 

I'm having quite a time with this one. I've built a model doing some linear/location referencing geoprocessing. It works fine, other than the fact that I need to run multiple feature classes through it, and there are two points within the model that I need the field maps to reset to default at runtime (table to table and append events). I've searched endlessly for a solution to this - and have mostly been referencing this thread: Field Mappings variable as a parameter not working in 10.5.1 . I actually got the table to table field map to reset using this method - calculate value to a field mappings object. Here is my code for that one: 

Expression:
getEvents("%JoinTable%")

Code:
def getEvents(intable):
            fms = arcpy.FieldMappings()

            fms.removeAll()
            fms.addTable(intable)
            return fms‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

However, when I try to do the exact same thing for the Append Events step, it fails with errors 000622 - parameters not valid and 000735 - Field map: value is required. I also tried this code for this step, which produced the same error.

Expression: 
getEvents("%TableTable%", "%PODS Line Events%")
 
Code:
def getEvents(intable, target):
            fms = arcpy.FieldMappings()
            tfields = arcpy.ListFields(target)
            tfnames = [f.name for f in tfields]
            
            fms.removeAll()
            fms.addTable(intable)
            
            for field in fms.fields:
                        if not field.name in tfnames:
                                    fmindex = fms.findFieldMapIndex(field.name)
                                    fms.removeFieldMap(fmindex)
            return fms

I also tried just adding the target table as the input, since Append Events just needs the schemas to match, and I also tried a few other combinations, but I cant seem to get it to work. Apologies in advance, my python knowledge is very limited to nonexistent. Hoping someone can help shed some light on this. I'm using ArcPro 2.4, also attaching some screenshots to show how they are kind of set up. 

Thank you!

1 Reply
mbramos
Occasional Contributor

Dang, sorry to see that no one replied here because I'm having a very similar issue. Need to interate through pairs of FC and merge them, but each pair may or may not have a schema that's different from its predecessor/follower. Trying to figure out how I can code a 'merge' tool allows me to 'reset field mappings' each iteration. Then, I could import it back into Pro as a custom script tool. Unfortunately, my python is only good enough to be dangerous rather than considered fluent.

0 Kudos