Defining program rules for multiselect questions in dhis2

Hii,

Following the introduction of ‘multi-select’ type option sets, how can these multiselect questions be referred to in a program rule expression?

I simply want to write a program rule expression that queries if the multiselect question (eg: Cities), has for example ‘Accra’ selected.

what function will be able to check if the multi-select field/data element contains a specific value?

Thanks

1 Like

Previously, I could access values of a data element with option sets (that only allowed select_one), as below in program rule expressions:

#{cities}='Accra'

and then the rule action follows.

but now these data elements store multiple values. For example the data element ‘Cities’ could store values: ‘Accra’, ‘Cape Town’, ‘Nairobi’.
Which I believe will be comma-seperated (not really sure).

So I’m looking through the d2 functions to see if there’s a d2 command as ‘contains’ or something similar that could tell if the multiselect value contains for example ‘accra’. But I’ve not found a supportive function for this yet.

I hope this comes out clearer now.

1 Like

Hi @Quoda

Great to see you posting, and thanks for the further clarification. Yes, you have clearly explained. I also need to search more and test on play. :pray:

Thanks!

Update:

@Quoda, I believe I found the answer to your question.

You can use d2:validatePattern to get the value from the multiselect option set. For example, d2:validatePattern(ProgramRuleVariable,'.*\\bAccra\\b.*') will look for the word in the string and if it’s true, it will return true.

Here’s a quick and short tutorial with more details:

I hope you like it! :grin:

Please mark as solved if it solves your question or feel free to ask for more details if it doesn’t work on your side.

Thank you!

2 Likes

Hi @Quoda

Just checking if you saw the update above. Thanks!

Hi @Gassim

I just tried it on a program we have with mutli options (this was a pending question on our side) and i confim your solution works for us.
Thanks for this.

1 Like

Great, thanks for the confirmation! :slight_smile:

I hope it works for @Quoda and everyone else too. :pray:

@Gassim I’m just seeing the response. This has escaped me, apologies!

Firstly, the community is thankful to you for making this video. I really appreciate your effort in putting this together. d2:validatePattern does the magic!

I’m trying this out to see how it solves my problem, I’ll surely come with a feedback.

Thank you once again!

1 Like

Thank You @Gassim ,

I tested your solution, and it works just fine …
But what about using “contains” or “containsItem”, i tried it and it didn’t work.
even its provided from the DHIS2 documentation, see below youtube video.

Thanks

Nice feature

1 Like

Hi

Thanks to you all (@Quoda , @mutali , and @ataallah )!

@ataallah , in the video you shared it’s mentioning for version 41 and the expression is for program indicators, so if you test in version 41+ in a program indicator the contains() or containsItems() and it’s not working, please create a new topic and I’ll test/triage. :pray:

Thanks again!

Thank you for your response.
version is: 2.41.2