Import organization units with parent as Code

I cannot seem to figure out how to import organisation units using the web api with idScheme=“CODE”. I keep getting the error message "Invalid reference [ BBA] (OrganisationUnit) on object High Court [mbMVFTmv9Yv] (OrganisationUnit) for association parent.
The documentation on using idScheme isn’t so clear on on how this can be done. Sample payload is
{“name”:“Bomba”,“idScheme”:“CODE”,“orgUnitIdScheme”:“UID”,“openingDate”:“2023-04-24”,“parent”:{“id”:“MBR”}. Am I missing something?

Thanks @Michael_Mwebaze ! Welcome back to the community…

Could you share the API request you are sending?

I am able to create organisation units using the metadata and the organisationUnits using parent id but not using code. I am simply making a post request to the /metadata and organisationUnit endpoints. I have also tried a payload with {“name”:“Bomba”,“idScheme”:“CODE”,“orgUnitIdScheme”:“UID”,“openingDate”:“2023-04-24”,“parent”:{“code”:“MBR”} but doesn’t seem to work either.

Hey! I’m experiencing the same issue. Did you make any headway?

FYI, I tried the same using the import/export app and for some reason got an error so I had to switch the parent to the UID rather than code but using the API I HAVE to use code based on the structure of my data.

curl --location 'http://xxxxx/api/organisationUnits' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic xxxxxxxxxx \
--header 'Cookie: JSESSIONID=2A8788DAA9D7B1264993DD6F92D1ED78' \
--data '
    {"code":"223C",
     "name":"KIKULANI",
     "shortName":"KIKULANI",
     "description":"Schools",
     "openingDate":"2023-07-15",
     "comment":"2023-06-20",
     "orgUnitIdScheme": "code",
     "parent":{
         "code":"1245"
        }
    }'

Is this structure correct?

Hi @lillian1n2 I couldn’t get this to work with codes only. UIDs work fine but codes something seems not to work. I did try orgUnitIdScheme = code and orgUnitIdScheme = uid but neither worked.
Sorry I wasn’t helpful.

:frowning:

{
    "httpStatus": "Conflict",
    "httpStatusCode": 409,
    "status": "ERROR",
    "message": "One or more errors occurred, please see full details in import report.",
    "response": {
        "responseType": "ObjectReport",
        "uid": "dInmvoHzbit",
        "klass": "org.hisp.dhis.organisationunit.OrganisationUnit",
        "errorReports": [
            {
                "message": "Invalid reference [JzhIsrLkXzw] (OrganisationUnit) on object KIKULANI [dInmvoHzbit] (OrganisationUnit) for association `parent`.",
                "mainKlass": "org.hisp.dhis.organisationunit.OrganisationUnit",
                "errorProperties": [
                    "[JzhIsrLkXzw] (OrganisationUnit)",
                    "KIKULANI [dInmvoHzbit] (OrganisationUnit)",
                    "parent"
                ],
                "errorCode": "E5002"
            }
        ]
    }
}

This is the response I get from DHIS2 on the same. It has a disassociation on the parent field so I’m wondering how to either structure my data or my API call to fix the error.

This particular means the organisationUnits already exist. You might want to double check that.

What’s leading me to believe that the parent assignment as code is the issue is because the message says the issue is the association for parent.

When I use the uid instead, it creates successfully.

On the contrary, the same message where there is a conflict for the org unit already existing is this:

{
    "httpStatus": "Conflict",
    "httpStatusCode": 409,
    "status": "ERROR",
    "message": "One or more errors occurred, please see full details in import report.",
    "response": {
        "responseType": "ObjectReport",
        "uid": "MQaLNAYEmCq",
        "klass": "org.hisp.dhis.organisationunit.OrganisationUnit",
        "errorReports": [
            {
                "message": "Property `code` with value `223C` on object KIKULANI [MQaLNAYEmCq] (OrganisationUnit) already exists on object yi9vDgNSXUh.",
                "mainKlass": "org.hisp.dhis.organisationunit.OrganisationUnit",
                "errorCode": "E5003",
                "mainId": "yi9vDgNSXUh",
                "errorProperty": "code",
                "errorProperties": [
                    "code",
                    "223C",
                    "KIKULANI [MQaLNAYEmCq] (OrganisationUnit)",
                    "yi9vDgNSXUh"
                ]
            }
        ]
    }
}

Would generating custom UIDs and adding a code to the OU at the same time? Why wouldn’t this be a solution?

Thanks!