Trouble with if statements in Survery123. I want to pulldata from a javascript, but when I go back to previouse responses they are overwritten.

466
1
08-19-2021 02:16 PM
by Anonymous User
Not applicable

I am trying to run an if statement in the calculation box of my xlsForm, it currently looks like this:

if(${case_number}>=0,${case_number}, pulldata("@javascript","myFunctions.js","getMax","bid",pulldata("@property","token")))

The second part of the calculation calls a function that finds the maximum value of previous results, and adds one, creating the next, new case number.

The second part of the calculation, starting from pulldata, has worked as I would expect it too, however when I look at the dashboard that is bringing in the survey, the values are overwritten to show the next highest case number.

I wrote the if statement in the same row in the XLSForm as the function as shown below.

BenjaminLutz1_0-1629407359116.png

I want the calculation to determine whether the field already has a value or not, if it does, I want to keep it, and if it does not, I would like to run the pulldata calculation.

The odd thing was that this exact calculation worked when I moved it to a different field.

The error it is giving me says,:

b'ODKValidate Errors:\n>>Something broke the parser. See above for a hint.\nDependencycycles amongst the xpath expressions in relevant/calculate\n\nResult: Invalid'

0 Kudos
1 Reply
DougBrowning
MVP Esteemed Contributor

You cannot calc a field to itself since it causes a logic loop. It is a common ask but it cannot be done if you think about it.  

Usually with these you create 3 fields.  One for the number, one for pulldata, and the 3rd to pick the correct value to send to the service.  You can set the first to to bind null so that they are not in the service if you like.

Hope that helps and makes sense.

0 Kudos