Function to update DHIS2 organization units

Can I use the upsert function of the dhis2 adaptor to add/update organization unit metadata in a DHIS2 instance?


@dsurrao Technically speaking, yes you should be able to upsert but let me know how it goes if you experiment with this example and its implementation here:

        attributeId: 'some_attribute_id_from_dhis2' //hypothetically, I would use the `id` for attributes like `name`?,
        attributeValue: 'some_attribute_value_from_state'
    { ou: 'some_org_unit_id' } //UPDATE: You may not need this param since you are upserting the `orgUnit` based on the `2nd` and `3rd` arguments. But you can send additional import params, if need be, see [here](

Or you can try and achieve the same with create with the help of api docs here and here. UPDATE: If you use create to upsert, then send CREATE_AND_UPDATE as the importStrategy . See docs.


Thanks @chaiwa, I ended up using the CSV metadata import API call with a direct HTTP request.

@dsurrao Great, that is a good hack! I, and many other users, would be curious to learn from your solution. Would you share the sample job/high level steps here or consider writing an OpenFn Operation that imports metadata as csv, for the dhis2 adaptor and raise a Pull Request?

@chaiwa my solution was to use the OpenFn http adaptor with a POST request to the DHIS2 server:

        body: state => {
            return state.metadataCSV;
        headers: {
            'Content-Type': 'application/csv'

where state.metadataCSV is a string with format:

"West province","uid12345678","WESTP","ImspTQPwCqd"
"East province",,"EASTP","ImspTQPwCqd"

uid is a string of exactly 11 alphanumeric characters that can be left blank, in which case DHIS2 will create a uid value for you.

1 Like