If someone can suggest a better title for my question, please feel free to...
I have a problem with the calculation of a field (or with it's result, I don't know):
I have four fields. Not all of them have to be filled but the total has to be 100. To check for this constraint I added a new field (as a text field, also tried as integer field) that adds up the values of these 4 fields (in the calculate attribute):
number(${landuse_share_1})+number(${landuse_share_2})+number(${landuse_share_3})+number(${landuse_share_4})
and writes it into the new field ${total}. In the constraint field I say ${total}=100 (also tried ${total}="100").
Now in Connect all of my tests worked well but when I try the same on a tablet or my phone (both Android) it seems like the calculation is not performed. Neither is my text/integer field filled nor does the survey accept my values but reminds me that the total has to be 100. The same with Survey123 on my windows computer.
In Connect it shows me straight away a 0 in that field (when Integer), where I expect my total to appear. In the apps, there is just nothing.
Any idea?
So, I think I see now where the problem is.
When the input fields are set as text everything works fine, also when not all fields are filled.
When the input fields are set as integers (which would make sense in this context), it is working as long as all fields are filled, but not any more when some are left empty. The problem is the way an empty integer field is saved (NaN) in contrast to an empty text field being just "".
In connect it seems to be working also with Integer fields, but not in the app.
Since I want the people only to enter numbers I did a workaround now with additional fields checking first if the fields are not empty and calculating then the result of those check fields:
type | name | label | calculation |
integer | landuse_share_1 | landuse_share_1 | |
integer | landuse_share_2 | landuse_share_2 | |
integer | landuse_share_3 | landuse_share_3 | |
integer | landuse_share_4 | landuse_share_4 | |
hidden | lu1_txt | lu1_txt | if(${landuse_share_1}>0,${landuse_share_1},0) |
hidden | lu2_txt | lu2_txt | if(${landuse_share_2}>0,${landuse_share_2},0) |
hidden | lu3_txt | lu3_txt | if(${landuse_share_3}>0,${landuse_share_3},0) |
hidden | lu4_txt | lu4_txt | if(${landuse_share_4}>0,${landuse_share_4},0) |
note | show_text | All_values | ${lu1_txt} +", "+${lu2_txt}+", "+${lu3_txt}+", "+${lu4_txt} |
integer | total | total | number(${lu1_txt})+number(${lu2_txt})+number(${lu3_txt})+number(${lu4_txt}) |
Maybe this helps also someone else with a similar problem...
That is an interesting behaviour. I would expect the sum to ignore a null/empty field when a sum is done. You could also set a default value of zero as a potential workaround.
That’s in fact also a good idea Weng!