Getting data for another "person" inside create person form

Hello everyone,

Recently our team was tasked with creating a field in the “person-create” form which would allow choosing another person as “mother” of the contact being created. In order to accomplish this, we attempted two approaches, one was to use db-object in the appearance field, which would allow searching through all contact types and the other was a more preferable option, which was to use contact_selector.

Note: In this implementation, the person contact is name “c82_person”

In our first approach, we could select the data and it would appear correctly but it shows all contacts even wards, districts, municipalities, etc. With the second approach, it showed a select field in the form and had an issue, that can be seen in the image below, where it would just produce no results in the search.

Additionally, even when you can select the person by using the first approach, we couldn’t extract the values from the selected object, i.e name, age, etc. which are fields present in the contact person. The form fields used for this is shown in the image above.

In order to get the values from the mother object, I attempted to reference it in the calculate field as such:

type name label_ne label_en appearance calculate
string mother_case_id select-contact type-c82_person
hidden mother_name ./mother_case_id/name
hidden mother_age ./mother_case_id/age

This was not successful in retrieving the data from the selected contact.

Note: The selection of mother depends on a condition that the age of the child currently being created is less than 2. The group mother_case shown in the image above is inside the contact c82_person group, not outside as shown in the examples in CHT documentation.

@sahaniarun @gaarimasharma

Hi @gkesh

You can limit the types of contacts that will appear in the dropdown by assigning a type to the db-object field (like db:person to only show contacts of type person). There should be a detailed explanation in the documentation: Building App Forms | Community Health Toolkit

4 Likes

Hi @gkesh, to add to @diana’s comments, I noticed in your form image that you are using field names such as mother_case_name, mother_case_age and they also have calculations. Unless the mother contact has these exact fields in the root, it might not work.

According to the documentation: Input data available in forms

existing data in fields that are in the same group as the contact selector and share the same name as a field on the selected contact

It might not be very clear from the docs, but the field names need to match. After pulling a field using exact name, we can copy it to another field with a unique name to make it easier to use later.

Please see this example:

type name label::en relevant appearance required default calculation
begin group mother field-list
string mother_case_id Select Mother select-contact type-c82_person
hidden name
hidden date_of_birth
calculate mother_case_name …/name
calculate mother_dob …/date_of_birth
note mother_info_display * Mother’s Name: ${mother_case_name}
* Mother’s DOB: ${mother_dob}
end group

Here we are pulling name and date_of_birth from the mother’s contact document, and copying them over to the calculated fields: mother_case_name and mother_dob.

image

You can also see the values of all the fields by running this command in the browser console:

window.CHTCore.debugFormModel

For this problem:

If the user can see a person with correct type (i.e. "type":"contact", "contact_type": "c82_person"), they should be available in the list. Please check the mother’s document and make sure that the user has access to it. Also please note that the second approach (select-contact type-) is supported from CHT versions 3.10.x onwards.

If it still does not work, we can look further.

1 Like

The first problem of the contact selector was a non-issue, no results were shown because the name fields of c82_person were empty and since the contact selector searches contacts based on names, it could not find any results.

{
   "type": "contact",
   "contact_type": "c82_person",
   "name": "",
   ...
}

Shown above is how the document was saved in our test instance, and as can be easily noticed from it, the name field is empty hence, it would not match the search criteria of the contact selector.

In order to fix it, I just had to fix the issue in the form that resulted in the name field to be empty which now populated the name field in the document, hence allowing the contact selector to do its work.

{
   "type": "contact",
   "contact_type": "c82_person",
   "name": "MT02-CH0221-12/131 - Ronnie Parker",
   ...
}

As for the second issue, after we got the contact selection to work, it worked after arranging it as stated in the highlighted solution.

type name label_ne label_en appearance calculate
string name Enter Full Name
number age Enter Age
begin group mother_case
string mother_case_id select-contact type-c82_person
hidden name
hidden age
calculate mother_name ./name
calculate mother_age ./age

The form was much more complicated in its entirety and the one presented above is a short boiled down snippet of how it was designed. Some common mistakes that were found that resulted in these issues are:

  • Accessing values from selected objects is only possible by using the exact same variable name as it appears in the object. Additionally, you must also be pay attention to whether the variable appears in the root of the object or inside some nested structure.
  • Using variable names that repeat as ${variable}, i.e. taking the form above as an example if you directly use ${name} instead of using calculate and ${mother_name}, this will throw an error, because the app does not know which ‘name’ (inside the group or outside) is being referenced.

@sahaniarun @gaarimasharma