Stream: shorthand
Topic: Questionnaire EnableWhen
Marieke Span (Feb 21 2022 at 15:32):
Does any of you know how to insert multiple EnableWhen elements in a questionnaire in FSH?
In a questionnaire I want to add 2 enablewhen elements. But when I insert two constrains by using EnableWhen, only the last one is set as a constraint. To make it clearer, here’s a simplified example of what I try to do:
I have 2 attributes:
Seasonality: In season, not in season
Fruit type: Apple, Pear, Lemon, Strawberry, Pineapple
Some questions need to be enabled based on both attributes. E.g. :
Question 1:
Enable when "in season" and "apple, pear or lime"
I have tried the following type of code:
* enableWhen
* question = "Seasonality"
* operator = #=
* answerCoding = SeasonalityCodeSystem#0 // SeasonalityCodeSystem#0 = “in season”
* enableWhen
* question = "Fruit-Type"
* operator = #=
* answerCoding = FruittypeCodeSystem#3 // FruittypeCodeSystem#3 = “apple”
* enableBehavior = #all
In this case I only used the FruitTypecode for apple but I want to use a list of codes (so with the codes for pear and lime), but I don’t know how. This also doesn't work since it only uses the fruittype = apple as a constraints.
Chris Moesel (Feb 21 2022 at 16:53):
Hi @Marieke Span. Since enableWhen
is multi-cardinality (e.g., 0..*
or an array), then you need to specify indices for each item. In FHIR Shorthand, lists start at index 0
(an approach familiar to computer scientists, but perhaps less familiar to others). So... you could do what you want like this:
* enableWhen[0]
* question = "Seasonality"
* operator = #=
* answerCoding = SeasonalityCodeSystem#0 // SeasonalityCodeSystem#0 = “in season”
* enableWhen[1]
* question = "Fruit-Type"
* operator = #=
* answerCoding = FruittypeCodeSystem#3 // FruittypeCodeSystem#3 = “apple”
* enableBehavior = #all
The reason for the behavior you were seeing is that FSH defaults the index to 0
if no index is provided. So the first rule with enableWhen
was interpreted as enableWhen[0]
, but then when you wrote another rule with enableWhen
, it was also interpreted as enableWhen[0]
(overwriting the same element in the list).
Chris Moesel (Feb 21 2022 at 16:56):
BUT... FSH also provides a simpler syntax for working with arrays so that you don't need to keep track of the numbers. In this syntax, you use [+]
to indicate you want to increment to the next index in the list. If you use [=]
, then that means you want to stay at the current index in the list. The first time you use [+]
, it will be interpreted as [0]
(if you didn't add any items to the list before that).
So you also could have written your FSH like this:
* enableWhen[+]
* question = "Seasonality"
* operator = #=
* answerCoding = SeasonalityCodeSystem#0 // SeasonalityCodeSystem#0 = “in season”
* enableWhen[+]
* question = "Fruit-Type"
* operator = #=
* answerCoding = FruittypeCodeSystem#3 // FruittypeCodeSystem#3 = “apple”
* enableBehavior = #all
This is all documented in the FSH spec starting here: http://hl7.org/fhir/uv/shorthand/reference.html#array-paths-using-numerical-indices
Marieke Span (Feb 22 2022 at 08:55):
Thank you so much, that was very helpful and it makes total sense! I can now fin-fish my questionnaires!
Last updated: Apr 12 2022 at 19:14 UTC