# Ensure Relationship

Operation ID: `ensureRelationship`

Group: Value Accounting › Master Data Associations

[Open in the interactive reference](https://lensing.ai/api-docs/lensing/v1/endpoints/ensureRelationship)

`POST /value-accounting/associations`

Retry-safe: A write that accepts Idempotency-Key: repeating a request with the same key replays the stored answer instead of executing twice.

Tag two master-data records to each other — the entry point for master-data tagging. WRITES. A relationship is stored as two directed edges, one on each record: this call writes the EXPLICIT edge from `record` to `relatedRecord` and the IMPLICIT reverse edge back, so both records can read the relationship from their own side and either can remove it. If the other record later tags this one, its edge becomes explicit too; an explicit edge is never downgraded. Idempotent — calling it again for the same pair merges the dates instead of duplicating anything (omit = as stored, null = clear, value = set), so retrying after a failure is safe. Returns an empty body; read the result with getRelationshipsForMasterData (POST value-accounting/associations/search). Both records must exist and must not be the same record, and the relationship must not end before it starts (END_BEFORE_START, judged on the dates it is left with: a date sent, else the stored one, and on a new pair an omitted start is today). An end that is not a value center is anchored on its own value object: the one standing for it today, or its retired one (also one that has ended while its link to the record still runs) as it stands — re-opened only when the call creates a NEW relationship, sends no endDate or one of today or later (an endDate before today records a past relationship and never re-opens), every record that value object would stand for again is ACTIVE and the screens of reactivateValueObjects (POST value-accounting/value-objects/reactivate) pass (never a custom value object's shadow); it creates one only when none ever existed (or the one it had now stands for another record), named from the master-data record and numbered with its business number, or a generated VO-n when another value object already holds that number (business numbers are unique only within their dimension), so its number can differ from the record's (also when the record already has a value center); removing the relationship does not delete it. To undo it, remove the relationship first and then delete that value object with deleteValueObject (DELETE value-accounting/value-objects/:id) (find it with searchValueObjects (POST value-accounting/value-objects/search) by `record`): once nothing else uses it, it is hard-deleted — `isSystemManaged: true` locks its name and mappings against edits, not against the delete. That delete needs the value-object delete grant (API: Delete Value Objects), which the tagging grant (API: Manage Master Data Associations) does not contain. ⚠️ A relationship keeps a record from being hard-deleted until it is removed: this package's deletes DEACTIVATE a value object, value center or custom value object that still has any relationship instead of deleting it, and deleting a department, team, location or employee is refused only while a relationship a user made or took over remains.

Tags two master-data records to each other. This is the entry point for master-data tagging; the per-edge repair apis are not.

A relationship is stored as TWO directed edges, one anchored on each record. This call writes the EXPLICIT edge record → relatedRecord and the IMPLICIT reverse edge relatedRecord → record. `isImplicit` records, per side, whether that record asserted the relationship: the initiator's edge is explicit, the target's is implicit. An existing implicit edge on the initiator's side is promoted to explicit; an existing explicit edge on the target's side is never downgraded, so if both records tag each other, both sides end up explicit.

Idempotent: calling it again for the same pair merges the dates rather than duplicating anything, so a retry after a network failure is safe. Dates follow the package convention on a re-run: a date you omit stays as stored, `endDate: null` clears a previously set end date, a date you send is written — always onto BOTH edges.

A record can never be tagged to itself (`SELF_RELATIONSHIP`): a self-loop would make the record report itself as in use and permanently block its own deletion.

Side effect worth knowing: an end that is not a value center is anchored on its own value object — the one standing for it today, or its retired one as it stands; this call re-opens a retired one only when it creates a NEW relationship, sends no `endDate` or one of today or later (the end date is the relationship's last active day, so a relationship ending today still runs today), every record that value object would stand for again is ACTIVE, and the screens of reactivateValueObjects (POST value-accounting/value-objects/reactivate) pass — a repeat call, a date change or a call sending an `endDate` before today (a past relationship) never re-opens anything, and a custom value object's shadow is never re-opened here. It creates one only when none ever existed (or the one it had now stands for another record), named from the master-data record itself and numbered with its business number — or a generated VO-n when another value object already holds that number (business numbers are unique only within their dimension), so its number can differ from the record's — also when the record already has a value center. Removing the relationship does not delete it. To undo it, remove the relationship first and then delete that value object with deleteValueObject (DELETE value-accounting/value-objects/:id) (find it with searchValueObjects (POST value-accounting/value-objects/search) by `record`): once nothing else uses it, it is hard-deleted — `isSystemManaged: true` locks its name and mappings against edits, not against the delete. That delete needs the value-object delete grant (API: Delete Value Objects), which the tagging grant (API: Manage Master Data Associations) does not contain: a caller that can only tag cannot remove a value object it caused, and should ask a holder of that grant.

⚠️ A relationship keeps a record from being hard-deleted until it is removed: this package's deletes DEACTIVATE a value object, value center or custom value object that still has any relationship instead of deleting it, and deleting a department, team, location or employee is refused only while a relationship a user made or took over remains.

## Parameters

| Name | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `Api-Version` | header | string · v1 | yes | The API version this request targets, named by its public identifier. Required on every request; omitting it, or naming a version this company does not have, is answered with 400. This document describes version `v1`. |
| `Idempotency-Key` | header | string | no | An opaque caller-chosen string (1-255 characters) that makes this request safe to retry. Repeating the same key with an identical request body replays the original response instead of re-executing - but only once that original request succeeded; a failed attempt re-executes on retry. Repeating the key with a different body is answered with 422. A key still in flight is answered with 409 - as is a key whose original request was interrupted before it finished, for a few minutes afterwards, until that attempt is presumed dead. A completed response stays replayable for 24 hours. The key is scoped to the caller, not to the action: reusing it for a different action is answered with 409 or 422 rather than treated independently. On read actions this header is ignored. On write actions that cannot offer the guarantee it is answered with `400 IDEMPOTENCY_NOT_SUPPORTED` rather than silently ignored. |

## Request body

Required, `application/json`: ensureRelationshipInput.

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `record` | object | yes | The INITIATING record — the one whose journey is doing the tagging. Its edge is written explicit. Identified by the (dimensionCode, id) pair; both halves are required, because a UUID is unique within its dimension but not across dimensions. Example: `{"dimensionCode":"department","id":"9a5e1c77-3b2d-4f10-8c6e-1d40a7b93c58"}`. |
| `record.dimensionCode` | string | yes | Dimension the record belongs to. One of 'department', 'businessUnit', 'team', 'location', 'region', 'employee', 'vendor', 'customer', 'valueCenter', 'custom' ('custom' being a custom value object). An unknown code raises UNSUPPORTED_DIMENSION_CODE. |
| `record.id` | string | yes | Id (a UUID) of the master-data record. This is the same value resolveReportingTags (POST base/reporting-tags/resolve) returns as modelUuid for the record. An id that resolves to no record of that dimension raises RECORD_NOT_FOUND — it is not silently created. Ids are matched case-insensitively and returned in canonical lowercase. |
| `relatedRecord` | object | yes | The TARGET record. Its edge is written implicit, unless it already exists as explicit — in which case it stays explicit. Identified by the same (dimensionCode, id) pair. Must not be the same record as `record`. Example: `{"dimensionCode":"valueCenter","id":"f91ecda5-2912-4624-b8b2-b6d28e1d64e1"}`. |
| `relatedRecord.dimensionCode` | string | yes | Dimension the related record belongs to. Same ten codes as `record.dimensionCode`. The two records may be of the same dimension (two departments, say) as long as they are different records. |
| `relatedRecord.id` | string | yes | Id (a UUID) of the related master-data record. An id that resolves to no record of that dimension raises RECORD_NOT_FOUND. Ids are matched case-insensitively and returned in canonical lowercase. |
| `startDate` | date | no | Date the relationship starts, as YYYY-MM-DD. Written on BOTH directed edges. On a NEW pair, omit it to start the relationship TODAY — but a system-managed caller (a sync mirroring an employee's job or a department move) MUST pass the real start date, because the relationship began when the source record did, not when the sync ran. On an EXISTING pair, omit it to keep the stored start date; send a date to correct it on both edges. Example: `"2026-01-01"`. |
| `endDate` | date \| null | no | Date the relationship ends, as YYYY-MM-DD. The package convention, as on every PATCH: omit to leave the stored end date UNCHANGED (a new pair without one is open-ended), pass null to CLEAR it back to open-ended, send a date to set or move it. Written on both edges. Omitting it on a re-run never clears anything. |

```json
{
  "record": {
    "dimensionCode": "department",
    "id": "9a5e1c77-3b2d-4f10-8c6e-1d40a7b93c58"
  },
  "relatedRecord": {
    "dimensionCode": "valueCenter",
    "id": "f91ecda5-2912-4624-b8b2-b6d28e1d64e1"
  }
}
```

## Response

`200` Success

```json
{
  "data": {},
  "diagnostics": [
    {
      "code": "<code>",
      "message": "<message>",
      "severity": "error",
      "path": [
        "<path>"
      ],
      "details": "<details>"
    }
  ]
}
```

## Response headers

- `Idempotency-Replayed` — Present and `true` only when this response replays a previously stored one for the same Idempotency-Key, rather than reflecting a live execution. Absent on a live response - never sent as `false`.

## Errors

| Code | Status |
| --- | --- |
| `UNSUPPORTED_DIMENSION_CODE` | 400 |
| `RECORD_NOT_FOUND` | 400 |
| `SELF_RELATIONSHIP` | 400 |
| `OBJECT_NUMBER_TAKEN` | 400 |
| `END_BEFORE_START` | 400 |
| `IDEMPOTENCY_KEY_CONFLICT` | 409 |
| `IDEMPOTENCY_PAYLOAD_MISMATCH` | 422 |

## Statuses

- `400` — Invalid request or business failure. Codes: UNSUPPORTED_DIMENSION_CODE, RECORD_NOT_FOUND, SELF_RELATIONSHIP, OBJECT_NUMBER_TAKEN, END_BEFORE_START
- `401` — The request carries no valid access token.
- `403` — Not authorized
- `404` — Not found
- `409` — The Idempotency-Key is still being processed by another request.
- `422` — The Idempotency-Key was already used with a different request payload.
- `429` — Too many requests from this client address. The response has no body and no `Retry-After` header.
- `500` — Internal server error
