Generate Survey123 Report

2726
27
Jump to solution
11-28-2018 09:56 AM
ChelseaRozek
MVP Regular Contributor

Really excited about the latest Python API update. Would like to be able to export survey responses as reports with Python. I'm currently stuck at this part. I can see that the reports get generated and I can download them (image below from Survey123 website), but I receive an error in the code: KeyError: 'results' (see attachment). I'm also curious if you can access the generated reports directly with Python after making them.

0 Kudos
27 Replies
JohnStowell
New Contributor III

Hi Michelle, if you are getting syntax errors with print() that would indicate you are using Python v2.

My code is written for Python v3. Can you check which version you are using? 

0 Kudos
MichelleWilliams1
Occasional Contributor III

Thank you for your quick response. I was using Python v2.

v3 - The code seems to run, but I'm getting zero reports, and I submitted a new survey today, any ideas why?

--- STARTING REPORT GENERATION PROCESS --- 2020-02-24 13:59:55.291881
Initialising session in AGOL
Reading Survey123 information for ID: 43335467cc64408d8928c890124a9ee6 
Selected template: <Item title:"BSI Infraction Letter2020B.docx" type:Microsoft Word owner:WilZZZ >
Generating report(s) for submissions from last 24 hours
Downloading relevant report(s) to: C:\Users\WilZZZ\Documents\BSIFeatureReportsPY
REPORTS GENERATED: 0
--- REPORT GENERATION PROCESS - FINISHED ---

--- STARTING EMAIL PROCESS ---
Getting list of Word docx files in: C:\Users\William5\Documents\BSIFeatureReportsPY
Files:
Reading raw table data from Word document(s)
DOCUMENTS SENT TO RECIPIENTS: 0
--- EMAIL PROCESS - FINISHED ---

0 Kudos
JohnStowell
New Contributor III

I've seen that issue with another user of the script, not sure if they were able to get it working or not?

cc Simon Allard

I think it is related to the where clause of the script. Try changing:

where_filter = '{"where":"CreationDate >= CURRENT_TIMESTAMP - INTERVAL \'1\' DAY"}'

to:
where_filter = '1=1'
And see if you have any luck. Also check the utc_offset variable is correct for your timezone.

If that works, you might need to play around with the where query at your feature server query page to see what works for your specific dataset (eg. https://....arcgis.com/.../ArcGIS/rest/services/.../FeatureServer/0/query )

This page helps to explain what the date query parameters should look like:

Querying Feature Services: Date-Time Queries 

0 Kudos
SimonAllard2
New Contributor III

Heya, I never did get it to work. I used the same where clause that worked when tested on the rest end service.

I removed the where clause and used the default 1:1 and got all the reports to export out!

I need to spend some more time on it, maybe if it becomes of buisness critical importance...

Cheers,

Simon

Get Outlook for Android<https://aka.ms/ghei36>

0 Kudos
MichelleWilliams1
Occasional Contributor III

Thank you, 

I've updated my utc_offset and made the where_filter = {"where":"LetterPrinted='no', AND Submit='yes'"} let me know if this is correct #Newbie.

I'm getting this error:

  File C:\... line 145, in main
     if surveyID in x.description:
TypeError: argument of type 'NoneType' is not iterable

I'm not sure if my where_filter or something else. Thank you for your time.

0 Kudos
ElizabethBurniston
New Contributor II

I have the script working fine through Task Scheduler. It creates and drops the word document in AGOL, and has successfully accessed the feature report template. However, the report does not output any of the Survey123 results. It looks to be just a copy of the template. Can you tip me off as to where I have gone wrong? Thank you!

0 Kudos
DrewNemecek
New Contributor II

Thank you for this script. It helped me fight through the process of automating download of feature reports from Survey123. However, I have a question. I have an Excel list of about 500 feature reports that need to be downloaded. The list includes a lot of identifying information from the records in the survey's feature service. For example, the list includes a field called "1. WMIS Number:", which is a unique identifier. I need to query this field for the where_filter, and after reading your script, I tried the following as well as some other queries:

where_clause = '{"where":"1. WMIS Number: = \'100615\'"}'
where_clause = '{"where":"1. WMIS Number:=\'100615\'"}'

Neither of these worked. I attached a screenshot of the field in Survey123. Do you have any suggestions?

0 Kudos
BHeist
by
New Contributor II

Drew,

I was able to get the 'Where' clause to work using this syntax:

"last_edited_date" was the field in my layer that I wanted to query by. This generated a report from my survey for the last 7 days successfully 

 

where_filter = "last_edited_date >= CURRENT_TIMESTAMP - INTERVAL '7' DAY"

survey.generate_report(template, where_filter, "+0400", "NCLF Daily Flare Inspection")