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

14125
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
DanielAbera
New Contributor III

Thanks Jake. Yes I after I posted the question, I found another post by you about the token and it worked well. I only had some issue with the send email part. I used gmail and gmail has very strict condition, and this is how I updated. I also added some conditional statement to check if there is a new data, if not it will not send email. ( just to avoid sending blank emails every hour. )

0 Kudos
JakeSkinner
Esri Esteemed Contributor

I went ahead and created a Document for these steps:

https://community.esri.com/docs/DOC-10163 

RayGreen
New Contributor II

Hello Jake or anyone else,

First of all this is a great notification script, and I admit I do not know a whole lot when scripting.   I was wondering if I could append multiple fields in the oidList for example I have the OBJECTID field and an 'Information' field that I both want to show up in the append list of the email.

Does that make sense?

Thanks!

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Hi aggiez98‌,

This is possible, but would take some additional scripting.  Take a look at the following document:

https://community.esri.com/docs/DOC-10163-send-email-when-a-feature-is-added-to-a-arcgis-online-host...

You would need to create a new empty list for the information (i.e. informationList = []).  You can then append the values to this list:

params = {'f': 'pjson', 'where': "1=1", 'outfields' : '{0}, {1}, {2}'.format(uniqueID, dateField, information), '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'][dateField]
    createDate = int(str(createDate)[0:-3])
    t = datetime.datetime.now() - timedelta(hours=hoursValue)
    t = time.mktime(t.timetuple())
    if createDate > t:
        oidList.append(feat['attributes'][uniqueID])
        informationList.append(feat['attributes'][information])‍‍‍‍‍‍‍‍‍‍‍‍

Finally, you would have to iterate through this list to update the e-mail message, or you could simply send the entire list in the message.

RayGreen
New Contributor II

So if I just use the oidList it works fine, but when I try and add a second list it does not seem to work.  Below is how I was adding my second list.  Appreciate the response!  If it is to much time no worries I will not keep bothering you.

oidList = []
informationList = []

URL = 'http://XXXXXXXXXXXX/FeatureServer/0/query'
params = {'f': 'pjson', 'where': "1=1", 'outfields' : 'OBJECTID, ReminderDate, Information', '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']['ReminderDate']
createDate = int(str(createDate)[0:-3])


t = datetime.datetime.now()
t = time.mktime(t.timetuple())
s = datetime.datetime.now() + timedelta(hours=24)
s = time.mktime(s.timetuple())


if t <= createDate <= s:
oidList.append(feat['attributes']['OBJECTID'])
informationList.append(feat['attributes']['Information'])‍









FROM = 'XXXXXX'
TO = ['email##.com']
SUBJECT = 'Incident action required!'
TEXT = "Incident(s) with OBJECTID(s) " + str(oidList) + str(informationList) + " are in danger of missing their action date."
0 Kudos
EdwardHankins
New Contributor II

It sounds like you want to loop through each record in oidList and output the OID and the Information.  This can be done like this,  loop through the oidList line by line, then output that to the email txt.  The below code is pretty raw but i think it may help.  The time stuff at the top was left in there since the subject requires it.  

t = datetime.datetime.now() - timedelta(hours=24)
t = time.mktime(t.timetuple())
TIMEX = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(t))


for oid in oidList:
   i = i+1
   date = datetime.datetime.now().strftime( "%d/%m/%Y %H:%M" )
   TMPTIME = oid['Closed_Date']/1000
   TIME = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(TMPTIME))
   TEXT = "\n\t" + str(i) + ") ZONE = " + str(oid['Zone']) + " Feature with ORACLE # " + str(oid['Oracle_ID']) + " was    closed on " + str(TIME) +" " + " LOCATION: " + str(oid['Streets']) + "\n\tWORK SUMMARY: " + str(oid['Notes'])
   newList.insert(i,TEXT)
   print TEXT


CURRENT=time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(t))
FROM = 'name@blah.com' #CHANGE TO YOUR NAME
TO = ['name@blah.com'] #CHANGE THIS TO RECEPIENT, ADD MORE AFTER THE COMMA
SUBJECT = '*** ' + str(i) +' Service Request(s) Closed in the GIS Records since: ' + str(TIMEX) + " ***"
BODY = "Features with OBJECTIDs " + str(oidList) + " were Closed."
PRIORITY = '2'

message = """\
From: %s
To: %s
X-Priority: %s
Subject: %s

%s
""" % (FROM, ", ".join(TO), PRIORITY, SUBJECT, '\r\n'.join(newList))

RayGreen
New Contributor II

I tried this and it was giving me i is undefined, what I wanted was two values to be listed so I went with this and just listed the "mergelist' in my email and gave me something close enough to what I was needing.  Thank you for the response I am sure your way was much better but I am still learning so had to make due with what works!

if t <= createDate <= s:
oidList.append(feat['attributes']['OBJECTID'])
complianceList.append([feat['attributes']['Compliance_Trigger']])
mergelist = zip(oidList, complianceList)

FROM = 'someone'
TO = ['email.com']
SUBJECT = 'Incident action required!'
TEXT = "Incident(s) with OBJECTID(s) " + str(mergelist) + " are in danger of missing their action date."
0 Kudos
EdwardHankins
New Contributor II

Sorry for the late reply, just add i = 0 right after you declare the TIMEX variable. 

ie:

t = datetime.datetime.now() - timedelta(hours=24)
t = time.mktime(t.timetuple())
TIMEX = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(t))

i = 0

for oid in oidList:
   i = i+1
   date = datetime.datetime.now().strftim .....

0 Kudos
AlexP_
by
Occasional Contributor III

Hello Jake,

I have a question. Do you or anyone know what is the script for 'Last edited date' field or 'Status' field that hasn't touch or change for last 24 and 48 hours triggers to email notification? It is about service request for internal on their end. Please advise. I can't find it anywhere. Thank you.

0 Kudos
EdwardHankins
New Contributor II

I dont fully understand your question.  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,  just change your range of dates which you compare the last edited date to,  ie show me all records where last edited date is less than than today - 2 (that is just psuedocode)  If you are trying to compare records where the status has not changed in 2 days that will be more difficult.  Also if i didnt understand your question please re-explain. 

0 Kudos