Select to view content in your preferred language

Using a choice filter and calculation for the same question

2019
11
01-19-2024 11:41 AM
EricaNova
Frequent Contributor

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):

EricaNova_0-1705692694415.png

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):

EricaNova_1-1705692876098.png

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     
typenamelabelcalculationchoice_filterrelevant
datedate_drit_testDate of DRIT test   
hiddendate_drit_resultWas there a DRIT test date?if(string-length(${date_drit_test})>0, "test_date_present", "test_date_absent")  
select_one test_resultdrit_test_resultDRIT test result test_filter=${date_drit_result}string-length(${date_drit_test})>0
      
choices worksheet     
list_namenamelabeltest_filter  
test_resultINCInconclusivetest_date_present  
test_resultNNegativetest_date_present  
test_resultPPositivetest_date_present  
test_resultNTNot Testedtest_date_absent  
test_resultRTRetestedtest_date_present  
test_resultFFailed (PCR)test_date_present  

 

 

0 Kudos
11 Replies
EricaNova
Frequent Contributor

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-...

 

Solution (with one caveat)

  • If a user does not select a date for date_drit_test, user does not see any choices for drit_test_result; the code "NT" will be entered into the database with associated label "Not Tested" when user presses submit:

EricaNova_0-1706626870508.png

 

  • If a user does select a date for date_drit_test, they will be provided with the list of appropriate choices.
  • The only disadvantage of this solution is that as soon as a user selects a date, the next question drit_test_result is highlighted in red and tells them the question is required. I would prefer they get that warning at the bottom of the form or after they try to press submit, but I couldn't figure out how to delay the required message. I'm all ears if anyone has ideas!

EricaNova_1-1706626925828.png

Here is the database as shown from Survey123 Data tab - note labels appear, not codes.

EricaNova_2-1706626959623.png

XLSForm options to reproduce:

survey worksheet         
typenamelabelappearancerequireddefaultcalculationchoice_filterbind::esri:fieldTypebody::esri:visible
datedate_drit_testDRIT test date       
textdate_drit_resultDRIT date presenthidden  if(string-length(${date_drit_test})>0, "test_date_present", "test_date_absent") null 
select_one test_resultdrit_test_resultDRIT test result yesNT test_filter=${date_drit_result} string-length(${date_drit_test})>0
          
choices worksheet         
list_namenamelabeltest_filterowner_notes     
test_resultIInconclusivetest_date_presentUnlike other choice lists in this survey, this choice list has a filter associated with it.   
test_resultNNegativetest_date_presentTherefore, you must create and update or edit the domain by hand to match this list in AGOL if the choice list changes. 
test_resultPPositivetest_date_presentSee more here:     
test_resultRRetestedtest_date_present https://community.esri.com/t5/arcgis-survey123-questions/survey123-name-field-displaying-instead-of-... 
test_resultUUnfit to testtest_date_present      
test_resultNTNot Testedtest_date_absent      
0 Kudos
EricaNova
Frequent Contributor

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:

EricaNova_0-1706632557056.png

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.

EricaNova_1-1706632641120.png

Here is the data view (from Survey123 data tab) of both options, showing labels, not codes.

EricaNova_3-1706632697144.png

Here are the options I used in the survey and choices worksheets:

survey worksheet         
typenamelabelappearancerequireddefaultcalculationchoice_filterbind::esri:fieldTypebody::esri:visible
datedate_drit_testDRIT test date       
textdate_drit_resultDRIT date presenthidden  if(string-length(${date_drit_test})>0, "test_date_present", "test_date_absent") null 
select_one test_resultdrit_test_resultDRIT test result yesNT test_filter=${date_drit_result} string-length(${date_drit_test})>0
          
choices worksheet         
list_namenamelabeltest_filterowner_notes     
test_resultIInconclusivetest_date_presentUnlike other choice lists in this survey, this choice list has a filter associated with it.   
test_resultNNegativetest_date_presentTherefore, you must create and update or edit the domain by hand to match this list in AGOL if the choice list changes. 
test_resultPPositivetest_date_presentSee more here:     
test_resultRRetestedtest_date_present  https://community.esri.com/t5/arcgis-survey123-questions/survey123-name-field-displaying-instead-of-... 
test_resultUUnfit to testtest_date_present      
test_resultNTNot Testedtest_date_absent