Community Health Toolkit

Pre-select yes/no question

I have a yes/no question and would like to pre-select it with value “Yes” based on some condition. I am aware that I can use the default column to pre-populate the value but that’s not what I want to do. I want to select “Yes” value based on a condition.

Here’s the condition: if(${previous_med}!= '',${received_med} = yes,${received_med} = no)

${received_med} is the Yes/No question.

I have tried to add this condition in the calculate column but that did not help. Any other ideas?

1 Like

I believe calculate can be used to set a default value, but if you do so - the value cannot be changed throught he UI. It is “stuck” on the value you calculate as default because the calculated value is re-assigned infinitely. With the upgrade to Enketo core scheduled in 3.12, we should get access to more of the standard ODK functions including the use of once. That will allow you to do set a calculate value of once(if(${previous_med}!= '',${received_med} = yes,${received_med} = no)) which should hopefully solve this.

Until that is released, I’ve seen this workaround used:

  1. Create a calculate field called myDefault with desired default value. So in your case: if(${previous_med}!= '',${received_med} = yes,${received_med} = no)
  2. Create the select_one field called myValue with calculate if(${once}=’’,${myDefault},${myValue})
  3. Create a calculate field called once with a calculated value of true. This field needs to appear after the text field.

This solution relies on the order of execution of the Enketo library - so ${once} is empty the first time that myValue is calculated and then it is true in subsequent calculations. Hopefully this can give you the behavior you need until the enketo bump comes in 3.12. Sorry it is hacky and gross.

1 Like