# Can you set the case\_id for a new case?

**URL:** <https://forum.dimagi.com/t/can-you-set-the-case-id-for-a-new-case/9455>\
**Category:** Users\
**Created:** [November 18, 2022, 5:14pm UTC](https://forum.dimagi.com/t/can-you-set-the-case-id-for-a-new-case/9455 "2022-11-18T17:14:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![achavez812](https://dimagi.b-cdn.net/letter_avatar_proxy/v4/letter/a/58956e/32.png) [@achavez812](https://forum.dimagi.com/u/achavez812)\
**Post date:** [November 18, 2022, 5:14pm UTC](https://forum.dimagi.com/t/can-you-set-the-case-id-for-a-new-case/9455/1 "2022-11-18T17:14:35Z")

</div>

Hello,

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.

---

<div class="post-metadata">

**Author:** ![Mazz](https://dimagi.b-cdn.net/user_avatar/forum.dimagi.com/mazz/32/1004_2.png) [@Mazz](https://forum.dimagi.com/u/Mazz)\
**Post date:** [November 21, 2022, 1:57pm UTC](https://forum.dimagi.com/t/can-you-set-the-case-id-for-a-new-case/9455/2 "2022-11-21T13:57:43Z")

</div>

There's a feature that is called "Child Cases" that addresses your need. [Child Cases - CommCare Public - CommCare Public](https://confluence.dimagi.com/display/commcarepublic/Child+Cases)

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

---

<div class="post-metadata">

**Author:** ![achavez812](https://dimagi.b-cdn.net/letter_avatar_proxy/v4/letter/a/58956e/32.png) [@achavez812](https://forum.dimagi.com/u/achavez812)\
**Post date:** [November 21, 2022, 2:23pm UTC](https://forum.dimagi.com/t/can-you-set-the-case-id-for-a-new-case/9455/3 "2022-11-21T14:23:58Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Ethan\_Soergel](https://dimagi.b-cdn.net/user_avatar/forum.dimagi.com/ethan_soergel/32/543_2.png) [@Ethan\_Soergel](https://forum.dimagi.com/u/Ethan_Soergel)\
**Post date:** [November 21, 2022, 2:56pm UTC](https://forum.dimagi.com/t/can-you-set-the-case-id-for-a-new-case/9455/4 "2022-11-21T14:56:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![achavez812](https://dimagi.b-cdn.net/letter_avatar_proxy/v4/letter/a/58956e/32.png) [@achavez812](https://forum.dimagi.com/u/achavez812)\
**Post date:** [November 21, 2022, 3:28pm UTC](https://forum.dimagi.com/t/can-you-set-the-case-id-for-a-new-case/9455/5 "2022-11-21T15:28:51Z")

</div>

@Ethan_Soergel

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!

---

<div class="post-metadata">

**Author:** ![achavez812](https://dimagi.b-cdn.net/letter_avatar_proxy/v4/letter/a/58956e/32.png) [@achavez812](https://forum.dimagi.com/u/achavez812)\
**Post date:** [November 21, 2022, 7:34pm UTC](https://forum.dimagi.com/t/can-you-set-the-case-id-for-a-new-case/9455/6 "2022-11-21T19:34:57Z")

</div>

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 - CommCare Public](https://confluence.dimagi.com/display/commcarepublic/CommCare+Session+Instance#CommCareSessionInstance-SessionData--ReferencingtheCaseidinsideFormBuilder)

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