Need to transfer TEIs ownership in bulk

Hi community, @Markus, @phil, @Gassim

I have a question for you.
Currently need to transfer part of existing TEIs to the new organisation. Here are the steps taken:
1. I have got all my TEIs by requesting /api/trackedEntityInstances.json?fields=*&program=program_Uid&ou=ou_Uid&trackedEntityInstance=TEI1_UID;TEI2_UID;…;TEIn_UID

2. Then has changed all orgUnit values into new OU uid and updated TEIs. But I couldn’t change programOwners[ownerOrgUnit]. I found that it can be done by using PUT method and API point: /api/tracker/ownership/transfer?trackedEntityInstance=TEI_UID&program=program_UID&ou=NEW_OU_UID.

Question: How can I change an ownership in bulk for all my TEIs like
/api/tracker/ownership/transfer?trackedEntityInstance=TEI1_UID;TEI2_UID;…;TEIn_UID&program=program_UID&ou=NEW_OU_UID.?
Is it possible or there is another way to migrate TEIs from one OU to another one in bulk?

Thank you
Regards,
Ulanbek

2 Likes

Dear Ulanbek,
Thank you for your question! I might have an idea but I welcome experts and community members to add theirs please!

Thanks for mentioning the steps and your final step. It’s very helpful! If you are using API there is a way using a software to send multiple requests (such as Postman) and keep changing the value of a parameter in each request until the list is complete. For example, the request could be (/api/tracker/ownership/transfer?trackedEntityInstance=<TEI-UID-Variable>;…;TEIn_UID&program=program_UID&ou=NEW_OU_UID) and then the software will iterate in each request and change the variable from the list of all TEI-UIDs. A developer could easily run this as long as the request (/api/tracker/ownership/transfer?trackedEntityInstance=TEI1_UID;…;TEIn_UID&program=program_UID&ou=NEW_OU_UID) is working well.

Another option is to use the app Data Import Wizard. It seems like it could do the action you want but it won’t transfer ownership, it will import the data to the orgunit you select (which means you’d probably need to delete the TEIs before importing) which I couldn’t try till the end because you’d need to get all the info you see in (api/trackedEntityInstances.json?fields=*&program=program_Uid&ou=ou_Uid&trackedEntityInstance=TEI1_UID;TEI2_UID;…;TEIn_UID) in xls or csv format, but if you download as xls the endpoint doesn’t support this file format and if you download as csv not all the info you see in the .json request is downloaded. This means it will take you some time to prepare an xls file - I think the API request is easier and should take less time than preparing the xls. (however, it might be worth checking out the app!)

https://apps.dhis2.org/app/190583b7-e39b-4e52-a1c9-c7641aeaa833

Hope this helps! Thanks! :+1:

Coincidentally I was also just doing this. We decided to expand our org units and need to move all TEIs to a new OU. The API approach worked well. I accomplished this using curl and looping through my TEIs.

I’m wondering why you can’t do this via the import/export app? When I import an update with the below ownerOrgUnit changed it seems to ignore the update. Is this a bug, or it intentional that you can’t make this change through the import/export app?

        "programOwners": [
            {
                "ownerOrgUnit": "**ZKO6ygilkaq**",
                "program": "xrwtYQH5ZLa",
                "trackedEntityInstance": "YKeF4xRPswX"
            }
        ],
1 Like

Hi @kstankevitz ,

Thank you for your message. The problem is I need to split TEis into few organisations. Therefore first need to filter TEis, then transfer to the corresponding OUs.

Today I am quite got confused and puzzled as few days ago I could get all needed TEIs by filtering and changing old OU UID to the new OU UID, then simply update all those TEis, then transfer ownership as described in the API documentation.
But today I have done the same and got surprised as the transferred TEIs become uneditable inside the new OU. Also interesting thing is if you update TEI one by one everything is fine, but when those updated in bulk they become uneditable… Any ideas??? :smirk::thinking::unamused:
@Gassim may be you have any suggestions?
Thank you

1 Like

Hi @Ulanbek :wave:
As far as I know the tracker/ownership/transfer endpoint does not support bulk updates as it’s a PUT request that required a specific TEI uid, for example:
https://play.dhis2.org/2.37.4/api/tracker/ownership/transfer?trackedEntityInstance=ifjQnG7uFwE&program=IpHINAT79UW&ou=g8upMTyEZGZ
So it would be interesting to hear how you were trying to do the bulk updates.
See the docs here.

1 Like

Hi @plinnegan ,

Thank you for your guidance Peter. I have experienced with some interesting side effects of tracker/ownership/transfer API point and I believe either that was a bug or some limitations which haven’t been mentioned in the documentation. Moreover I am planning to share my experience as special small guidance to the users, who may need to split and transfer TEIs amongst few OUs.

Thank you all and have a great week-end.
Regards,
Ulanbek

1 Like

Thanks all!

Looking forward to it @Ulanbek! Thanks! :+1: