RAP Overview

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

A clerk opens a list of expense claims in a browser, clicks one, changes the total, presses Save, and the new value is in the database. Behind that screen sits an ABAP program that has to read the claim, check the change, protect it against a second user, write it, and answer in a form the browser understands. The ABAP RESTful Application Programming Model, RAP for short, is SAP’s recipe for building exactly that kind of program: you describe the data and what may be done with it, and a framework supplies the rest of the plumbing, including the web service. This page is the map of RAP for a newcomer. It names every part, shows how the parts stack on top of each other, and explains the two ideas that the later pages build on, the transactional buffer and the difference between managed and unmanaged business objects. SAP states that RAP is meant for ABAP Cloud and recommends the ABAP language version ABAP for Cloud Development; this page is written against ABAP for Cloud Development release 9.20 (2608). The RAP guide declares itself valid for SAP S/4HANA Cloud Public Edition, SAP S/4HANA Cloud Private Edition, the SAP Business AI Platform ABAP environment (the product known as the SAP BTP ABAP environment) and SAP S/4HANA, and it describes the newest tool and backend scope, so details such as individual keywords can depend on the release of the system you use. Every listing here needs an SAP system with ADT and the RAP framework, so each one is labelled as requiring an SAP system and was not run here.

Before you read

Key terms

  • RAP: the ABAP RESTful Application Programming Model, the transactional programming model of ABAP Cloud.

  • RAP business object: a tree of CDS entities plus a behavior definition that says what may be done with them.

  • Behavior definition: the repository object, written in the Behavior Definition Language, that describes the operations and characteristics of a business object.

  • Transactional buffer: the memory area that holds the data of a business object while it is being worked on.

  • Interaction phase: the part of a RAP transaction in which data is changed in the transactional buffer.

  • Save sequence: the part of a RAP transaction that checks the buffer and writes it to the database.

  • Managed business object: a business object whose standard behavior the RAP framework supplies.

  • Unmanaged business object: a business object whose behavior, including its buffer, the developer codes.

  • Projection view: a CDS view entity that selects, and renames, a part of a business object for one service.

  • Service definition: the list of CDS entities that a business service exposes, independent of any protocol.

  • Service binding: the object that ties a service definition to a protocol such as OData V4 and to a kind of service, UI or Web API.

What RAP is and where it applies

SAP defines RAP as the architecture for end-to-end development of OData services in ABAP Cloud, where OData is the web protocol that a browser app uses to ask for and change data. RAP supports all kinds of transactional Fiori applications (Fiori is SAP’s current user interface) and the publishing of Web APIs, that is, services meant for programs rather than for screens. It is built on three technologies: Core Data Services for the data model, a service model infrastructure that creates the OData services, and ABAP classes for the business logic that the framework cannot guess. The glossary adds that in RAP the application server plays the role of a stateless web server: each request stands alone, and the state of an ongoing conversation can be saved to the standard database, which is what draft handling does.

Two statements from the guide help a reader of older books. First, RAP was designed to improve on the ABAP Programming Model for SAP Fiori, the model of the AS ABAP 7.5x era that combined CDS views with the Business Object Processing Framework (BOPF), and RAP took over its lessons. A book written for that era describes the older model, and its annotations and BOPF classes are not how new development is done; a later page of this section compares the two. Second, RAP separates the business logic from the protocol-specific parts, so the same business object can be exposed as OData V2 and as OData V4 without reimplementing anything.

The layers at a glance

A RAP application is a stack. At the bottom is a database table. On it you build the data model of one business object, add its behavior, then cut a service-specific view of it, name that cut as a service, and finally bind the service to a protocol. A Fiori elements app (an app whose screens SAP generates from annotations instead of hand-written screen code) sits on top.

flowchart TD TAB["Database table ZEXP_CLAIM"] --> BASE["Base business object: root view entity ZI_ExpClaim with its items, plus the base behavior definition"] BASE --> PROJ["Service projection: projection view ZC_ExpClaim plus the projection behavior definition"] MDE["UI annotations, often in a metadata extension"] -.-> PROJ PROJ --> SD["Service definition: which entities are exposed"] SD --> SB["Service binding: OData V4 UI service"] SB --> FE["Fiori elements app, or the preview in ADT"]

The next table repeats the stack and names who supplies what. The names in the middle column are the running example of this section; SAP’s own tutorial uses other names for the same roles, as the last section shows.

Layer Running example What it does

Database table

zexp_claim (and the item table of the CDS pages)

Holds the persisted data. A draft table is added when drafts are used.

Base business object, data model

ZI_ExpClaim (root) and ZI_ExpItem (child)

CDS view entities joined by a composition. The root entity stands for the whole business object.

Base business object, behavior

behavior definition of ZI_ExpClaim

Says which operations exist (create, update, delete, actions) and the characteristics (locking, authorization, ETag, numbering, draft).

Behavior implementation

a behavior pool class

ABAP code for what the framework does not do, for example a validation.

Service projection

ZC_ExpClaim and a projection behavior definition

Takes the part of the business object one service needs and adds service-specific details such as UI annotations.

Business service

a service definition and a service binding

Names the exposed entities, then binds them to OData V2 or V4 as a UI service or a Web API.

The business object: structure, behavior and runtime

A business object is the programming name for a real-world thing such as a product, a sales order or an expense claim, together with its parts and the actions allowed on it. The RAP guide describes a business object through three characteristics: a structure, a behavior and the runtime that carries them out.

Structure. A business object is a hierarchical tree of nodes. Each node is a CDS entity, and nodes are linked by compositions, a composition being an association with a whole and part meaning: a part exists only together with its parent. The top node is the root entity, written with the keyword ROOT; it represents the whole business object. The root names its child with COMPOSITION, and the child names its parent with ASSOCIATION TO PARENT. For the expense claim the claim is the root and its items are the children, exactly as built on CDS Associations and Compositions.

Behavior. The behavior is a separate repository object, the behavior definition, written in the Behavior Definition Language (BDL), and it always refers to a CDS data model. One behavior definition belongs to exactly one root entity and covers all the entities of the composition tree below it. It holds a behavior characteristic for each entity (general properties such as ETag, draft handling, feature control, numbering and authorization control) and the operations. The standard operations are create, update and delete, plus a lock operation; application-specific operations are called actions. Read operations change nothing: read, read by association, and functions, which are like actions without side effects.

Runtime. A consumer, such as the OData request of a browser or an ABAP program, calls the operations. The framework keeps the changes in an internal memory area, the transactional buffer, and writes them to the database later, in the save sequence. The next sections explain this, because it is the idea that makes RAP different from older ABAP code that wrote to the database straight away.

A cut-down behavior definition for the claim and its items shows how little a managed business object needs to say. The lines on locking and authorization are the ones every business object needs; the mapping connects the CDS element names to the field names of the tables.

managed implementation in class zbp_i_expclaim unique;
strict ( 2 );

define behavior for ZI_ExpClaim alias Claim
persistent table zexp_claim
lock master
authorization master ( global )
{
  create;
  update;
  delete;
  association _Items { create; }

  mapping for zexp_claim corresponding
  {
    ClaimId      = claim_id;
    EmployeeId   = employee_id;
    Status       = status;
    CurrencyCode = currency_code;
    TotalAmount  = total_amount;
  }
}

define behavior for ZI_ExpItem alias Item
persistent table zexp_item
lock dependent by _Claim
authorization dependent by _Claim
{
  update;
  delete;
  association _Claim;
}

The listing requires an SAP system; not run here. It is a sketch of the structure, not a complete behavior definition: the mapping of the item entity, the audit fields of zexp_claim, the item table of the CDS pages and the methods that authorization master ( global ) requires in the behavior pool are left out. What the lines mean:

  • managed implementation in class zbp_i_expclaim unique; is the header. managed says the framework supplies the standard behavior, and the class is the behavior pool, the home of your own code. The glossary notes that the behavior itself lives in local handler and saver classes inside the pool, not in the global class.

  • strict ( 2 ); switches on a set of extra syntax checks. SAP recommends strict mode for all RAP business objects with a few listed exceptions, such as business configuration objects, and requires it for objects that are released with a release contract.

  • persistent table zexp_claim names the table that the managed framework reads and writes.

  • lock master, lock dependent by _Claim and the two authorization lines place the entities in the locking and authorization structure; the locking is explained on the locks page.

  • create; update; delete; declare the standard operations, and association _Items { create; } allows creating items through the claim.

Managed or unmanaged

Every business object has an implementation type. The guide names two standard types and says that both are based on a data model written in CDS view entities. They differ in who provides the transactional behavior.

Managed Unmanaged

Who provides the behavior

The RAP framework, through the managed business object provider. You only declare the standard operations.

You. The behavior is declared in the behavior definition but coded by hand in the behavior pool.

Transactional buffer

Provided and handled for you.

Provided and handled by the developer.

Typical use

New applications built from scratch, with no existing business logic.

Existing logic, often in function modules, that you want to put behind a RAP service.

Cost

Existing logic is hard to fit into the standard behavior.

You write what the framework would otherwise give you.

Between the two sits the managed type with additional or unmanaged save. The interaction phase stays managed, but you code the save yourself, for example to write to a different table or to add a step such as a change document. The managed type with unmanaged save cannot name a persistent table. The glossary adds that a behavior definition has five implementation types in all: managed, unmanaged, interface, projection and abstract. The last three describe layers and helpers rather than a way to provide behavior, and later pages cover them. For the expense claim a new managed object is the natural choice. If the company had a mature function module that posts a claim, an unmanaged object, or a managed object with unmanaged save, would wrap it.

The transactional buffer, the interaction phase and the save sequence

RAP does not invent its own unit of work. It uses the SAP LUW, the logical unit of work described on ABAP SQL: Writing Data, and divides it into two phases.

  • In the interaction phase the consumer calls operations. New instances are created in the transactional buffer, and existing data is read from the database into the buffer for changes. The data in the buffer may be inconsistent during this phase, and the database stays untouched and consistent. Programs must not change the database directly here.

  • In the save sequence the framework checks that the buffer is consistent and then writes it to the database. The save sequence starts with the EML request COMMIT ENTITIES, which an ABAP program or the RAP transactional engine can issue, for example when it handles an OData changeset.

flowchart LR DB[("Database table")] -- "read into the buffer" --> BUF["Transactional buffer"] OP["Consumer calls create, update, delete, actions"] --> BUF BUF -- "COMMIT ENTITIES: save sequence" --> CHK{"Checks pass?"} CHK -- "yes" --> SAVE["Save to the database and commit"] CHK -- "no" --> CLEAN["Clean up the buffer, nothing is written"]

The save sequence has steps that map to saver methods: the early steps FINALIZE and CHECK_BEFORE_SAVE can still fail, and COMMIT ENTITIES then returns sy-subrc 4 with all touched buffers cleaned up. If they succeed, ADJUST_NUMBERS (which assigns final numbers when numbering is late) and SAVE run, and a successful SAVE ends the LUW with sy-subrc 0. A failure in the late save phase gives sy-subrc 8 and the whole LUW must be rolled back. The guide also gives rules that surprise classic ABAP programmers: an explicit COMMIT WORK or ROLLBACK WORK in RAP code is a runtime error, because the framework alone handles the commit, and with the strict ( 2 ) mode a database change in the interaction phase is a runtime error too. In draft scenarios the buffer is also written to a draft table during the interaction phase, so that half-finished data survives the end of the session; a later page covers drafts.

The projection layer

The base business object is service agnostic: it carries every feature that any service might want. A projection is the first layer that is specific to a service. It takes the subset of the structure and behavior that the service needs, may rename elements, and holds the details that do not belong to the general model: UI annotations, value helps, calculations and defaulting. The guide’s example is a business partner object projected differently for customers, vendors and suppliers. The benefit is stability: the base object can grow without changing a service that already exists. The same base object can also be projected once for a Fiori UI and once for a stable Web API.

A projection has two parts, a CDS projection view for each node, and a projection behavior definition. The projection view is a view entity of the provider contract transactional query. It contains only elements of the projected entity (it cannot add persistent fields), may add virtual elements calculated in ABAP, and a composition must be redirected to the projected child. The projection behavior definition only lists, with use, what the base definition already has; it has no implementation of its own unless it defines new actions.

@EndUserText.label: 'Expense claim for the claims app'
define root view entity ZC_ExpClaim
  provider contract transactional_query
  as projection on ZI_ExpClaim
{
  key ClaimId,
      EmployeeId,
      Status,
      CurrencyCode,
      @Semantics.amount.currencyCode: 'CurrencyCode'
      TotalAmount
}
projection;
strict ( 2 );

define behavior for ZC_ExpClaim alias Claim
{
  use create;
  use update;
  use delete;
}

Both listings require an SAP system; not run here. They are sketches: the composition to the items, which must be redirected to a projection view of the item entity, and the item’s projection behavior are left out, and the claim view shows only the elements the sketch needs. Notice what is missing from the projection behavior: locking and authorization are not repeated, because the projection keeps what the base object already restricts.

The business service: definition and binding

The last two objects turn the projection into something a program can call. A RAP business service is a RESTful service that a consumer can call and that is published with an OData protocol. It consists of two repository objects with different jobs.

  • The service definition lists which CDS entities, with their behavior, are exposed. It is protocol agnostic, so one definition can be used by several bindings.

  • The service binding ties the definition to a protocol, OData V2 or OData V4 (the guide also lists InA and SQL for other scenarios), and to a service type: a UI service, which carries UI annotations so that a Fiori elements app can be built on it, or a Web API, which carries none. The binding is published with the Publish button of its editor in ADT, which enables the service in the current system only, and a Fiori elements preview can be started from the binding. The guide advises OData V4 wherever possible for transactional services, and adds that full support for Fiori elements on OData V4 exists only for draft-enabled scenarios.

Separating the two objects means that exposing the same data through another protocol is a new binding, not new code.

@EndUserText.label: 'Expense claims service'
@ObjectModel.leadingEntity.name: 'ZC_ExpClaim'
define service ZSD_ExpClaim
{
  expose ZC_ExpClaim as Claim;
}

The listing requires an SAP system; not run here. It follows the shape of the service definition example of the RAP guide (a label, the leading entity, define service, and expose ... as lines) with the running example’s names; the service binding has no source text, because it is created and published in the ADT editor.

Generating the layers

Writing all of these objects by hand is slow, so ADT has generators. SAP’s tutorial "Generate a transactional UI service" starts from a single database table and uses the wizard Generate ABAP Repository Objects with the generator named ABAP RESTful Application Programming Model: UI Service. The tutorial’s table shows the result by layer: a business object data model (a CDS entity with the alias Travel), a behavior implementation class and a draft table for the business object; a projection entity and a projection behavior class for the service projection; and a service definition and a service binding of type OData V4 UI for the business service. After you publish the binding, you open the entity in the Fiori elements preview and see the app. The tutorial names the base entity with an R and the projection with a C, whereas this section’s pages use the older I and C prefixes for the interface and consumption views; the prefix is a naming convention, not something the framework checks. The tutorial also shows that a generated object can be a draft business object, which a later page explains. A separate wizard, OData UI Service from Scratch, can start from an empty package, but its documented limits are real: one business object, UUID keys, a managed object with internal early numbering, and no actions, determinations or validations.

Summary

  • RAP is SAP’s architecture for transactional OData services and Web APIs in ABAP Cloud, built on CDS data models, behavior definitions, ABAP classes and a service infrastructure.

  • A business object is a composition tree of CDS entities with one root, a behavior definition written in BDL, and a runtime that keeps changes in a transactional buffer.

  • Managed business objects get their standard behavior and buffer from the framework; unmanaged ones code both, which suits existing logic; managed objects can also add or replace the save.

  • Changes are made in the interaction phase and written in the save sequence started by COMMIT ENTITIES; RAP code must not call COMMIT WORK itself.

  • A projection view and a projection behavior definition cut a service-specific view of the business object; a service definition names it and a service binding publishes it as OData V2 or V4, as a UI service or a Web API.

  • Older books that describe the CDS and BOPF model of the 7.5x era describe the predecessor of RAP.