I created a short script which creates a layer from a table view, applies a definition query (hereafter DQ) to that layer, then uses that layer to generate a report. The script works perfectly except for in cases where the DQ results in an empty layer (i.e. no records in the SDE table satisfy the query). When this happens, the script results in an error message. This would not bother me, except for the fact that this script is primarily used by a fellow co-worker who has little background in GIS. I would prefer that instead of getting an error message, he/she gets a statement saying something like "no records found for this report, report cannot be generated."
With the help of someone on this forum, I have come up with an updated script with an if/then
statement. This script works exactly as it should, but only when run in IDLE. I have created an ArcGIS toolbox, added a script to that toolbox, and referenced the Python script through the toolbox script. When I run this tool through the toolbox script in ArcCatalog, I receive an error. Why does this tool work perfectly in IDLE, but not in ArcCatalog? Perhaps I am using a command that ArcGIS does not recognize? I will include the updated sript and the error message below. You will see below that the script is failing after the line arcpy.AddMessage('1')
but before the line arcpy.AddMessage("Parameters set, generating report")
.
#THIS SCRIPT WILL GENERATE AN INSPECTION REPORT FOR APPROACHING DEADLINES
#DEFINE PARAMETERS
import arcpy, os, datetime
from arcpy import env
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r'filepath'
today = datetime.date.today()
Str_today = str(today)
where_clause = "OPEN_ = 'Y' AND ( NOTICE_DEADLINE >=" + "'" + Str_today + "'" + "OR NOTICE_DEADLINE2 >=" + "'" + Str_today + "'" + "OR NOTICE_DEADLINE3 >=" + "'" + Str_today + "'" + ")"
#BUILD LAYER FOR APPROACHING DEADLINES
mxd = arcpy.mapping.MapDocument(r"filepath.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "")[0]
lyr = arcpy.mapping.ListTableViews(mxd, "", df)[0]
lyr.definitionQuery = where_clause
arcpy.AddMessage('1')
DQ_Result = arcpy.GetCount_management(lyr)
Count = int(DQ_Result.getOutput(0))
arcpy.AddMessage("Parameters set, generating report")
#GENERATE REPORT FOR APPROACHING DEADLINES
if (Count == 0):
arcpy.AddMessage("No records exist for this report, cannot generate report")
else:
arcpy.mapping.ExportReport(lyr, r"filepath.rlf", r"filepath.pdf")
del mxd
os.startfile(r"filepath.pdf")
arcpy.AddMessage("Report generated and in appropriate file")
arcpy.AddMessage("Tool Complete")
Here is the error message I receive in the ArcCatalog result box when I try to run the script. It is not very helpful.
Executing: Deadlines
Start Time: Thu Sep 22 08:18:31 2016
Running script Deadlines...
1
ERROR 000714: Error in script Deadlines.
Error in executing: cmd.exe /C Q:\GIS_MA~1\BUILDI~1\INSPEC~1\DO_NOT~1\Scripts\DEADLI~1.PY "#"
Failed to execute (Deadlines).
Failed at Thu Sep 22 08:18:40 2016 (Elapsed Time: 9.10 seconds)
.tbx
)? Looks like you might be calling an external python process from within Arc*.