2

I'd like to make a model with a last step, a python script, that will send an email, containing messages and status of the model. The email sending part is okay, nut the hard part for me is to query the status and the message. I found the "result" and the "GetMessages" things but could not succeeded.

I'd like to make it work on a really simple model than use it when its needed. The dummy model looks like this:

i.imgur.com/hjaSJFc.png

And the script:

import arcpy
arcpy.ImportToolbox(r"c:\arcGIS_2_email\toolbox\tulboksz.tbx")
arcpy.topmodel_tulboksz()
result = arcpy.topmodel_tulboksz()
print result.getMessages()
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Mar 21, 2016 at 14:18
2
  • 3
    What, specifically, happened when the script did not succeed? Are you getting error messages? Can you post some of your code and an outline of your model, and how the two are connected? Commented Mar 21, 2016 at 14:20
  • result.status indicates if the last step succeeded Commented Mar 21, 2016 at 14:32

1 Answer 1

2

The easiest way to do this might be to turn your email script into a wrapper script round around your model (run a tool inside a tool). This script would call your script, catch any errors and then send an email with the result.

If your email script is in the form:

import arcpy
import os.path
try:
 path = arcpy.GetParameterAsText(0)
 params = arcpy.GetParameter(1)
 tbx_path, selected_tool = os.path.split(path) # Split so you have the toolbox and tool name
 tbx = arcpy.ImportToolbox(tbx_path) # import the toolbox
 tool = getattr(tbx, selected_tool) # get the tool by name from the toolbox
 result = tool(*params) # run the tool with all the required parameters
 messages = arcpy.GetMessages()
 status = result.status # Will be 4 on successful completion - see http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-classes/result.htm
except Exception as e:
 messages = arcpy.GetMessages(2) # get only the error messages (alternately just use the exception object)
 status = 5 # For Failed
# Send your email with your messages here

If you then add the script to a toolbox with two parameters:

  • The first should have a Data Type of Tool, which will be the model you're interested in running
  • The second should have a type of Any value, which will contain any arguments you might need to pass to your model, in order (including output parameters). This should be set to accept multiple values, and be an optional parameter.

Then run the model by using your email script as the wrapper, and you will get an email back with the status of the tool.

answered Mar 22, 2016 at 3:23
2
  • Henry! I tried it but something not working. I aint got no email when the script/model fails. Here is my whole script: pastebin.com/ZXdDNymC The script/tool/model should be able to send the email if the process fails or not. Now It send the email only when it not fails. Here is an additional image <img src="i.imgur.com/uuPogqs.png"> please help me, what did I wrong? Commented Mar 30, 2016 at 15:37
  • @STO I've updated the answer so it's a bit more explicit - if the tool fails before you get the result object in the try then status wasn't being set. That's what it looks like is causing the message to fail generating if the script fails. Also, to make it a bit more usable I'd set the second parameter to accept multiple inputs so it can be ussed with any tool. If you've still got issues it's probably best to take this to a chat room - feel free to set one up and ping me. Commented Mar 30, 2016 at 22:07

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.