You can use existing in OCL when you need to determine whether a collection still contains at least one non-deleted object, including while changes are being made in the current transaction.
Syntax
collection->existingcollection is an OCL collection, commonly a role (association end) such as self.Orders.
Return value
existing returns a Boolean value:
| Collection state | Result |
|---|---|
| The collection contains one or more objects that have not been deleted. | true
|
| The collection is empty. | false
|
| The collection contains only objects deleted during the current transaction. | false
|
Use existing on a role when your rule needs to know whether at least one related object remains valid.
For example, a Customer has an Orders role:
self.Orders->existingThe expression returns true when the customer has at least one non-deleted Order. It returns false when the customer has no orders, or when all of its orders have been deleted in the current transaction.
Use it with optional roles
An optional role can have no related object. Use existing when the relationship may be empty or when its object may have been deleted before the transaction is committed.
For example, if Customer has an optional PrimaryOrder role, evaluate whether that role still has a valid object:
self.PrimaryOrder->existingThis returns false if PrimaryOrder has not been set or if the related order has been deleted in the current transaction.
existing compared with exists
existing checks whether a non-deleted object is present. exists(condition) evaluates a condition for the elements in a collection. Choose the operator based on what your expression needs to answer.
| Expression | Question answered | Example |
|---|---|---|
collection->existing
|
Does the collection contain at least one non-deleted object? | self.Orders->existing
|
collection->exists(condition)
|
Does at least one collection element satisfy the condition? | o.Total > 1000) |
Use existing for presence checks. Use exists(condition) when you must test a property or relationship of the collection elements.
For example, these expressions test different things:
-- At least one valid Order remains
self.Orders->existing
-- At least one Order has a total greater than 1000
self.Orders->exists(o | o.Total > 1000)