But after defining the expression as above, I have a doubt whether this will work due to the single quotes I have used in it. I can clearly see that the parser will confuse with the beginning and closing quotes. Can someone assist me how I can achieve what I want.
Hi there @MSP,
The single quotes might be what causes you the problem, or the parenthesis. We have a unresolved bug in jira that might be relevant: [DHIS2-5593] - Jira
I am sure however that we should be able to help you achieve what you want without using d2:condition(), or a simpler d2:condition() call.
You can try removing the d2:hasValue(‘ovc_ra_hiv_risk_of_hiv’) part of the expression - it does not need to be there, as “Yes” will be the value. Then you can try using double quotes within the expression, like so: d2:condition(’#{ovc_ra_hiv_active_sex}=="Yes"’,1,0)
Otherwise you can do the same with a calculation and variables. If you make a program rule variable with the type set to “Calculated value” for each of the fields you want to sum: #{cal_ovc_ra_hiv_active_sex}
Then you can make an assign rule for each of these variables, assigning 1 of the corresponding variable contains ‘Yes’:
Priority: 1
Expression: #{ovc_ra_hiv_active_sex}==‘Yes’
Action: Assign 1 to variable #{cal_ovc_ra_hiv_active_sex}
Then sum up all the #{cal_variables: and assign to wherever you planned to assign in the first place. These will all be 1 if your condition is true.
@MSP, just as a side-note, we usually configure Yes/No questions with the numeric values 1 and 0 instead of text values ‘Yes’ and ‘No’, so that we can very easily calculate various results (eg simply sum the data element to get the number of ‘Yes’ responses). I realise this won’t help you with your current problem, as you’ve already configured the program, but it might be worth considering when designing future programs.
Using d2:condition(’#{ovc_ra_hiv_active_sex}==“Yes”’,1,0) : This one did not work. I got the error like:
Original condition was: d2:condition(‘#{ovc_ra_hiv_active_sex}==“Yes”’,1,0) - Evaluation ended up as:d2:condition(‘‘Yes’==“Yes”’,1,0) - error message:SyntaxError: missing )
I doubt whether the double quote can work here or not. Kindly correct if I have done anything wrong.
Using Calculate Values: I tried this for 2 of my data elements and it worked. I guess this could work for me. But the challange is I have 10 data elements to check. This means I should create 10 variables, define 20 rules (one to assign 1 for YES and another one to assign 0 for NO). This approach is not only tedious and timeconsuming but also adds more maintenance work for future changes. If the d2:condition function works, I will be doing a better job. Kindly see, if you can assist with d2:condition error message.
Otherwise, many thanks for sharing your knowledge and experience with me. That is wonderful