CDS Annotations in SAP – 7

The metadata components known as CDS View Annotations are specified in the CDS view to affect how various SAP applications display, process, or use the data. They offer a layer of extra information that may be used by other tools and technologies in the SAP ecosystem, without altering the underlying data.

The representation of objects and connections in CDS views is the main emphasis of the Object Model annotations, which include items like:

Describe the representation of entities and their connections in the CDS model using entity and association annotations.
Describe the connections and cardinality between things, particularly when working with associations (such as many-to-one or one-to-many links).
Operations and Actions: enable you to specify operations or actions on data that may be linked to certain business logic or user interface activities.

Object Model Annotations in CDS Views

Below are some of the most commonly used object model annotations in CDS views:

@ObjectModel.modelCategory

This annotation defines the nature of the model—whether it’s a transactional model, analytics model, or master data model. It is primarily used for semantic categorization and behavior.

@ObjectModel.modelCategory: #BUSINESS_OBJECT

Possible Values:

  • #BUSINESS_OBJECT: Indicates that the CDS view represents a business object.
  • #MASTER_DATA: Indicates that the CDS view represents master data (for example, Customer, Product).
  • #TRANSACTIONAL: Indicates that the CDS view represents transactional data (for example, Sales Order, Invoice).
  • #ANALYTICAL: Indicates that the CDS view is for analytical purposes (for example, KPI views, reports).

@ObjectModel.association

This annotation defines an association between entities, which is the way relationships between entities are represented in CDS. Associations are used to express “navigations” between entities, similar to how foreign keys or joins work in relational models.

@ObjectModel.association: [
  { target: 'ZProduct', type: #ONE_TO_MANY, sourceElement: 'ProductID', targetElement: 'ProductID' }
]

target: The target entity or table of the association.

type: The type of relationship (e.g., #ONE_TO_MANY, #MANY_TO_ONE).

sourceElement and targetElement: The fields that establish the relationship.

@ObjectModel.composition

This annotation is used to define a composition relationship between two entities, where one entity is a part of another. Composition is stronger than association in that it suggests the lifecycle of the “child” entity is tied to the lifecycle of the “parent” entity.

@ObjectModel.composition: [
  { target: 'ZInvoiceItem', sourceElement: 'InvoiceID', targetElement: 'InvoiceID' }
]

@ObjectModel.transactionalProcessing

This annotation is used in transactional models to indicate that the CDS view supports transactional processing. It is especially relevant in the context of SAP applications that require transactional data to be processed.

@ObjectModel.transactionalProcessing: true

@ObjectModel.usageType

The @ObjectModel.usageType annotation is used to define how a CDS view or entity is intended to be used within the system, such as for UI applications, backend processing, or other specific purposes.

@ObjectModel.usageType: { service: #UI, application: #APP }

#UI: The entity is used in the user interface layer.

#APP: The entity is used for application-level business logic.

#ANALYTICS: The entity is used for analytical purposes.

@ObjectModel.semanticKey

This annotation helps to identify which field(s) in a CDS view should be treated as the semantic key, which represents the unique identifier for the business object.

@ObjectModel.semanticKey: ['CustomerID', 'Country']

@ObjectModel.navigationalProperty

This annotation is used to define properties or fields that can be navigated in the data model, which typically represent relationships like foreign keys or links between entities.

@ObjectModel.navigationalProperty: 'ZCustomer'

Search-Annotations in ABAP CDS Views

In SAP Fiori apps, where users often interact with big data sets and require strong filtering and search capabilities, search annotations are utilized to improve the user experience. These annotations make it possible to specify fuzzy or full-text search behavior, employ fields as search criteria, and increase the effectiveness of data searches.

@Search.defaultSearchElement

Specifies the field that should be used as the default search field when the user enters a search term.

@Search.searchable

Marks fields as searchable, meaning that they can be used as part of search operations.

@Search.fuzzySearch

Enables fuzzy search on specific fields. This allows partial or approximate matches to be found when the search term is not an exact match (e.g., searching for “Joh” to match “John”).

@Search.fullTextSearch

Enables full-text search for textual fields, making it possible to search for terms that appear anywhere within a text field.

@Search.filterExpression

Defines a custom filter expression for search queries, allowing more complex filtering logic to be applied.

In this example, we’ll define a Customer CDS view where certain fields are marked as searchable or available for filtering in the UI.

@AbapCatalog.sqlViewName: 'ZV_CUST_SEARCH'
@EndUserText.label: 'Customer Search View'
@OData.publish: true
define view ZCustomerSearch
  as select from zcustomer
{
  key CustomerID,
      CustomerName,
      City,
      Country,
      Email,
      PhoneNumber,
  
  @UI.selectionField: [{ position: 10, value: 'CustomerName' },
                       { position: 20, value: 'City' },
                       { position: 30, value: 'Country' }]
  @Search.defaultSearchElement: 'CustomerName'
  @Search.searchable: true
  @Search.fuzzySearch: true
  CustomerID,
  CustomerName,
  City,
  Country,
  Email
}

@Search.defaultSearchElement: 'CustomerName': The CustomerName field is set as the default search field. When the user performs a search, the system will search first in the CustomerName field.

@Search.searchable: true: Marks the fields CustomerID, CustomerName, City, Country, and Email as searchable. These fields will be indexed and available for search queries.

@Search.fuzzySearch: true: Enables fuzzy search for the CustomerName field, meaning that users can search for partial or similar terms. For example, searching for “Joh” can match “John”, “Johann”, and other similar terms.

@Search.fullTextSearch: true: This enables full-text search on the Description field. Users can search for any part of the description text, which provides more flexibility in finding products based on partial matches (e.g., searching for “plastic” will return products with descriptions containing the word “plastic”).

@Search.searchable: true: Marks the ProductName and Description fields as searchable, meaning users can search these fields for specific terms.

@Search.filterExpression: This annotation defines a custom filter expression. The expression is constructed to allow users to search for terms in either the ProductName or Category fields. For example, if a user searches for “Electronics”, the system will return products where either the ProductName or Category contains the term “Electronics”.

@Search.searchable: true: Marks the ProductName and Category fields as searchable.

Leave a Comment