New event to an exiting TEI

Hi everyone,

How can I add an event to an existing TEI using the DHIS2 tracker API. I’m currently implementing this using python and my DHIS2 version is 2.40.3.

Below is a sample of the code where I want to put the code:

query_url = f"http://localhost:8081/dhis/api/33/trackedEntityInstances/query.json?program={program}&query={person_case_id}&ou={organisation_unit}"
    response = requests.get(query_url, auth=(username, password))
    data = response.json()

    # print(data)

    if data['rows']:
        # Record exists, perform import update
        instance_id = data['rows'][0][0]

       # Create a new event for the existing TEI here
        
    else:
        # Send the POST request to import the tracker data
        response = requests.post(url, auth=HTTPBasicAuth(username, password), json=json_data)

        # Check the response status
        if response.status_code == 200:
            print('New Tracker record number ' +str(successful_insert_counter)+ ' imported successfully and Status is ' + str(status))
            # print('Returned message:', response.text)
            successful_insert_counter += 1
        else:
            print('Failed to import tracker data. Status code:', response.status_code)
            print('Error message:', response.text)
            failed_insert_counter += 1

so first where query to check if any TEI of the person to be imported into the DHIS2 exist. If it does, then that’s where I want to create a new event of that existing TEI else create a new TEI all together. note that this is sample and incomplete code. I neeed the end points to just create the new event in an existing TEI otherwise everthing else I can do

Hi @1c699b409a68f19fba95

Before to add an event, check if there is an active enrolment. And if doesn’t exists first create it, and only then you can create an event on … /api/tracker

1 Like

Yes I did check that but I just want to know the endpoint of adding an event to an existing active enrolment

…/api/events for the new and existing event.
If you have an existing event, then you can change existing value by using…/api/events/event_id/dataelement_id

I hope you know the payload format

1 Like

I guess this is for editing an exiting event. I actually want to create a new event in the same enrolment. such that I have 2 or more event in one enrolment. See the attached picture

@1c699b409a68f19fba95 I already mentioned that this API point can be used for both: new one and existing events. For new one you should provide TEI and Enrollment UID.

1 Like

I really don’t understand you

how the payload format for a new event to an already existing tei

@1c699b409a68f19fba95 as I mentioned earlier, the same API point can be used for GET and POST requests.
In order to create a new event you should POST your payload

{
“orgUnit”: {orgUnit_UID},
“program”: {program_UID},
“programStage”: {desired_programStage_UID},
“status”: “ACTIVE”,
“trackedEntityInstance”: {exisitng_TEI_UID},
“enrollment”: {exisitng_enrollment_UID},
“dataValues”: [
{
“dataElement”: {DE_UID},
“value”: {value}
}
]
}

to the …/api/events API point. But before to POST your event - find TEI UID and select ACTIVE ENROLLMENT, otherwise you will get an error.

1 Like

Thanks I got it, it works now

1 Like