Populate Select One Field with Contact Parent Hierarchy / Ancestor Places

Populate Select One Field with Contact Parent Hierarchy / Ancestor Places

Hi @antony @binod

We are facing an issue while implementing a form in CHT and would like to get some guidance on the recommended approach.

Requirement

We need to populate a select_one field with the parent hierarchy / ancestor places of the current contact as selectable options.

Example Hierarchy

State → District → District Hospital → Community Health Center → Primary Health Center → Health & Wellness Center → Village

Use Case

If a contact belongs to a lower-level place (for example, Village or HWC), we need the form to dynamically display its ancestor hierarchy places (such as PHC, CHC, District Hospital, etc.) as options in a select_one field.

Clarification Required

  • Is there any recommended way in CHT/XLSForm to fetch and populate ancestor places dynamically?

  • Can this be achieved using contact.parent, lineage, or any existing helper functions?

  • Are there any sample implementations or best practices available for this use case?

Any guidance or reference implementation would be helpful.

Thanks!

@Ramu

You can get the parent name and grandparent names of the hierarchy for offline users like this:

type name label calculate
calculate parent_name Parent Name ../inputs/contact/parent/name
calculate grandparent_name Grandparent Name ../inputs/contact/parent/parent/name

Place names from the user’s own hierarchy (facility, ward, municipality, district, etc.) can be prepopulated dynamically via inputs/contact, inputs/user, or contact-summary context. However, places outside the user’s replicated hierarchy can’t be fetched offline. The only way I can see to make those available would be to add them in the xlsx as choices.

Curious why this requirement came up though. The reports already contain hierarchical information that can be extracted, or passed through via contact-summary. Is there a case where a user registered under a lower-level HWC or village might actually belong to a different place than the hierarchy they were created under?

Hi @Prajwol,

Thanks for the suggestion. We tried implementing it using the following calculations:

calculate patient_parent_name ../inputs/contact/parent/name
calculate patient_grandparent_name ../inputs/contact/parent/parent/name

The parent name is being populated correctly, and we’re able to display it in the form. However, the grandparent value is coming empty.

We also tested it using notes:

  • Household: ${patient_parent_name}

  • Village/Ward: ${patient_grandparent_name}

But Only the parent value is resolving successfully.

Could you please let us know if we are missing anything in the implementation, or if there are any limitations/requirements for accessing deeper hierarchy levels like grandparent?

Thanks!

Hi @Prajwol

My findings were wrong yesterday. It was not showing the first parent of the patient.

It displays the patient’s parent hierarchy only up to the level of the logged-in user:

  • If logged in as ASHA (Village Login User) → only Village is shown

  • If logged in as CHO (HWC Login User) → Village and HWC are shown

  • If logged in as Physician (PHC Login User) → Village, HWC, and PHC are shown

@Ramu
This behavior you’re describing is the expected and by-design behavior of the CHT hierarchy system. A logged-in user will only see the parent hierarchy levels at or below their own placement in the hierarchy. We can connect tomorrow to talk about this. Feel free to setup a meeting.