1

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)
6
  • 1
    How does it fail? What error message, including a line number does it throw from the code presented?
    – PolyGeo
    Commented Sep 20, 2016 at 12:57
  • The error message is as follows: 'ERROR 000714: Error in script test. Error in executing: cmd.exe /C Q:\GIS_MA~1\BUILDI~1\INSPEC~1\DO_NOT~1\Scripts\DEADLI~1.PY "#" Failed to execute (test)' Not a very helpful error message.
    – Jacob DA
    Commented Sep 20, 2016 at 15:42
  • Please include the error message in your question rather than as comments which are not always read by potential answerers. There is no line number in the error message - are you certain that it came from running the precise code that you have presented? How precisely are you running it in ArcCatalog?
    – PolyGeo
    Commented Sep 20, 2016 at 19:13
  • @PolyGeo the question above now reflects the most current version of the script. I am running the same script in ArcCatalog as I am in IDLE. In fact, I am referencing the Python script from the script in Catalog.
    – Jacob DA
    Commented Sep 22, 2016 at 12:26
  • Are you using a script tool inside a toolbox (.tbx)? Looks like you might be calling an external python process from within Arc*.
    – Paul
    Commented Sep 22, 2016 at 15:49

1 Answer 1

1

After applying your definition query to your table view object I think you should be able to use the GetCount function to see if it contains any rows.

1
  • Thank you for the response. I tried your suggestion and got it working in IDLE, but it fails in ArcCatalog. I have added the new code as a code block in my original question.
    – Jacob DA
    Commented Sep 20, 2016 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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