What is correct PATCH request structure to update a specific attributevalue of an organisationUnit

Hi Community,
What is the correct PATCH request structure to update a specific attributevalue of an organisationUnit via the ../api/organisationUnits/{id} endpoint?
(I want to update only specifc attributevalue (Custom Attribute) rest should stay “as it is” in metadata.(instead of full PUT Request))
Version 2.40.6
Partial Updates.
Any working examples would be appreciated!

Hi @arslan_mughal

Could you please share the JSON that includes your custom attribute?

For this case you will use PATCH /api/organisationUnits/{id} as you explained and the payload patch will be (based on the https://jsonpatch.com/ instructions)

[
  { "op": "replace", "path": "pathtothecustomattribute", "value": "newValue" }
]

Payload:
json

[
  {
    "op": "replace",
    "path": "/attributeValues/attribute/id/qyY5ZVWvpBr/value",
    "value": "newCode123"
  }
]

I also tried:

[
  {
    "op": "replace",
    "path": "/attributeValues/qyY5ZVWvpBr/value",
    "value": "newCode123"
  }
]

But got the error:
Payload cannot contain objects or arrays. ErrorCode: E1003
Also tried without [ ] square brackets and response is 1 but value not updated.
with PATCH ../api/organisationUnits/{id}

Error Response is :

{
    "httpStatus": "Bad Request",
    "httpStatusCode": 400,
    "status": "ERROR",
    "message": "Payload can not contain objects or arrays.",
    "errorCode": "E1003"
}

Hi @arslan_mughal. I hope you have figured out your question by now, but if you still are looking into this, you need to patch the entire array of attribute values when making an update.

E.g.
[{"op":"replace","path":"/attributeValues","value":[{"attribute":{"id":"UKNKz1H10EE"},"value":"test"}]}]

You can also look at the new Maintenance Preview app (available via App Management App if it is not already installed). There you can edit an organisation unit, update an attribute value, and then look at the networks tab to inspect the payload

1 Like

You are right the correct payload is:

[
  {
    "op": "replace",
    "path": "/attributeValues",
    "value": [
      {
        "attribute": {
          "valueType": "TEXT",
          "mandatory": false,
          "id": "qyY5ZVWvpBr"
        },
        "value": "mynewvalue"
      }
    ]
  }
]

This works for PATCH requests, but only if you set the Content-Type header to ->application/json-patch+json Without that, the server will reject it. Just wanted to clarify that in case anyone else runs into the same issue. Thanks!

2 Likes

That’s great! Thanks for sharing the solution. :slight_smile: