SAP CDS TUTORIAL

CDS View Annotations, Associations and Access Control (DCL) in S/4HANA: A Practical Guide

Annotations describe a CDS view, associations connect it to other views, and DCL decides who may see which rows. Build all three on a purchase order view, step by step.

Anshuman Behuria
Anshuman BehuriaLead SAP Mentor ยท theTechMentor
Updated
CDS View Annotations, Associations and Access Control (DCL) in S/4HANA: A Practical Guide
ModuleSAP ABAP
Read time10 min
LevelIntermediate
T-codes covered

SU21SU53ST01SU01

Most learners can write a basic define view entity after one tutorial. The trouble starts when a Fiori app shows no data for a user, a view will not activate, or a consultant asks, "Where is the authorization check?" Those problems come from three things: annotations, associations and access control.

This guide covers CDS view annotations, associations and access control (DCL) in one flow, using a purchase order view that MM and EWM consultants will recognise. By the end you will be able to annotate a view, expose associations, write a DCL role against M_BEST_EKO, and troubleshoot activation errors. If CDS is new to you, read Core Data Services (CDS) Views for Beginners first.

โšก Key takeaways
  • Annotations add meaning (labels, currency links, UI, authorization mode) to a CDS view; use ADT's Ctrl+Space to avoid invalid ones.
  • An association is a lazy relationship: define it, then expose it in the select list so consumers can navigate with path expressions.
  • DCL roles (define role) filter rows using PFCG authorizations, for example M_BEST_EKO for purchasing organization.
  • @AccessControl.authorizationCheck: #CHECK with no DCL role usually means business users see no rows.
  • Protect the lowest sensitive view and reuse conditions by inheriting, and always test with a real business user.
  • Check ADT's Problems view first when activation fails; the cause is usually a missing exposed field, wrong $projection reference or inactive dependency.
Process at a glance
  1. 1Create data definitionWrite the view entity with annotations in ADT and activate with Ctrl+F3.
  2. 2Add associationsDefine _Supplier and _CompanyCode, then expose them in the select list.
  3. 3Set authorization checkUse @AccessControl.authorizationCheck: #CHECK on the view.
  4. 4Write DCL roleCreate define role with aspect pfcg_auth(M_BEST_EKO, EKORG, ACTVT = '03').
  5. 5Assign PFCG roleGive the test user matching authorization values in PFCG and SU01.
  6. 6Test as business userRun data preview or ABAP SQL, and use SU53 or ST01 if rows are missing.

01How the three building blocks fit together

A CDS view has three layers of meaning. The select list says what data you read. Annotations say how the data is to be treated: labels, currency fields, UI behaviour, analytics type and the authorization mode. Associations say how this view relates to other views, without forcing a join. DCL (Data Control Language) says which rows a given user may read.

The reason this matters in S/4HANA is that the same view is consumed by ABAP reports, Fiori apps, OData services and analytics. Authorization has to live with the data model, not in each consumer. That is the idea behind DCL. It is also why code-pushdown work, as in ABAP on HANA code pushdown, normally ends with a CDS view plus an access control object.

ElementWhere it is writtenPurpose
AnnotationsAbove or inside the DDL source (@...)Metadata for labels, semantics, UI, analytics, authorization mode
AssociationDDL source, after as select fromReusable, lazy relationship to another entity
Access controlSeparate DCL source (define role)Row-level filtering based on PFCG authorizations or literals

02Annotations: the ones you will actually use

There are hundreds of annotations. Learn the handful that appear in almost every project, and look up the rest in the ABAP CDS documentation inside ADT (ABAP Development Tools in Eclipse).

AnnotationExample valueWhat it does
@EndUserText.label'PO Header with Supplier'Description shown in tools and the repository
@AccessControl.authorizationCheck#CHECKTells the framework to apply DCL. Other values: #NOT_REQUIRED, #NOT_ALLOWED, #PRIVILEGED_ONLY
@VDM.viewType#BASIC, #COMPOSITE, #CONSUMPTIONPosition in the virtual data model layers
@Semantics.amount.currencyCode'DocumentCurrency'Links an amount field to its currency field
@Semantics.quantity.unitOfMeasure'OrderQuantityUnit'Links a quantity to its unit
@Analytics.dataCategory#DIMENSION, #FACT, #CUBERole of the view in analytics
@UI.lineItem[{ position: 10 }]Shows the field as a column in a Fiori list report
@Search.searchabletrueEnables fuzzy search on the view

Annotations apply at three levels: view level (above define), element level (above a field) and, in some cases, parameter level. The same annotation name can be valid at one level only, so a misplaced annotation is a common activation error.

One annotation deserves special care: @AccessControl.authorizationCheck. If you leave it out on a view in a system with the check enforced, you get a warning in the activation log. If you set #CHECK and no DCL role exists, users with normal authorization see no rows at all. We return to this in the DCL sections.

03Associations: relationships without a join

An association defines a link to a target entity but does not execute until a consumer uses it. Think of it as a join that is only built when somebody navigates through it. This makes views smaller and faster than stacking joins into every view.

Here is a purchase order header view that exposes associations to supplier and company code. The released interface view names below are standard in S/4HANA, but confirm field names in your release using ADT's Open ABAP Development Object (Ctrl+Shift+A).

@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'PO Header with Supplier'
@VDM.viewType: #COMPOSITE
define view entity ZI_PurchaseOrderHdr
  as select from I_PurchaseOrderAPI01 as Po
  association [0..1] to I_Supplier    as _Supplier
    on $projection.Supplier = _Supplier.Supplier
  association [0..1] to I_CompanyCode as _CompanyCode
    on $projection.CompanyCode = _CompanyCode.CompanyCode
{
  key Po.PurchaseOrder,
      Po.CompanyCode,
      Po.PurchasingOrganization,
      Po.PurchasingGroup,
      Po.Supplier,
      Po.PurchaseOrderDate,
      _Supplier,
      _CompanyCode
}

Points to notice:

Using the association in ABAP SQL

A consumer navigates with a path expression, which is a backslash followed by the association name:

SELECT PurchaseOrder,
       \_Supplier-SupplierName AS SupplierName
  FROM zi_purchaseorderhdr
  WHERE CompanyCode = '1010'
  INTO TABLE @DATA(lt_po).

The database performs a left outer join to I_Supplier only for this query. The same navigation is used by Fiori elements, where a navigation property in the OData service maps to the association.

04Writing a DCL role for the purchase order view

Access control is a separate repository object created in ADT through New → Other ABAP Repository Object → Core Data Services → Access Control. For our view we want to restrict rows by purchasing organization, using the standard authorization object M_BEST_EKO (field EKORG, with ACTVT).

@EndUserText.label: 'Access control for PO header'
@MappingRole: true
define role ZI_PURCHASEORDERHDR {
  grant select on ZI_PurchaseOrderHdr
    where ( PurchasingOrganization ) =
      aspect pfcg_auth( M_BEST_EKO, EKORG, ACTVT = '03' );
}

Read this as: a user may select a row only if their PFCG authorizations contain activity 03 (display) for the purchasing organization in that row. The left side is the view field; the right side maps it to the authorization field.

Other DCL conditions you will meet

After activation, give the test user a PFCG role containing M_BEST_EKO with EKORG = 1000 and ACTVT = 03. Log in as that user, run a data preview of the view in ADT, and check that only organization 1000 rows return. If nothing appears, run SU53 for the failed check or trace with ST01.

05Step-by-step: build and test the secured view

  1. In ADT, create a package such as ZMM_CDS and open the ABAP project connected to your S/4HANA system.
  2. Create the data definition ZI_PurchaseOrderHdr with the DDL above. Press Ctrl+F3 to activate.
  3. Run Open With → Data Preview (F8). With #CHECK and no role, a user without special rights sees no rows, which confirms the check is live.
  4. Create the access control object ZI_PURCHASEORDERHDR with the define role code and activate it.
  5. Assign a test user a PFCG role with M_BEST_EKO restricted to one purchasing organization (PFCG, then SU01 for assignment).
  6. Log on as the test user and repeat the data preview. Only authorised rows should appear.
  7. Run the ABAP SQL path expression from the earlier section to confirm the association works under the same user.

06CDS view activation: what goes wrong and how to fix it

Activation is the step that turns the DDL source into a runtime object. In ADT you activate one object with Ctrl+F3 and several inactive objects with Ctrl+Shift+F3. Activate the underlying views before the views that depend on them, and the DCL role after the view it protects.

SymptomLikely causeFix
Activation log: unknown field in on conditionUsed the source field instead of the projected field with $projectionAdd the field to the select list or use the correct alias
Association not found by consumerAssociation defined but not added to the select listList _Supplier in the field list so it is exposed
Warning about missing authorization check@AccessControl.authorizationCheck missingSet #CHECK and create a DCL, or #NOT_REQUIRED if the data is not sensitive and your governance allows it
DCL cannot be activatedView inactive, or field name in where does not exist in the viewActivate the view first and use the exposed field names
Annotation rejectedValue or position not valid for that annotationUse Ctrl+Space in ADT to see allowed values
Duplicate rows in the resultWrong association cardinality or a join to a one-to-many targetCorrect the cardinality and the on condition

The "association cannot be used locally" problem

People search for this message when an association defined in one view does not work where they try to use it. In practice, the cause is usually one of three things: the association is used in the same view in a place or a way the syntax does not allow, the association was never exposed so another view cannot reach it, or the path expression is applied to a view type that does not support it. The exact wording differs between releases, so read the full message in the ADT Problems view and note the line it points to.

The working fix is almost always the same: define the association, expose it in the select list, and navigate to it from the consuming view or ABAP SQL statement through a path expression. If you need the target data in the same view, use an explicit join or define a path expression in the field list (for example _Supplier.SupplierName as SupplierName).

07Finance views: ACDOCA and ACDOCP in CDS

Two of the most searched finance tables in this area are ACDOCA and ACDOCP. ACDOCA is the Universal Journal table that stores actual line items; its standard CDS interface view is I_JournalEntryItem. See the Universal Journal (ACDOCA) guide for the table itself and New General Ledger for ledger concepts.

ACDOCP is the planning counterpart: it stores plan line items from integrated planning. For reporting on it, first search the system for released standard CDS views that read ACDOCP (in ADT use Open ABAP Development Object and the Where-Used list on the table). Build a custom view only if nothing standard fits.

For any custom view on these tables, three rules save trouble:

08Common mistakes and interview questions

Common mistakes

Interview questions on this topic

  1. What is the difference between a join and an association in CDS?
  2. What does @AccessControl.authorizationCheck: #CHECK do and what happens without a DCL role?
  3. How would you restrict a purchase order view by plant and purchasing organization?
  4. How do you reuse one DCL condition across header and item views?
  5. Where would you look when a Fiori list is empty for one user only?

For a logistics view of where such CDS models are consumed, see end-to-end logistics integration.

๐ŸŽ
Free: SAP MM & EWM video course + interview kit

Get the 40+ lesson playlist, the most-asked interview questions and our 90-day roadmap on WhatsApp.

?Frequently asked questions

What is a CDS view DCL?

DCL (Data Control Language) is the language used to write access control objects for CDS views. A role defined with define role grants select on a view only for rows that match PFCG authorization values, literals or the current user. It is applied automatically when the view has authorizationCheck set to #CHECK.

How do I create a CDS view on ACDOCP?

ACDOCP holds plan line items. First search for a released standard CDS view that already reads it, using the where-used list in ADT. If none fits, create a view entity selecting from the table, add semantic annotations for amounts and currencies, and protect it with a DCL role on company code.

How does CDS view activation work?

You activate a data definition in ADT with Ctrl+F3, or several inactive objects with Ctrl+Shift+F3. Activation checks syntax and dependencies and generates the runtime object. Activate underlying views first, and activate the DCL role after the view it protects.

What does the error that an association cannot be used locally mean?

It generally points to an association being used in a place or way the view does not allow, or one that has not been exposed. The exact text varies by release, so read the Problems view in ADT. Normally you expose the association in the select list and navigate through it from the consumer, or use an explicit join in the same view.

Which CDS view reads ACDOCA?

The standard interface view I_JournalEntryItem reads the Universal Journal table ACDOCA. Use it as the base for custom reporting because it already carries semantic annotations. Confirm the view and its fields in your release using ADT.

What is the difference between an association and a join in CDS?

A join is executed whenever the view runs. An association is only resolved when a consumer navigates through it using a path expression. This keeps views lighter and lets the consumer decide which relationships to use.

Why does my CDS view return no data for a business user?

Most often the view has #CHECK and the user's PFCG role lacks matching authorization values, or no DCL role exists. Check SU53 right after the failure, or trace with ST01. Then adjust the role or the DCL condition.

Learn SAP hands-on with theTechMentor

Live weekday & weekend batches or 24/7 recorded courses, with S/4HANA practice server access and interview preparation.