Can you set the case_id for a new case?


Is there a way to set the case_id for a new case?

I'm trying to do the following:

I have a case of type 'patient' that will have child cases of type 'lab' and 'consult'. It is possible to create a 'lab' case during the creation of a 'consult' case. It is also possible to create a 'lab' case independent of a 'consult' case. If a 'lab' case is created during the creation of a 'consult' case, I want to ensure they are linked by using case properties for each. For the 'lab' case, I would have a case property for linked_consult_case_id and for the 'consult' case, I would have a case property for linked_lab_case_id.

If I can generate the 'case_id' within the form that creates these cases, then I could use it to set the applicable case properties. I also realize I could just generate my own id for each case distinct from the actual 'case_id', but would rather just use the existing field if possible.

Is there a way to accomplish this? I believe it could be down with the advanced "Save to Case" feature, which I will request if that is the only way to accomplish what I described above.

There's a feature that is called "Child Cases" that addresses your need. Child Cases - CommCare Public - Global Site

the case ID field should not be configurable by applications for many reasons. you can always create your own case properties that have an ID of any sort you actually configure and refer to it in other cases but I think that the feature above will fit your needs perfectly

The use of child cases is not an option for what I'm trying to accomplish. Yes, "lab" cases and "consult" cases will be child cases of a "patient" case, but my question had to do with linking a "consult" case to a "lab" case, when both of these child cases (of a "patient" case) are being created with the same form submission. Given that a "lab" case can be created independent of a "consult" case, the parent case of both a "lab" case and "consult" case will always be a "patient" case. Thus the child case feature is not an option for linking a "lab" case to a "consult" case given the data structure.

Thank you @Mazz for the suggestion of an ID I can just configure myself. If the case ID is not configurable, then I agree that is the best way to accomplish this (or using the advanced "Save to Case" where I do believe it is possible to configure the case id).

Even if you don't create the case ID yourself, you should be able to reference it from within the form that creates the case. For instance, in "normal" case creation forms, the xpath /data/case/@case_id will give you the ID that will be used for the case. Since your form creates multiple cases, the exact path for each will be different, but it should still be available.

The easiest way to figure out what to use is probably by using the "data preview" widget in app preview. Check the "form data" tab, and I think you should see something like subcase_0/case/@case_id. You can also check the XML if that doesn't have it, though that's more complicated to follow.

1 Like


I was not aware you could access the "pending" case_id for a case that has yet to be created. I just tried it out using "data preview" to view the case id and it worked great. I'll set-up a hidden value to access it within the form. Thank you for your help!

For anyone that is looking to do something similar, here is some information for how the hidden value calculation would need to be structured to access the "pending" case id for the yet to be created case: CommCare Session Instance - CommCare Public - Global Site

See the "Session Data-- Referencing the Caseid inside Form Builder" section.