Save questions form the repeat group to the case properties

Hi folks,

I have a repeat group that registers contacts of a patient in a registration form but Iam unable to save questions in this repeat group as case properties. Iam able to save as case properties only the questions that are not in the repeat group. I need help on this issue.

Thanks in advance

Hi!

This is not really an "issue" or unexpected behavior.

Cases are singular items. or, if you want to look at them that way, they are a database row. and as i'm sure you are aware, you can only store one value per row for each column (property)

So, if you have a repeat group and you're collecting an answer to a question inside it such as "what's your favorite color?" you're going to get a color, multiple times. as many times as the group question is repeated. so, if you were to select that question and tried to store the answer as a case property, there's no way for the system to know which property you're after. or if it's the average of all the answers, a long string of all the answers, or whatever other operation it needs to do to come up with one singular value that it can store as a case property.

you have 2 options:

1- use a join or similar function to concatenate all those answers into one long string, or use an average function, but do that outside the repeat group after the group is finished. this way, you'll have a hidden value with all the answers to that question in the repeat group, and you can store that against this case. you will need to read the following links before you can accomplish this
Logic outside repeat groups:
https://confluence.dimagi.com/display/commcarepublic/Using+Repeat+Groups+in+the+Form+Builder#UsingRepeatGroupsintheFormBuilder-LogicOutsideTheRepeatGroup
Commcare string functions
https://confluence.dimagi.com/display/commcarepublic/CommCare+Functions#CommCareFunctions-StringFunctions

2- you want to store all the answers to that question in the repeat group separate from each other. for this, you will need to create child cases. these can be of the same type as the case you're trying to update, or they could be different cases. for more information on that, please look at this link Child Cases - CommCare Public - CommCare Public
This will allow you to create a new child case with the current case as its parent, for an many times as that repeat group repeated. you can also use attributes from the parent case and copy them across to the child cases too.

1 Like

Thank you,Mazz, for your clear explanation. I will read the links you have shared.

This is helpful @Mazz, thanks! The join will work fine for some kind of cases.

But, I find it tricky for my case (Agriculture):

We are trying to get land coverage of crops, and a forecast of four weeks for crops. It goes like this:

This module will be treated as a child case of a parent farmer.

  • A farmer will be selected.
  • Crops on the land will be selected
  • A repeat group will ask the land coverage of each crop, joined, and saved to a property ( would have been even better to have a separate child case for each crop, rather than comma separated joint)
  • On a second repeat group, a forecast of four weeks ( four separate questions for each week) of every crop will be recorded; you might see, this will be where joining will throw an error.

I am looking into it, do you have idea on how to proceed with this - joining the weeks for each crop?

Please, look the form below:

ok so what you're trying to do is to join the values in the questions week1 week2 and week 3?
the join function expects either a collection of nodes to join with a string, or a bunch of different strings as arguments "more than 2" where the first argument will be considered as the string to join with.

in your case, if you're trying to join the weeks numerical value, you'll likely have more luck using the string(value) to cast them as strings that the join function can understand. but the syntax is different for this type of join.

if you want to forecast for each crop, why don't you just put the forecast group into the land coverage loop group? this way, you'll be asking about the land coverage as well as the schedule at the same time.

if that's not how you want to do it, then concatenate(string(week1)," ",string(week2)," ",string(week3)) will put those values right next to each other with spaces between them. so will join(" ", string(week1),string(week2),string(week3))

you want them to be space separated if you want to use them in repeat groups later on "the selected-at function needs them to be space separated" you can also use the function count-selected strings that are space separated

Hope this helps! good luck!

Mazz

Thanks! Concatenating week forecast values in the forecast group has solved the problem.