Generate/send email when a feature is added to a hosted service

14464
67
Jump to solution
02-24-2015 09:47 AM
ChandreyeeLahiri
New Contributor III

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?

67 Replies
JakeSkinner
Esri Esteemed Contributor

I'm not sure what's happening with this service.  When I try and query it, and include to return all the fields, only the 'OBJECTID' field is returned:

URL = 'http://sig.versaillesgrandparc.fr/arcgis/rest/services/Hosted/Nicolli_Rapports/FeatureServer/0/query'
params = {'f': 'json', 'where': "1=1", 'outfields' : '*'}
req = urllib2.Request(URL, urllib.urlencode(params))
response = urllib2.urlopen(req)
data = json.load(response)
for feat in data['features']:
    print feat['attributes']

I would test the above script on other services to see if other fields are returned other than just the 'objectid'.

0 Kudos
RemiFoicik
New Contributor II

Yes, I saw that on Fiddler :\ 

But I get the created_date attributes when I'm doing the query manually on the Sharing/Rest

0 Kudos
RemiFoicik
New Contributor II

Maybe could I have more chance working directly with Querys from sharing/rest like 

http://sig.versaillesgrandparc.fr/arcgis/rest/services/Hosted/Rapports_Nicollin/FeatureServer/0/quer... 

Thanks for your time, I will try to understand how to make python working with that  

0 Kudos
DominikVisca1
New Contributor

This is exactly what I'm looking for,  thank you for this script. However I get a KeyError 'features' in line 11. Is this a field from example feature class?

0 Kudos
JakeSkinner
Esri Esteemed Contributor

d_visca‌, 'features' is a key returned from the JSON of the feature service.  This key is returned for all feature services.  Do you have editor tracking enabled?  The code queries for fields OBJECTID and created_date.  If these do not exist, this may be the reason you are receiving this error.

0 Kudos
DominikVisca1
New Contributor

I have the fields OBJECTID and CreationDate, so I changed create_date to my field name. I also activated the editor tracking but I still geht the same KeyError: 'features'. Maybe the problem is my URL, because it is a secure feature layer? I use the token as URL parameter like this:

http://server.arcgis.com/####/arcgis/rest/services/####/FeatureServer/0?token=####/query'  

Maybe this is twisting everything up?

0 Kudos
DominikVisca1
New Contributor

So my feature layer does not have any key named "features" or "attributes". I have keys like 'types', 'templates' and 'indexes'...

0 Kudos
KevinDunkin1
New Contributor III

Sounds like the issue is your URL.

Make sure you are not just using the rest endpoint service URL, but have the query JSON page ".../FeatureServer/0/query'" ending of the URL. I had the same issue with a publicly shared HFS and needed to add "/0/query" to the end of my URL

DanielAbera
New Contributor III

Hi Jake,

I came across your post while searching for email  alert for data change, and I found it interesting. I tried to use your code as is with my hosted Feature Service link, but still getting the error: KeyError: 'features'. Someone has reported the same error above. I have both the OBJECTID and create_date fields. Any idea why I'm getting the error. Thanks for your help in advance. 

Daniel

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Hi Daniel,

You will need to generate a token if your feature service is not shared with 'Everyone'.  Try the following:

import urllib2, json, urllib, datetime, time, smtplib
from datetime import timedelta

oidList = []

username = 'jskinner_CountySandbox'
password = '********'

try:
    print('Generating Token')
    tokenURL = 'https://www.arcgis.com/sharing/rest/generateToken'
    params = {'f': 'pjson', 'username': username, 'password': password, 'referer': 'http://www.arcgis.com'}
    req = urllib2.Request(tokenURL, urllib.urlencode(params))
    response = urllib2.urlopen(req)
    data = json.load(response)
    token = data['token']
except:
    token = ''

URL = 'http://services.arcgis.com/dlFJXQQtlWFB4qUk/arcgis/rest/services/PropDamage/FeatureServer/0/query'
params = {'f': 'pjson', 'where': "1=1", 'outfields' : 'OBJECTID, created_date', 'returnGeometry' : 'false', 'token' : token}
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=1)
    t = time.mktime(t.timetuple())
    if createDate > t:
        oidList.append(feat['attributes']['OBJECTID'])

print oidList

FROM = 'sender@hotmail.com'
TO = ['receiver@yahoo.com']
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='smtp.hotmail.com', port=25)
smtpObj.sendmail(FROM, TO, message)
print "Successfully sent email"
smtpObj.quit()