CommCare Session Instance
Often it is useful to have access to certain system data, such as username, case id, or the unique device id, from within form entry. This can be achieved via the CommCare session instance.
Device Data
Type | Path | Details |
---|---|---|
device id | instance('commcaresession')/session/context/deviceid | The unique ID for the entering device |
username | instance('commcaresession')/session/context/username | The username of the user entering data |
user id | instance('commcaresession')/session/context/userid | The GUID of the user entering data |
app version | instance('commcaresession')/session/context/appversion | The identifier for the software application version performing form entry |
Note: the userid is not the same as the the commcare-user case id. In order to access the commcare-user case id, use this expression:
instance('casedb')/casedb/case[@case_type='commcare-user'][hq_user_id=instance('commcaresession')/session/context/userid]/@case_id
Session Data-- Referencing the Caseid inside Form Builder
As you navigate through your mobile app to enter form entry, you will sometimes select cases. The ID of those cases are available in commcare session instance.
In normal workflows you only select one case. In such settings the case_id can be accessed using the instance('commcaresession')/session/data/case_id path. If you're referencing the case id in a form that created the case, you have to reference it as instance('commcaresession')/session/data/case_id_new_[case_type]_[n]. N indicates where that case falls in the order of cases being created in that form (is 0-indexed). For example, if your form is just creating one case of the one client case type, this will be: instance('commcaresession')/session/data/case_id_new_client_0. However, if your form is creating multiple cases, that last number will increment. You can find out the exact syntax by looking at the suite file.
If you are in a more advanced workflow (via advanced modules) you will be prompted for the 'case tag' of the case being loaded on HQ. If you set the case tag to 'load_mother' for example, then in the CommCare session instance you can access that case's id using the path instance('commcaresession')/session/data/case_id_load_mother
Custom User Data
It is possible to attach custom data to users in CommCare and access that information within form entry. Details found at the 'Custom User Data' page.
There are several default values that are provided along with the user data:
Data | Path | Format |
---|---|---|
User's first name | instance('commcaresession')/session/user/data/commcare_first_name | |
User's last name | instance('commcaresession')/session/user/data/commcare_last_name | |
User's phone number | instance('commcaresession')/session/user/data/commcare_phone_number | |
User's primary location | instance('commcaresession')/session/user/data/commcare_location_id | |
User's locations | instance('commcaresession')/session/user/data/commcare_location_ids | space-separated list |
User type | instance('commcaresession')/session/user/data/commcare_user_type | "web" or "commcare" |