ODK Validate Errors - if statement

1. What is the problem? Be very detailed.

I am receiving the following error:

OKD Validate Errors:

XForm is invalid. See above for the errors.
org.javarosa.xform.parse.XFormParseException: invaide calculate for the bind attached to "${s_2}": Bad node: org.javarosa.xpath.parser.ast.ASTNodeAbstractExpr@567d299b in expression....

Error only happens when I move "pulldata" out of "if" statement and call on the "pulldata" from referenced field. If i put "pulldata" in "if" statement, there is no issue with validation, however this does not meet my needs.

The problem "if" statement is emboldened below:

2. What app or server are you using and on what device and operating system? Include version numbers.

XLSform, win10

3. What you have you tried to fix the problem?

-nesting the if statements, but it doesn't accomplish what I need.
-moving the pulldata rows above the if calculations
-creating a new row for each "if" (this works, but seems untidy and silly)
-changing the "type" to "hidden" and "decimal" rather than "calculate"
-putting many things in and out of parenthesis

4. What steps can we take to reproduce the problem?

set up a calculate type field by referencing another calculate rather than typing out the "pulldata" in the "if" statement

5. Anything else we should know or have? If you have a test form or screenshots or logs, attach below.

Nope!

Following up!

If anyone else runs into this type of error -

I was able to find a way around this by creating a new choice that contained a list and referencing that list.

if(${practice}=${grassedpractice},${s_gp},"")

I'm marking as solved

The syntax of your if(...) statement is incorrect. Instead of:

if($practice}="Conservation Cover (317)(acres)", ${s_gps},0) if(${practice}="Critical Area Planting (342)(acres)", ${s_gps}, 0)

you should probably have:

if($practice}="Conservation Cover (317)(acres)", ${s_gps}, if(${practice}="Critical Area Planting (342)(acres)", ${s_gps}, 0))

Specifically, the second if(...) statement needs to constitute the false component of the first if(...) statement. That's how to implement an "if (a) then foo else if (b) then bar else baz", which I presume is your intention...

1 Like

I'm kicking myself at how obvious and how much simpler this was.

While my solution had worked, this is far more effective for what I'm doing.

Thank you so much, Xiphware.

Note, in your specific example, since ${s_gps} is the result in both cases, you can in fact simplify your expression even further to:

if($practice}="Conservation Cover (317)(acres) or ${practice}="Critical Area Planting (342)(acres)", ${s_gps}, 0)

[I didnt mention it before 'cause knowing how to properly write a nested-if was more important :slight_smile: ]

I'm looking forward to rewriting much of these calculations ha!

I will definitely use the latter. Thanks for saving me some embarrassment when I share it with my colleagues!