# Function to update DHIS2 organization units

**URL:** <https://community.openfn.org/t/function-to-update-dhis2-organization-units/132>\
**Category:** Adaptors & Templates\
**Created:** [August 16, 2021, 9:18pm UTC](https://community.openfn.org/t/function-to-update-dhis2-organization-units/132 "2021-08-16T21:18:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dsurrao](https://community.openfn.org/letter_avatar_proxy/v4/letter/d/50afbb/32.png) [@dsurrao](https://community.openfn.org/u/dsurrao)\
**Post date:** [August 16, 2021, 9:18pm UTC](https://community.openfn.org/t/function-to-update-dhis2-organization-units/132/1 "2021-08-16T21:18:29Z")

</div>

Can I use the [upsert](https://openfn.github.io/language-dhis2/global.html#upsert) function of the dhis2 adaptor to add/update organization unit metadata in a DHIS2 instance?

Thanks.

---

<div class="post-metadata">

**Author:** ![chaiwa](https://community.openfn.org/user_avatar/community.openfn.org/chaiwa/32/33_2.png) [@chaiwa](https://community.openfn.org/u/chaiwa)\
**Post date:** [August 17, 2021, 9:08am UTC](https://community.openfn.org/t/function-to-update-dhis2-organization-units/132/2 "2021-08-17T09:08:33Z")

</div>

@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](https://github.com/OpenFn/language-dhis2/blob/e7ae0b6e1fb5bbdfa60a5ff7b1cf052ac47ad4bb/src/Adaptor.js#L1691-L1808):

```auto
upsert(
     'organisationUnits',
     {
        attributeId: 'some_attribute_id_from_dhis2' //hypothetically, I would use the `id` for attributes like `name`?,
        attributeValue: 'some_attribute_value_from_state'
    },
     state.data,
    { 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](https://docs.dhis2.org/en/develop/using-the-api/dhis-core-version-master/metadata.html#webapi_metadata_create_update).
  );

```

Or you can try and achieve the same with [create](https://openfn.github.io/language-dhis2/global.html#create) with the help of api docs [here](https://docs.dhis2.org/en/develop/using-the-api/dhis-core-version-master/metadata.html) and [here](https://docs.dhis2.org/en/develop/using-the-api/dhis-core-version-master/metadata.html#webapi_metadata_create_update). **UPDATE:** If you use **create** to upsert, then send `CREATE_AND_UPDATE` as the `importStrategy` . See [docs](https://docs.dhis2.org/en/develop/using-the-api/dhis-core-version-master/metadata.html#webapi_metadata_create_update).

---

<div class="post-metadata">

**Author:** ![dsurrao](https://community.openfn.org/letter_avatar_proxy/v4/letter/d/50afbb/32.png) [@dsurrao](https://community.openfn.org/u/dsurrao)\
**Post date:** [August 20, 2021, 3:25pm UTC](https://community.openfn.org/t/function-to-update-dhis2-organization-units/132/3 "2021-08-20T15:25:55Z")

</div>

Thanks @chaiwa, I ended up using the [CSV metadata import](https://docs.dhis2.org/en/develop/using-the-api/dhis-core-version-master/metadata.html#webapi_csv_org_units) API call with a direct HTTP request.

---

<div class="post-metadata">

**Author:** ![chaiwa](https://community.openfn.org/user_avatar/community.openfn.org/chaiwa/32/33_2.png) [@chaiwa](https://community.openfn.org/u/chaiwa)\
**Post date:** [August 20, 2021, 4:54pm UTC](https://community.openfn.org/t/function-to-update-dhis2-organization-units/132/4 "2021-08-20T16:54:38Z")

</div>

@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](https://docs.openfn.org/documentation/build/adaptors/#extending-an-adaptor) that imports metadata as csv, for the [dhis2 adaptor](https://github.com/OpenFn/language-dhis2) and raise a Pull Request?

---

<div class="post-metadata">

**Author:** ![dsurrao](https://community.openfn.org/letter_avatar_proxy/v4/letter/d/50afbb/32.png) [@dsurrao](https://community.openfn.org/u/dsurrao)\
**Post date:** [August 20, 2021, 6:27pm UTC](https://community.openfn.org/t/function-to-update-dhis2-organization-units/132/5 "2021-08-20T18:27:59Z")

</div>

@chaiwa my solution was to use the [OpenFn http adaptor](https://github.com/openfn/language-http) with a POST request to the DHIS2 server:

```auto
post(
    'api/metadata?classKey=ORGANISATION_UNIT', 
    {
        body: state => {
            return state.metadataCSV;
        },
        headers: {
            'Content-Type': 'application/csv'
        }
    }
);

```

where `state.metadataCSV` is a string with format:

```auto
name,uid,code,parent
"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.
