Is it possible to have an email sent (to a list of recipients) whenever a point is added to a hosted File GDB on a public web-app?
The idea is to originate a permit application process from a web-map/app: applicants can add a point for the location they need a permit for, enter their information and apply for a permit (of particular kind). An email alerts officials that a permit has been applied for. The rest of the approval process flow from there and the hosted data is used to collect applicant information (including location for permit) as well as flag the status of the application as it makes its way through various stages of the approval process.
Any ideas on how to have edits trigger emails?
Solved! Go to Solution.
Edward,
Thank you for your quickly response. Your statement "Are you asking for a return of all records where the last edit date field or the status field has not changed in 24-48 hours? If so the date field would be easy to do it on" is what I am asking. How can I do it? Is there a script or other? Please advise.
The script that is at the top of this page would be the one you want to use. You would have to change some things to match your field names and your REST Service URL of course.
Also the current script is searching for dates a little different, you would need to change the current setup
you want to change lines 6 to your URL, also change line 7 to , this will return all of the attribute fields, also you can use the where part to query only certain features (i will tell you more about this at the bottom)
and then you want to change createDate to your LastEditedDate field, since that is what you are comparing on.
I would imagine yours would be something similar to
Once you have dumped the returns into the oidList you can access them by calling oidList['FIELDNAMEYOUWANT']
** alternatively you ignore the date compare in python and could use a SQL query in the params to only return records that havent been edited in the last 2 days
you would change the where part to something like this (this wont work because i am not sure the exact syntax for querying datefields in rest services because they use unix epoch time)
'where': "LAST_EDIT_DATE < TODAY()-2" # again this wont work, but it should give you an idea of what to look for.
Hello Edward.
Thank you for your response. Please clarify. What are features and attributes? Ex. Attributes = STATUS field? and features = ???
Do I need to replace LastDate = int(str(createDate)[0:-3]) ? I am not sure what this is..
If the status field is completed or closed, would it stop run 24 hours?
Thank you for your assistance!
attributes are your fields, but when i am referencing them in this case I am talking about the parts of your data object. data is made up of geometry and attributes, and likewise the attributes are made up features.
for feat in data['features'] <<- features here is to get at your attributes, ie you dont want geometry, just the features
oidList.append(feat['attributes']) <-- each feature has attributes, append these attributes in the list
LastDate = int(str([YOUR LAST DATE FIELD IN YOUR TABLE])[0:3]
^^ This is just making an INT (that is in UNIX EPOCH TIME) of your LAST DATE FIELD in YOUR RECORDS. For somereason you need to remove the trailing 000s that occur when using EPOCH TIME. This part is solely to get your time into a format that you can use datetime.datetime on to turn into a valid date to compare to your local tables.
*** addressing your screenshot below, you would have to send me your entire code, maybe post it here or make a zip file from it. That error could be caused by any number of things.
import urllib2, json, urllib, datetime, time, smtplib
from datetime import timedelta
oidList = []
URL = 'https://utility.arcgis.com/usrsvcs/servers/xxxxxxxxx/rest/services/xxxxx/FeatureServer/0/query'
params = {'f': 'pjson', 'where': "1=1", 'outfields' : 'OBJECTID, created_date', 'returnGeometry' : 'false'}
req = urllib2.Request(URL, urllib.urlencode(params))
response = urllib2.urlopen(req)
data = json.load(response)
for feat in data['features']:
createDate = feat['attributes']['created_date']
createDate = int(str(createDate)[0:-3])
t = datetime.datetime.now() - timedelta(hours=24)
t = time.mktime(t.timetuple())
if createDate > t:
oidList.append(feat['attributes']['OBJECTID'])
FROM = 'xxxxxxxx'
TO = ['xxxxxxxxxxxx']
SUBJECT = 'New Feature Added'
TEXT = "Features with OBJECTIDs " + str(oidList) + " were added."
message = """\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
smtpObj = smtplib.SMTP(host='xxxxxxxx', port=xxx)
smtpObj.sendmail(FROM, TO, message)
print "Successfully sent email"
smtpObj.quit()
It has been run successful but the email I have recieved is blank.
Ok so now you have built your oidList. It should contain a list of all the OBJECTIDs and their created_date.
You can check that by typing oidList in your python window and seeing what appears. You now have to go line by line through the list and build your email.
for me it was like this:
For you it may be different. but think of it like this, now you have a list (oiddict) that holds your objectid and your date. now you open a for loop and go through each line and make your email message (TEXT in my case) by appending it to a new list (in my case its called newList)
Then format an email msg, and then append the newList to it. then fire off the email.
i can probably share with you my script but i will have to clean it up and remove all my local server information and email accounts.
I am not sure that I am following. That would be great if you can share it to me. Much appericated.
here you go. i zipped the .py script and uploaded it here:
Thank you for the file. I revised it and tried to run it but I got invalid syntax. Screenshot below.
Send me the file with your changes and I will look at it. I am off for the weekend so it wont be till next week.