Hi smart people!!
I have created a script to automate our workflow at work but have been having difficulty with my optional parameter. I've googled every possible help i could get with this but nothing so far...
Please see below the script:
# Import all necessary modules
import os, sys, arcpy
from arcpy import env
import getpass
# Define all objects to be used
sde_conn = "S:/Projects/Oilsands/FortHills/OS0101_Fort_Hills_MPG/010_FH_GDA/002-GDA_LOG/My_ArcSDE_Conn.sde"
outpath = sde_conn + "/"
gdaFC = "FORTHILLS.GDA_PERMIT_BOUNDARY_A"
arcpy.AddMessage("SDE Connection Successful")
# Prompt for user parameters
table = arcpy.GetParameterAsText(0)
field = arcpy.GetParameterAsText(1)
gdanum = arcpy.GetParameterAsText(2)
extstart = arcpy.GetParameter(3)
extend = arcpy.GetParameter(4)
extdue = arcpy.GetParameter(5)
asbsubmit = arcpy.GetParameter(6) # Optional Parameter
arcpy.SetParameter(3, gdanum)
# Specify selection
whereClause = '"GDA_NUMBER"' + " = '" + str(gdanum) + "'"
# Updates information of gda feature class
try:
cursor = arcpy.UpdateCursor(outpath + gdaFC, whereClause)
for row in cursor:
row.setValue("EXTENSION_START_DATE", extstart)
row.setValue("EXTENSION_FINISH_DATE", extend)
row.setValue("EXTENSION_DUE_DATE", extdue)
row.setValue("ASBUILT_SUBMIT_DATE", asbsubmit) # Script should skip this if no value entered
row.setValue("STATUS", "E")
row.setValue("INTERNAL_ROW_CHANGED_DATE", time.strftime("%x %X", time.localtime()))
row.setValue("INTERNAL_ROW_CHANGED_BY", getpass.getuser().upper())
cursor.updateRow(row)
del cursor, row
except:
print arcpy.GetMessages(1)
arcpy.AddMessage("GDA has been extended")
Solved! Go to Solution.
When a parameter is not set, this is normally replaced by a "#" sign. I did a small test and reading the optional parameter when it is supposed to be an object ended in a "Object: Error in getting parameter as text" error. So I placed it in a try except and assigned it to None when there is an error (presumably the parameter was not set).
I guess you could do something like this:
# Import all necessary modules import os, sys, arcpy from arcpy import env import getpass # Define all objects to be used sde_conn = "S:/Projects/Oilsands/FortHills/OS0101_Fort_Hills_MPG/010_FH_GDA/002-GDA_LOG/My_ArcSDE_Conn.sde" gdaFC = "FORTHILLS.GDA_PERMIT_BOUNDARY_A" fld_gdanum = "GDA_NUMBER" # Prompt for user parameters table = arcpy.GetParameterAsText(0) field = arcpy.GetParameterAsText(1) gdanum = arcpy.GetParameterAsText(2) extstart = arcpy.GetParameter(3) extend = arcpy.GetParameter(4) extdue = arcpy.GetParameter(5) try: asbsubmit = arcpy.GetParameter(6) # Optional Parameter except: asbsubmit = None ##arcpy.SetParameter(3, gdanum) # Specify selection fc = os.path.join(sde_conn, gdaFC) whereClause = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(fc, fld_gdanum), gdanum) # Updates information of gda feature class try: cursor = arcpy.UpdateCursor(fc, whereClause) for row in cursor: row.setValue("EXTENSION_START_DATE", extstart) row.setValue("EXTENSION_FINISH_DATE", extend) row.setValue("EXTENSION_DUE_DATE", extdue) if asbsubmit != None: row.setValue("ASBUILT_SUBMIT_DATE", asbsubmit) # Script should skip this if no value entered row.setValue("STATUS", "E") row.setValue("INTERNAL_ROW_CHANGED_DATE", time.strftime("%x %X", time.localtime())) row.setValue("INTERNAL_ROW_CHANGED_BY", getpass.getuser().upper()) cursor.updateRow(row) del cursor, row except: print arcpy.GetMessages(1) arcpy.AddMessage("GDA has been extended")
I switched line 22 off. Not sure why you want to set the value of the parameter extstart (3) to contain the value contained in gdanum...
When a parameter is not set, this is normally replaced by a "#" sign. I did a small test and reading the optional parameter when it is supposed to be an object ended in a "Object: Error in getting parameter as text" error. So I placed it in a try except and assigned it to None when there is an error (presumably the parameter was not set).
I guess you could do something like this:
# Import all necessary modules import os, sys, arcpy from arcpy import env import getpass # Define all objects to be used sde_conn = "S:/Projects/Oilsands/FortHills/OS0101_Fort_Hills_MPG/010_FH_GDA/002-GDA_LOG/My_ArcSDE_Conn.sde" gdaFC = "FORTHILLS.GDA_PERMIT_BOUNDARY_A" fld_gdanum = "GDA_NUMBER" # Prompt for user parameters table = arcpy.GetParameterAsText(0) field = arcpy.GetParameterAsText(1) gdanum = arcpy.GetParameterAsText(2) extstart = arcpy.GetParameter(3) extend = arcpy.GetParameter(4) extdue = arcpy.GetParameter(5) try: asbsubmit = arcpy.GetParameter(6) # Optional Parameter except: asbsubmit = None ##arcpy.SetParameter(3, gdanum) # Specify selection fc = os.path.join(sde_conn, gdaFC) whereClause = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(fc, fld_gdanum), gdanum) # Updates information of gda feature class try: cursor = arcpy.UpdateCursor(fc, whereClause) for row in cursor: row.setValue("EXTENSION_START_DATE", extstart) row.setValue("EXTENSION_FINISH_DATE", extend) row.setValue("EXTENSION_DUE_DATE", extdue) if asbsubmit != None: row.setValue("ASBUILT_SUBMIT_DATE", asbsubmit) # Script should skip this if no value entered row.setValue("STATUS", "E") row.setValue("INTERNAL_ROW_CHANGED_DATE", time.strftime("%x %X", time.localtime())) row.setValue("INTERNAL_ROW_CHANGED_BY", getpass.getuser().upper()) cursor.updateRow(row) del cursor, row except: print arcpy.GetMessages(1) arcpy.AddMessage("GDA has been extended")
I switched line 22 off. Not sure why you want to set the value of the parameter extstart (3) to contain the value contained in gdanum...
Thank you so much Xander!!! It works!
Happy new year to you
I'm glad it works. Happy New Year to you too.