Field Mappings variable as a parameter not working in 10.5.1

2534
6
Jump to solution
05-16-2018 06:41 AM
deleted-user-miDSV-lAm0B-
New Contributor II

I have some custom models built to assist with the loading of events for roads & highways using the Append Events tool from the Location Referencing Toolset.  This was working beautifully in ArcMap 10.4.1 until I updated to 10.5.1.  I've attempted removing the append tool and re-adding it to the model and reconstructing the parameters, but nothing seems to work.  The Field Mappings variable as a parameter for the model will no longer update the field names dynamically based on the input and output tables it is fed.  I can get around this by editing the tool and specifying the field map tables within the variables while in the model's editing window, but this nearly defeats the purpose of the "custom" nature of the model and slows down the operation tremendously.

Curious if anyone else has seen this for other field mapping variables and if there is a fix or new setting in the updated version that I am not aware of.

0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor

Field mappings are pretty complicated but are well documented in the help (with examples):

FieldMappings—Help | ArcGIS Desktop 

You'd basically create an empty FieldMappings object and then run .addTable on the inputs to get the default mappings to populated at runtime based on the particular inputs. I'm not familiar with the tool you are discussing, but my experience with the Append and Merge tools is that unless I do this the mappings do not update to the default at runtime (ie they remain unchanged from the last time I validated the model in edit mode).

View solution in original post

6 Replies
curtvprice
MVP Esteemed Contributor

In my experience, these field maps only get properly populated is when you are validating the model at edit time -- not at runtime as you need to do here. To do what you want I have found I must create the field mappings at runtime using a Python function inside the Calculate Value tool, using the arcpy FieldMappings and FieldMap objects and their methods.

deleted-user-miDSV-lAm0B-
New Contributor II

Thanks for your reply Curtis. Sounds like I'd still need to edit the field mappings parameters prior to running the model though, correct? Or does it ask for them as user input during execution? Do you have an example you wouldn't mind sharing or pointing me to?

It functioned correctly at run time in 10.4.1 without editing the model ahead of time. I don't have experience attempting to do this sort of thing with other operations that use field maps to know if this was unique to the Append Events tool or not. Being able to use the model in this way was great for setting and limiting options made available during execution and streamlining the process, of course. I think now I will just need to separate that operation from the other tools tagged along after it into the native append and another custom model. It will definitely slow things down a bit. I wish this hadn't happened at this stage of our implementation. I hope ESRI will produce a fix for this and enable the use of other field map related tools to function this same way if they hadn't been, it was a very nice capability of model builder.

0 Kudos
curtvprice
MVP Esteemed Contributor

Field mappings are pretty complicated but are well documented in the help (with examples):

FieldMappings—Help | ArcGIS Desktop 

You'd basically create an empty FieldMappings object and then run .addTable on the inputs to get the default mappings to populated at runtime based on the particular inputs. I'm not familiar with the tool you are discussing, but my experience with the Append and Merge tools is that unless I do this the mappings do not update to the default at runtime (ie they remain unchanged from the last time I validated the model in edit mode).

deleted-user-miDSV-lAm0B-
New Contributor II

Thanks for the tip, from the sound of it that should restore part of the functionality.  I was hoping to restore the custom input field assignment as well 😕 At least with this if my field names and data types match 1 to 1 then they should all connect accurately by default.  There's really not a whole lot to this particular tool in the way of setup, so separating it from the other operations is not that bad if need be, just a nuisance after having this work successfully for so long.  Maybe ESRI enabled something about the field mappings for that tool in 10.4.1 they never intended too   Thanks for your help!

0 Kudos
deleted-user-miDSV-lAm0B-
New Contributor II

Thank you for pointing me in the right direction. I'm unfamiliar with doing this for other tools that require the use of field mappings in python like this and some of the info about the Append Events python usage is a little vague, so I thought I'd share. Since it's already requiring the target table as a parameter, it only needs to be fed the input table for the fms object. If my field names and data types match in both tables (which I am handling in a prior process), the input and target fields link right up. I only needed to loop through the input fields and remove those that don't match the target schema from the fms.  I've done a record count on my input table and target feature layer to confirm all events were added before returning a switch that tells other processes to continue or not.  I'm sure there's a better more efficient way to do this, but this is what I've come up with and so far seems to be working for my needs at the moment...time will tell here shortly I'm sure.  Also one thing to note, this is being done on 10.6 now.....Thanks again

Expression:
getEvents( "%RHStagingGDB%", "%RHTestingGDB%", "%DissolvedEventView%", "%RHEventLayer%" )

Code Block:
def getEvents( scratch, test, dtable, rhevent ):
  arcpy.CheckOutExtension( "Highways" )
  arcpy.env.scratchWorkspace = scratch
  arcpy.env.workspace = test
  fms = arcpy.FieldMappings()
  tfields = arcpy.ListFields( rhevent )
  tfnames = [f.name for f in tfields]
  srcrec = int( arcpy.GetCount_management( dtable ).getOutput(0) )
  tarrec = int( arcpy.GetCount_management( rhevent ).getOutput(0) )

  fms.removeAll()
  fms.addTable( dtable )

  for field in fms.fields:
    if not field.name in tfnames:
      fmindex = fms.findFieldMapIndex( field.name )
      fms.removeFieldMap( fmindex )

  arcpy.AppendEvents_locref( dtable, rhevent, fms, "ADD", "GENERATE_EVENT_IDS", "GENERATE_SHAPES" )

  totrec = int( arcpy.GetCount_management( rhevent ).getOutput(0) )

  if ( srcrec + tarrec == totrec ):
    return 1
  else:
    return 0‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
curtvprice
MVP Esteemed Contributor

If you could edit your post to format it as code

/blogs/dan_patterson/2016/08/14/script-formatting?sr=search&searchId=15526bb5-0af1-41dc-b0b4-f08f864...

it would be helpful for others. Also, please mark my June 4 answer as correct so it will be easy for people to find.

So glad this helped you out!

0 Kudos