I am authoring a survey with Survey123 XLSForms and I cannot quite get where I want with choice filters and the relevant field.
The form has a question, "date_drit_test" that the user can leave blank (if no test occurred) or fill out (if a test occurred):
If the user selects a date, then another question, 'drit_test_result' should appear to indicate test results (which was hidden if no date was selected):
However, I do not want any null values in the 'drit_test_result' column. If a user does not select a date for the DRIT test, I want the value of 'drit_test_result' to be filled with "Not tested"/"NT".
I've got everything working save the last bit. I also suspect I could make my choice filter without the "helper" column 'date_drit_result' but couldn't figure out how to do it - any thoughts there?
Relevant worksheets & fields:
survey worksheet | |||||
type | name | label | calculation | choice_filter | relevant |
date | date_drit_test | Date of DRIT test | |||
hidden | date_drit_result | Was there a DRIT test date? | if(string-length(${date_drit_test})>0, "test_date_present", "test_date_absent") | ||
select_one test_result | drit_test_result | DRIT test result | test_filter=${date_drit_result} | string-length(${date_drit_test})>0 | |
choices worksheet | |||||
list_name | name | label | test_filter | ||
test_result | INC | Inconclusive | test_date_present | ||
test_result | N | Negative | test_date_present | ||
test_result | P | Positive | test_date_present | ||
test_result | NT | Not Tested | test_date_absent | ||
test_result | RT | Retested | test_date_present | ||
test_result | F | Failed (PCR) | test_date_present |
I was having trouble getting @abureaux 's solution to work. I didn't want an extra field in my database, and I also wanted users to see the label, rather than the code, in the final field.
I found out that select_one questions with a choice filter will not automatically create a domain for you, so you must manually create the domain in AGOL if you want one. https://community.esri.com/t5/arcgis-survey123-questions/survey123-name-field-displaying-instead-of-...
Here is the database as shown from Survey123 Data tab - note labels appear, not codes.
XLSForm options to reproduce:
survey worksheet | |||||||||
type | name | label | appearance | required | default | calculation | choice_filter | bind::esri:fieldType | body::esri:visible |
date | date_drit_test | DRIT test date | |||||||
text | date_drit_result | DRIT date present | hidden | if(string-length(${date_drit_test})>0, "test_date_present", "test_date_absent") | null | ||||
select_one test_result | drit_test_result | DRIT test result | yes | NT | test_filter=${date_drit_result} | string-length(${date_drit_test})>0 | |||
choices worksheet | |||||||||
list_name | name | label | test_filter | owner_notes | |||||
test_result | I | Inconclusive | test_date_present | Unlike other choice lists in this survey, this choice list has a filter associated with it. | |||||
test_result | N | Negative | test_date_present | Therefore, you must create and update or edit the domain by hand to match this list in AGOL if the choice list changes. | |||||
test_result | P | Positive | test_date_present | See more here: | |||||
test_result | R | Retested | test_date_present | https://community.esri.com/t5/arcgis-survey123-questions/survey123-name-field-displaying-instead-of-... | |||||
test_result | U | Unfit to test | test_date_present | ||||||
test_result | NT | Not Tested | test_date_absent |
I was having trouble implementing @abureaux 's solution - I wanted the labels, not the codes, to appear in my data table (like other select_one questions) and I also didn't want to save a second field that would potentially confuse my users.
I came up with a fairly simple solution that does what I want. There is one caveat I describe below - would be great if anyone has an idea for how to get around it!
Please note: I had to add the domain manually in AGOL for this question. I went into the hosted feature layer overview, clicked "Data", clicked "Fields", clicked my drit_test_result field, and clicked "Create List" and entered the same values that are present in my choice list on my XLSForm. Apparently, when you use a choice filter, domains do not automatically get added to your feature layer like other select_one questions. See more, here: https://community.esri.com/t5/arcgis-survey123-questions/survey123-name-field-displaying-instead-of-...
So, finally:
If a user does not enter a drit_test_date, they do not see options to select a drit_test_result. They submit the form, and code "NT" (label "Not Tested") is recorded in the database. Here is what the form looks like:
If a user does enter a drit_test_date, they see options to select a drit_test_result. Unfortunately, the field is automatically flagged with red with a "required" message before they have a chance to click an option. I can live with this but I'd rather they get an error message after hitting submit.
Here is the data view (from Survey123 data tab) of both options, showing labels, not codes.
Here are the options I used in the survey and choices worksheets:
survey worksheet | |||||||||
type | name | label | appearance | required | default | calculation | choice_filter | bind::esri:fieldType | body::esri:visible |
date | date_drit_test | DRIT test date | |||||||
text | date_drit_result | DRIT date present | hidden | if(string-length(${date_drit_test})>0, "test_date_present", "test_date_absent") | null | ||||
select_one test_result | drit_test_result | DRIT test result | yes | NT | test_filter=${date_drit_result} | string-length(${date_drit_test})>0 | |||
choices worksheet | |||||||||
list_name | name | label | test_filter | owner_notes | |||||
test_result | I | Inconclusive | test_date_present | Unlike other choice lists in this survey, this choice list has a filter associated with it. | |||||
test_result | N | Negative | test_date_present | Therefore, you must create and update or edit the domain by hand to match this list in AGOL if the choice list changes. | |||||
test_result | P | Positive | test_date_present | See more here: | |||||
test_result | R | Retested | test_date_present | https://community.esri.com/t5/arcgis-survey123-questions/survey123-name-field-displaying-instead-of-... | |||||
test_result | U | Unfit to test | test_date_present | ||||||
test_result | NT | Not Tested | test_date_absent |