2

Task: add several new fields to a feature class table and assign a default value to one of the fields.

Software/Environment: Using ArcMap 10.2 (Advanced License) and Python 2.7. Data is stored in file geodatabase in WGS_84 projection.

I used ModelBuilder to string the add field tools together and generate each new field. The last step in the model is to assign a default value based on a variable input. My model is shown below:

enter image description here

The desired output is shown below. This screen shot is from a finished edit, where I used the field calculator within the table to set the values after the tool had run. enter image description here

The model fails on adding the default value. The error log is below:

Executing: BV-IKE-Processing test2 test2
Start Time: Wed Apr 15 09:32:33 2015
Executing (Add BV_PoleID): AddField test2 BV_PoleID TEXT # # 13 # NULLABLE NON_REQUIRED #
Start Time: Wed Apr 15 09:32:33 2015
Adding BV_PoleID to test2...
Succeeded at Wed Apr 15 09:32:33 2015 (Elapsed Time: 0.08 seconds)
Executing (Add X_Long): AddField test2 X_Long DOUBLE # # # # NULLABLE NON_REQUIRED #
Start Time: Wed Apr 15 09:32:33 2015
Adding X_Long to test2...
Succeeded at Wed Apr 15 09:32:33 2015 (Elapsed Time: 0.09 seconds)
Executing (Add Y_Lat): AddField test2 Y_Lat DOUBLE # # # # NULLABLE NON_REQUIRED #
Start Time: Wed Apr 15 09:32:33 2015
Adding Y_Lat to test2...
Succeeded at Wed Apr 15 09:32:34 2015 (Elapsed Time: 0.07 seconds)
Executing (Add Z_Alt): AddField test2 Z_Alt SHORT # # # # NULLABLE NON_REQUIRED #
Start Time: Wed Apr 15 09:32:34 2015
Adding Z_Alt to test2...
Succeeded at Wed Apr 15 09:32:34 2015 (Elapsed Time: 0.09 seconds)
Executing (Add Data_Pkg): AddField test2 Data_Pkg TEXT # # # # NULLABLE NON_REQUIRED #
Start Time: Wed Apr 15 09:32:34 2015
Adding Data_Pkg to test2...
Succeeded at Wed Apr 15 09:32:34 2015 (Elapsed Time: 0.09 seconds)
Executing (Add Status): AddField test2 Status TEXT # # # # NULLABLE NON_REQUIRED #
Start Time: Wed Apr 15 09:32:34 2015
Adding Status to test2...
Succeeded at Wed Apr 15 09:32:34 2015 (Elapsed Time: 0.06 seconds)
Executing (Assign Data_Pkg): AssignDefaultToField test2 Data_Pkg test2 #
Start Time: Wed Apr 15 09:32:34 2015
ERROR 999999: Error executing function.
Failed to execute (Assign Data_Pkg).
Failed at Wed Apr 15 09:32:34 2015 (Elapsed Time: 0.02 seconds)
Failed to execute (BV-IKE-Processing).
Failed at Wed Apr 15 09:32:34 2015 (Elapsed Time: 0.64 seconds)

I converted the model to a python script in case it will help. The python version is here:

    # Import arcpy module
import arcpy

# Script arguments
Input_Table = arcpy.GetParameterAsText(0)

Data_Pkg_Name = arcpy.GetParameterAsText(1)

# Local variables:
Output_Feature_Class = Input_Table
Output_Feature_Class__2_ = Output_Feature_Class
Output_Feature_Class__3_ = Output_Feature_Class__2_
Output_Feature_Class__4_ = Output_Feature_Class__3_
Output_Feature_Class__5_ = Output_Feature_Class__4_
Output_Feature_Class__6_ = Output_Feature_Class__5_
Output_Table = Output_Feature_Class__6_

# Process: Add BV_PoleID
arcpy.AddField_management(Input_Table, "BV_PoleID", "TEXT", "", "", "13", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Add X_Long
arcpy.AddField_management(Output_Feature_Class, "X_Long", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Add Y_Lat
arcpy.AddField_management(Output_Feature_Class__2_, "Y_Lat", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Add Z_Alt
arcpy.AddField_management(Output_Feature_Class__3_, "Z_Alt", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Add Data_Pkg
arcpy.AddField_management(Output_Feature_Class__4_, "Data_Pkg", "TEXT", "", "", "25", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Add Status
arcpy.AddField_management(Output_Feature_Class__5_, "Status", "TEXT", "", "", "50", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Assign Data_Pkg
arcpy.AssignDefaultToField_management(Output_Feature_Class__6_, "Data_Pkg", Data_Pkg_Name, "")

Disclaimers: I'm a noob when it comes to coding/scripting/model builder. This is intended to be a time saver for me and possibly be extended by some more complex coding (creating an iterator to generate the BV_PoleID, etc).


I edited the tool to include text lengths for the Data_Pkg and Status fields. The tool still failed to assign the default value and returned the same error message.

  • Decided to run Assign Default Value to Field straight from ArcToolbox with the same parameters as my model. The tool ran successfully, but didn't assign the values. – Chad Smith Apr 16 '15 at 14:06
  • Note that assign default value, only assigns a default value to the field; thus any NEW row will get the value, However it doesn't alter the rows which are already there. To do the latter you should use calculate field with a python expression as: '%Value%' – dof1985 Apr 16 '15 at 15:05
1

Attached below is a modification of your code that works.

Shortly, the problem was the use of outputs names, as follows:

# Local variables:
Output_Feature_Class = Input_Table
Output_Feature_Class__2_ = Output_Feature_Class
Output_Feature_Class__3_ = Output_Feature_Class__2_
Output_Feature_Class__4_ = Output_Feature_Class__3_
Output_Feature_Class__5_ = Output_Feature_Class__4_
Output_Feature_Class__6_ = Output_Feature_Class__5_
Output_Table = Output_Feature_Class__6_

Namely, python or model builder do not handle underlines well (when it ends a variable name). Fixing it is easy if you follow those rules:

  1. In model builder, use the rename option to rename your outputs and prevent using spaces, underlines, dash, Brackets, or any other sign which is not a letter or a number.
  2. If in arcpy, do the same, without renaming - just give proper names. See the modified code below:

      # Import arcpy module
      import arcpy
    
      # Script arguments
      Input_Table = arcpy.GetParameterAsText(0)
      Data_Pkg_Name = arcpy.GetParameterAsText(1)
    
      # Local variables:
      Output1 = Input_Table
      Output2 = Output1
      Output3= Output2
      Output4 = Output3
      Output5 = Output4
      Output6= Output5
      Output_Table = Output6
    
      # Process: Add BV_PoleID
      arcpy.AddField_management(Input_Table, "BV_PoleID", "TEXT", "", "", "13", "",    "NULLABLE", "NON_REQUIRED", "")
    
      # Process: Add X_Long
       arcpy.AddField_management(Output1, "X_Long", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
    
      # Process: Add Y_Lat
      arcpy.AddField_management(Output2, "Y_Lat", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
    
      # Process: Add Z_Alt
      arcpy.AddField_management(Output3, "Z_Alt", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
    
      # Process: Add Data_Pkg
      arcpy.AddField_management(Output4, "Data_Pkg", "TEXT", "", "", "25", "", "NULLABLE", "NON_REQUIRED", "")
    
      # Process: Add Status
      arcpy.AddField_management(Output5, "Status", "TEXT", "", "", "50", "", "NULLABLE", "NON_REQUIRED", "")
    
      # Process: Assign Data_Pkg
      arcpy.AssignDefaultToField_management(Output6, "Data_Pkg", Data_Pkg_Name, "")
    
0

Looking at your python version, it appears you are not setting the length of the text fields when you add them, you must do this, it is not optional for a text field. This would explain why the assign default would fail.

  • My question was edited to include your answer. The tool still failed. Thank you for the help. – Chad Smith Apr 15 '15 at 19:12
  • That should read my *script. Thanks for your help in making my code cleaner. – Chad Smith Apr 16 '15 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.