Related to Update strategies for Tracked Entity Instances. My use case is that I am uploading historical data to DHIS2 via the web API. I would like to create TEIs if the record does not already exist in DHIS2, but update the TEI record if a matching record is found based on the identifier I specify (e.g., farmerID
).
The DHIS2 documentation suggests that I can set an identifier (e.g., farmerID
) for a tracked entity when posting TEI data to DHIS2.
From the web api documentation:
1.67.5 Update strategies. Two update strategies for tracked entity instance are supported: enrollment and event creation. This is useful when you have generated an identifier on the client side and are not sure if it was created or not on the server.
CREATE_AND_UPDATE: Try and match the ID, if it exist then update, if not create.
I have been testing with DHIS 2.29 using this CREATE_AND_UPDATE strategy in my post request to DHIS2, and the response will always try to create a new tracked entity (but never update if it already exists).
curl -X PUT -H "Content-Type:application/json" -d @payload.json -u $UN:$PW https://severname/api/29/trackedEntityInstances?strategy=CREATE_AND UPDATE
This CREATE_AND_UPDATE strategy will only update a tracked entity if I include the TrackedEntityInstance (tei uid) generated by DHIS2 in my payload. Therefore can I not generate a new/ use a different external unique identifier (farmerID
) to lookup and update existing tracked entity instances? Must I always use the DHIS2-generated tracked entity instance ID?
To sum it up, we have generated an ID ( farmerID
) on the client side, and weâre not sure if it was created or not on the server. This is the classic case for an upsert (i.e., strategy=CREATE_AND UPDATE
) and is reflected in the documentation, but the current behavior of the API suggests that client-side generated IDs can not be used for matching on upserts. If thatâs correct (only server-generated IDs can be used), how does one perform the upsert given that the client-side system wonât know the server-generated ID if it hasnât yet been created?