Auto-fill only if a condition is met

Dear forum,

I am trying to find a way of getting a question to be auto-completed ONLY if the answer to a previous question was ‘Yes’. If the answer was ‘No’, then one needs to enter the value manually.

The situation is a very common one: after asking the name of the person that is completing the form (say person1), I ask if this person (person1) is filling the form in her/his/they own name. Then if Yes, the following question should be autocompleted with the person1’s name. Otherwise, person1 should enter the name of the person she/he/they is/are completing the form for (say person2), which could be anything (meaning, there is no list where to choose from).

I tried a couple of options combining calculate and IF-THEN in calculation, and combining a text question with constraints but didn´t succedd in it. I also navigated the forum and tried a couple of things proposed, but they did not apply because most cases where IF-ELSE-THEN situations. A key feature here is that I want the name of the person in whose name the form is being filled (being person1 or person2) always present in my data. Otherwise, I could use the ‘relevant’ parameter to ask for the person2’s name, ONLY when person1 is different thatn person2. I hope I’m being clear!

Since I think this is probably a common type of situation, I though asking for your help.

Many thanks in advance!
Solange

Can you share the xlsform with what you’ve tried? Using if, calculation and relevant seems like a way it could be done, so by showing what you already have we may be able to help spotting the error.

1 Like

Are you using Collect or Enketo (Webgorm), please?

Hi Nmambre,

I actually deleted the things I tried from my form because they didn’t work. I will try te replicate them (as you may imagine, it was not only one thing and I tried different things for a couple of days) and provide you with some toy forms.

Thanks!
Solange

Hi Wroos,

We are using Enketo.

Thank you!
Solange

@sfermepin, maybe you could do it as outlined in the image shared below:

In the survey tab of your XLSForm:

image

Data entry screen as seen in Enkekto: when condition is met

Data entry screen as seen in Enkekto: when condition is not met

Reference XLSForm:

Prefill with If Statement.xlsx (11.3 KB)

Hi Kal_Lam,

Thank you very much! I was not aware of the possibility of using calculation in a text question. It worked perfectly.

Many thanks!
Solange

1 Like

@sfermepin, :clap: :heart: :partying_face:

Hint: This solution will always (re)set the name to empty, if Q2 is not Yes (1). No manual entry will work here.

Other solutions might be using:
- relevant: Only ask for head name if Q2 is No.
- trigger
- once( ) function (but update problem)

As Enketo does not yet allow dynamic read_only, it might be difficult to directly block editing the pre-set head name. But reset by calculation could work.

Some hints can be found in the documentation
- https://docs.getodk.org/form-logic/#dynamic-defaults-from-form-data
- https://docs.getodk.org/form-logic/#triggering-calculations-on-value-change.

I think the requirement here is:

  • If the respondent (Q1) is the head of HH (Q2 is Yes), her/his name is taken for the head name (Q3). And cannot be changed there (in Q3).
  • Otherwise, the name for the head has to be entered in this field (Q3).
  • Moving back to Q1 or Q2 and changing the referenced answers must update (reset/empty) the name for the head.

@sfermepin, please, explain more.

Hi wroos,

Thank you for getting back to this!

I am not sure I understand when you say that no manual entry will be allowed with the solution Kal_Lam proposed. When I played a bit with the form using that solution and put Q2=‘No’, I was able to enter the name of Q3 manually (please see the data related to the form in the attached screenshot; sorry it’s in Spanish).

The other soutions you proposed are ok (relevant and trigger; the once() function I don’t know), and are the ones that I had in mind in case there was no way of auto-filling Q3 if Q2=Yes. But as I said in my post, I would like for Q3 to be filled always, even when it is not relevant or triggered.

So for now, I think the other solution is my best option. What I didn’t realize is that when Q2=‘Yes’, Q3 defaults to Q1, but can be edited in the form. Ideally, it shouldn’t, so thank you for pointing that out. As you say, a dynamic read_only would be necessary here.

The requirement you describe is correct. I think the solution proposed by Kal_Lam deals fine with requirements 2 and 3, and the only thing that it is not addressed in requirement 1 is the fact that Q3 should be read_only when Q2=Yes.

I think I might me missing something here. I am not an expert in Kobo/ODK, so I may be missunderstanding you. Please let me know if that is the case. In any case, I will explore the documentation you shared with me.

Thank you very much!
Best,
Solange