Text field to record default answer if left blank

1261
6
02-15-2022 12:43 PM
ryanEvanczyk
Occasional Contributor

I have a survey with a select_multiple that queues different text fields based on their relevance. When these fields are relevant, I want them to remain blank. They are required fields and will be completed.

However, if the field is not relevant, I want the survey to record a default answer and NOT leave the field with a 'null' response.

if((string-length(${textfield})=0),'text',.) doesn't work - any other ideas? is my syntax correct? Do I need a different formula or approach?

Thanks!

RyanE

0 Kudos
6 Replies
PaulPetersen1
Occasional Contributor

You might be able to do this through temporary schema (bind::esri:fieldType = null). Write the default value to a first (hidden) temporary field...so it always holds the default value .  And then have the second temporary field shown (and required) when relevant. Then in your third field (the permanent, "final" field) write a calculation expression to populate it with the value of the first temporary field if the second temporary field is null, else if the second field is not null then populate with the second field. Something like, 

if(${temp_field2} = '',${temp_field1}, ${temp_field2})

 

0 Kudos
KayleeRivera
Esri Contributor

If a question is not relevant, then Survey123 will always remove any value from the question. I would recommend using the body::esri:visible column, instead of relevant. This column works similar to relevant, except it does not clear out responses. 

Once your questions are setup using body::esri:visible, you'll also need an if statement in your default column. For example, your body::esri:visible value would be something like: string-length(${textfield})=0. Then the default column would be: if(string-length(${textfield})=0,'text',.).

Hope this helps!

JessicaShue
New Contributor II

Hi Kaylee,

   I'm hoping to apply your suggestion to a select_multiple field, but I'm not having success with what you've suggested. I read here that string-length will work on any field type, so I'm not sure what I'm missing.

I'd like a select_one list to show if certain choices are selected in the select_multiple field, but if those options aren't selected, I'd like a default value to be automatically entered. Basically, a '0' for 'absent'.

I added a string-length(${select_multiple})='choice' in the body::esri:visible column and if(string-length(${select_multiple}}='choice', ., 0) in the default column of the select_one field. Right now this doesn't show the select_single column if I do select that specific 'choice', nor does it default to 0.

Thanks for any help/insights you may be able to share!

Jess

0 Kudos
KayleeRivera
Esri Contributor

Hi Jess,

I might need to see the XLS Form to get a better understanding of what you've got going on, but there are a couple of things you can check if your formulas aren't working out here.

The string-length function is going to output the number of characters in an answer. It looks like your formula of string-length(${select_multiple})='choice' would never evaluate to true, since it's comparing a number to a text string.

So if I'm understanding correctly, you would want a formula in your select_one field's body::esri:visible column that looks something like: if(selected(${select_multiple},'choice1') or selected(${select_multiple},'choice2') or selected(${select_multiple},'choice3'), true, false). This would make it so that your select_one question only shows if choice1, choice2, or choice3 are selected in your select_multiple question. (Note: please remember to use the "name", as opposed to the "label" of your choices for these calculations)

Then you can use a similar formula in the default column for your select_one question to populate a 0 if the select_multiple doesn't meet your critera. Something like: if(selected(${select_multiple},'choice1') or selected(${select_multiple},'choice2') or selected(${select_multiple},'choice3'), "", 0).

0 Kudos
JessicaShue
New Contributor II

Thank you for the suggestion! I had tried using a select expression, but not within an if statement in the body::esri:visible field.

I now have the select_one field appearing when the option is selected. But the default value when the choice is not selected is showing as the formula of the default field. This is a snapshot for the select_one field:

typenamelabeldefaultbody::esri:visible
select_one list_name_cover_ALPEcover_ALPEALPE cover if(selected(${spcode},'ALPE'), "", 0)if(selected(${spcode},'ALPE'),true,false)
0 Kudos
KayleeRivera
Esri Contributor

Hi @JessicaShue,

The select_one fields are string field by default, so you'd likely need to amend your calculation to wrap your 0 in quotes. Something like: if(selected(${spcode},'ALPE'), '', '0')