Web API: Unable to create Event when inserting Tracked Entity Instance

Is it possible to create a Tracked Entity Instance and create an Event in the same request and payload? The web API documentation for version 2.29 indicates yes via the update strategy, but I am finding otherwise when testing…

1.67.5 Update strategies. Two update strategies for tracked entity instance are supported: enrollment and event creation.

In one HTTP post request via my command line, I am trying to (1) create a Tracked Entity Instance, (2) enroll the TEI, and (3) create a related Event. I am able to do 1 and 2 successfully using the below curl and this TEI payload – but not #3.

curl -X POST -H "Content-Type:application/json" -d @payload.json -u UN:PW https://dhis2servername.net/api/29/trackedEntityInstances?strategy=CREATE_AND_UPDATE

I receive a success message response for importing Events, but no Event record is actually created/imported:
"events":{"responseType":"ImportSummaries","status":**"SUCCESS","imported":0**,"updated":0,"deleted":0,"ignored":0}

When I copy/paste the same Event data into a new Event-only payload, and try to create an Event via the Events API endpoint, I am able to successfully create this event no issues.

curl -X POST -H "Content-Type:application/json" -d @EventPayload.json -u UN:PW https://dhis2servername.net/api/29/events

In sum, I am unable to create the Event at the same time as creating the Tracked Entity Instance using the CREATE_AND_UPDATE strategy. Is there something about my payload I need to modify? Or is the web api documentation not accurate?

Thanks for your support.

3 Likes

Hi @aleksa_krolls,

I am tagging @Emma_Kassy to have a look at this and advise while other community members also look into it.

Bet,
James.

1 Like

Hi aleksa,

Did you get any solution for this issue Even i am facing the same issue.Please provide me the solution if anything with you.

Good timing! I was just discussing with a partner today (check out this slide). The ‘CREATE_AND_UPDATE’ strategy does not work unless you can provide the DHIS2-generated resource Id.

To upsert via the DHIS2 api when we do not have the DHIS2-generated Id in our external application, I implemented a custom pattern that used 2 API calls to:

  1. First send a GET request to search for an existing TEI, filtering by custom attributes (e.g., another unique external Id)
  2. And then either send a POST request (if the TEI does not exist) or PUT request (if the TEI does exist) using the TEI Id returned in step 1

I used OpenFn to automate this, so feel free to check out this custom upsert pattern here: dhis2-demo/custom-TEI-upsert.js at master · OpenFn/dhis2-demo · GitHub

@RameshReddy What issues exactly are you experiencing? Is it only with upserts?

1 Like

Thanks for reply aleksa,I am trying to create tracked entity instance and enrollment and events at a time.I was getting the missing mandatory error even i gave all the mandatory filed values.

And Also i would like to know the how we can improve the performance of the api/27/trackedEntityInstances for this API.If you have any solution please please provide me.

@RameshReddy can you share the request you were sending to DHIS2 to help troubleshoot? Also what type of performance improvements are you seeking?

Hi Aleksa,

Thanks for the reply,Please find the below request which am sending the request.In playload am sending all the tracked entity attributes.But still it is saying madatory attributes missing.And in terms of performance i am trying to save the 63 trackedentityattributes with 100 data elements at a time it is taking 2.5 secs.is there any way to reduce the saving time.

{

“trackedEntity”: “MCPQUTHX1Ze”,

“orgUnit”: “niqqEs4Iln1”,

“attributes”: [

{

  "attribute": "d3tzFSFrxna",

  "value": "TAMMA"

},

{

  "attribute": "w0pJZJZoqdK",

  "value": "VFFBF"

},

{

  "attribute": "MoOhSIpq3NX",

  "value": "testing"

},

{

  "attribute": "WxfFHIgsj5Z",

  "value": ""

},

{

  "attribute": "OObRLoYDfek",

  "value": "FILIPINO"

},

{

  "attribute": "kleFBlenj3u",

  "value": "Misamis Oriental"

},

{

  "attribute": "QOzEZDj8VL2",

  "value": ""

},

{

  "attribute": "wI9qaCATrjy",

  "value": "National Capital Region"

},

{

  "attribute": "T3KKd2EMGHb",

  "value": "vgyhgyu"

},

{

  "attribute": "UxhlmsUNBCx",

  "value": "hvhbhbk"

},

{

  "attribute": "gr2PJls04tR",

  "value": "-"

},

{

  "attribute": "mdqJEG4qYDO",

  "value": ""

},

{

  "attribute": "rYMvmftDZdA",

  "value": ""

},

{

  "attribute": "zsCKct9N0LE",

  "value": "ftyftyfy"

},

{

  "attribute": "SRmz7JlCdXb",

  "value": ""

},

{

  "attribute": "VCCGZ4jEqrv",

  "value": ""

},

{

  "attribute": "WmYu1RSFo6b",

  "value": ""

},

{

  "attribute": "YGDlFlflS82",

  "value": ""

},

{

  "attribute": "YxyaMVmwQ4g",

  "value": "9876543210"

},

{

  "attribute": "Amdxw05ko10",

  "value": ""

},

{

  "attribute": "rWuHI731rLo",

  "value": "0001-01-01"

},

{

  "attribute": "bvNIgdSarvI",

  "value": "01"

},

{

  "attribute": "I5T44W38KhD",

  "value": "49"

},

{

  "attribute": "KqnuYsJcc01",

  "value": "1971-06-28"

},

{

  "attribute": "RphJAbUnfkH",

  "value": ""

},

{

  "attribute": "sHpuvxG89od",

  "value": "0"

},

{

  "attribute": "T7fRsP8FRZ1",

  "value": "0"

},

{

  "attribute": "SqlD3nIm3XR",

  "value": ""

},

{

  "attribute": "TtbTt8ADcfQ",

  "value": "02"

},

{

  "attribute": "M1XaaILD7KH",

  "value": ""

},

{

  "attribute": "tgk7rBac1EG",

  "value": "000000000"

},

{

  "attribute": "HjhW7er988Q",

  "value": "02"

},

{

  "attribute": "HFBEcChfBLC",

  "value": "CHO CDO"

},

{

  "attribute": "PEErz8oCVcd",

  "value": "02"

},

{

  "attribute": "JOXA4odxf3k",

  "value": "ed150e97-75c0-4206-hgdshfg-edhgudjnsajnfnjbxbfjsauj nsnvjsngj4c3291jdbfjsbb2bd"

},

{

  "attribute": "RI6SpXKVOLx",

  "value": "test@gmil.com"

},

{

  "attribute": "OWKnMtW3NSX",

  "value": "CDRS_ed150e97-75c0-4206kdmndmvgfyfysnkv-9a16-ednvvnd4c3291b2bd"

},

{

  "attribute": "RCqcEXy3exn",

  "value": "Active"

},

{

  "attribute": "lUK1lUUe0wc",

  "value": "BBz5DG0VoK"

},

{

  "attribute": "HERUttsBIZL",

  "value": "2020-08-13"

},

{

  "attribute": "Jbl5GBqH3qW",

  "value": "KIXJXQ77Y3"

}

],

“enrollments”: [

{

  "program": "nSy7PFqQykt",

  "status": "ACTIVE",

  "orgUnit": "niqqEs4Iln1",

  "enrollmentDate": "2020-08-13",

  "incidentDate": "2020-08-13"

}

]

}