Prefilling the variable based on previous variable

Please help me write a logic to prefill variable depending on the previous variable. Let me give an example

I have a variable for example Q1. What are the mobile brands you are aware of?
options are 1. Nokia
2. Samsung
3. Huawei
4. Apple
5. Vivo
6. Oppo

and the Q10. Are you aware of brand Nokia?
options are 1. Yes
2. No

I want the Q10 to be default (not user entry) "Yes" if the user has selected "Nokia" in Q1. Please consider Q1 is a multiple select variable.

Hi @Kiran
I have a sample form that should help you solve your problem: choice-name.xlsx (9.2 KB)
It contains everything you need but if there is something you don't understand please ask.

Hi @Grzesiek2010

Thank you for ur time and reply. I am attaching a sample form for better understanding. Here in this I need to write logic for 3 scenarios:

  1. Start time and endtime (Timestamp should be the last question recorded in the form for endtime, not the submission time)
  2. Q503.1/503.2/503.3 in any of these variables, if "Customer Interaction Meeting (CIM)" is selected, Q506 should be prefilled as "Yes" by default.
  3. For example if "Helpline-1912" option is selected in Q503.1, Interviewer should not be allowed to select "Helpline-1912" in Q503.2 and Q503.3

Please refer to the sample form attached for your reference

Prefill_Example_Form.xls (79 KB)

To my knowledge, pre-filling an answer like that is not possible. You can certainly add strings to the label or hint that say a message like Based on previous questions, this should be "yes". You can also include a constraint that says it must be "yes" based on previous questions. You could also consider skipping the question (relevant column) and in the data cleaning phase to add a "yes" to that column.

This should be handled with a constraint, e.g.

if(
selected(${Q503_1}, "Helpline-1912"),
not(selected(., "Helpline-1912")),
1
)
2 Likes

You can pre-fill answers in Collect v1.24 and later. See https://docs.getodk.org/form-logic/#dynamic-defaults-from-form-data for an example

2 Likes

I stand corrected! That is great!

@yanokwa is right and the formula in calculation (for q506) would be like
if(${q503_1}='6' or ${q503_2}='6' or ${q503_3}='6', ‘1’, ‘2’)

1 Like

Hi @jpringle

Thank you for ur time. "Helpline-1912" is just an example. Any options selected in Q503_1 should not be reflected or selected by interviewer in the following questions of Q503_2 and Q503_3. Please help me write a dynamic logic.

First: have you considered a "rank" widget? See https://docs.getodk.org/form-question-types/#rank-widget

It would show all the choice options. You could give instructions to your interviewer to only worry about the top three.

Now, to answer your question about what constraints to add:

  • 503_1 needs no constraint.
  • 503_2 checks that it is not equal to 503_1. The constraint should be: . != ${q503_1}
  • 503_3 checks that it is not equal to 503_1 or 503_2. The constraint should be: (. != ${q503_1}) and (. != ${q503_2})
1 Like

I agree with @yanokwa that this seems impossible. I see it differently though; There just no need to ask for the the brand "Q10. Are you aware of brand Nokia?" if one already responded to "Q1. What are the mobile brands you are aware of?" with 1. Nokia. Instead Q10 is skipped as irrelevant using a relevance condition eg. "not(selected(${q1},'1'))". In your data management software, then (eg stata), include a line in the do file to replace q10= 1 if q10==. [ie replace q10 as Yes if missing]. If you are managing the data in excel, filter out the blanks in q10 and select all these blank cells; type 1 and press control enter or do a similar thing for your other favorite data management software