Introduction

The ABAP RESTful Application Programming Model (RAP) is an innovative framework that enables developers to build enterprise-ready, cloud-centric applications using the powerful ABAP language. It’s designed to streamline the development process, ensuring a consistent approach to creating both on-premise and cloud-based solutions. Let’s delve into what makes RAP an exciting development paradigm.

The Foundation of RAP

RAP is built on three key pillars: Domain Model, Behavior Model, and Service Model. These three models work together to provide a cohesive and robust programming environment.

  1. Domain Model: Defines the core business entities and their relationships. It leverages the ABAP language to create data models that are reusable and scalable.
  2. Behavior Model: Specifies the business logic and operations that can be performed on the entities defined in the domain model. This includes CRUD (Create, Read, Update, Delete) operations and custom business logic.
  3. Service Model: Exposes the functionality defined in the behavior model as RESTful services. This allows for easy integration with other systems and applications, enabling seamless interaction with the business logic.

Getting Started with RAP

To begin working with RAP, developers need access to an SAP system that supports the framework. Here are the essential steps:

  • Set Up Your Environment

    First and foremost, make sure you have an SAP HANA trial account.

    You need a HANA BTP account to access SAP ABAP environment on cloud, follow the link for a quick guide on how to Create free BTP account and while doing so you need to Setup ABAP environment on your BTP. Then install Eclipse for your local development environment.

    Next connect your BTP cloud account with ADT on your eclipse. See here.

    Once you’re done setting up, and connected your ADT workspace to your BTP ABAP environment.

    Create a new Project: Right-click System name in File explorer>New>ABAP Package.

    Give a proper name and description and click on Next.

    In your next step, if you’re using Trial account, you can choose ZLOCAL software component, or HOME, if you want to use default component and you’re connected to a different system.

    Next, create a new TR for your new application. Give it a meaningful description. Then click Finish.

    Optional: Add your new package to your Favorite Packages list in your eclipse workspace.

    Right Click on Favorite Packages>Add Package>Type your package name and click on OK.

    Voila, you’re done setting up your environment, now you can start developing your application.

  • Create Database Table

    Now let us start creating our application, Right-click on your package navigate to New>Other ABAP Repository Object.

    Next, in the wizard search for Database Table and click next.

    Give it a name, in this case ZXX_TRAVEL_A, the letter A denotes that is an active table. We usually create two types of tables, Active and Draft table, which we will cover later.

    Click next, and save it in your TR.

    Lets define the fields, types and their semantics for our database table. Save, check and activate our table.

    @EndUserText.label : 'Travel App'
    @AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
    @AbapCatalog.tableCategory : #TRANSPARENT
    @AbapCatalog.deliveryClass : #A
    @AbapCatalog.dataMaintenance : #RESTRICTED
    
    define table ztest_travel_a {
        key client            : abap.clnt not null;
        key travel_id         : /dmo/travel_id not null;
        agency_id             : /dmo/agency_id;
        customer_id           : /dmo/customer_id;
        begin_date            : /dmo/begin_date;
        end_date              : /dmo/end_date;
        @Semantics.amount.currencyCode : 'ztest_travel_a.currency_code'
        booking_fee           : /dmo/booking_fee;
        @Semantics.amount.currencyCode : 'ztest_travel_a.currency_code'
        total_price           : /dmo/total_price;
        currency_code         : /dmo/currency_code;
        description           : /dmo/description;
        overall_status        : /dmo/overall_status;
        attachment            : /dmo/attachment;
        mime_type             : /dmo/mime_type;
        file_name             : /dmo/filename;
        created_by            : abp_creation_user;
        created_at            : abp_creation_tstmpl;
        local_last_changed_by : abp_locinst_lastchange_user;
        local_last_changed_at : abp_locinst_lastchange_tstmpl;
        last_changed_at       : abp_lastchange_tstmpl;
    }
    
  • Generating Views, Behaviors and Service

    Next we will let ADT generate our view entities(root, projection), Behavior definitions, service definitions and binding.

    Right-click on your Database table and select Generate ABAP Repository Objects.

    Next under ABAP Restful Application Programming Model, select OData UI Service and click next and in the next step type in your Package name.

    Next, we will configure all of our objects in the generator wizard.

    IMPORTANT: Do not proceed until you have defined all properties of the objects.

    Business Object

    Inside the Data Model, we are prompted to give an alias for our CDS entity, you can also change the default entity name.

    Go the Behavior tab, this will automatically create our Behavior Definitions CDS object and notice that we are creating a behavior implementation class and a draft table, which I mentioned before.

    The class is where we will define and implement all the behavior events(create, read, update, delete), validations, determinations, actions, etc.

    The draft table will be used to store unrecognized data, meaning the data which is not yet updated to our database but as a draft data. A use case is when we are creating, updating or deleting a record where the data is stored in a draft and we haven’t yet saved it.

    Service Projection

    Next, we will define our Service Projection View and Projection Behavior, as we can see, the wizard has already generated the names for us, we can proceed or change the names according to our needs.

    Similar to before, we are defining implementation class for our Service Projection.

    Business Service

    Here, we will define our Service Definition name and Binding name. Change the naming accordingly or leave the defaut ones.

    Wizard automatically selects OData V4 as our default connection type.

    Generation configuration is completed, we can proceed and click Next.

    Object List

    The next window shows all the objects that are listed for creation and a preview of the code that it will generate.

    Business Object Projection Layer acts as projection components for our root entity definitions under Business Object Layer.

    We can also notice a Metadata Extension annotation object, it is important to note that this object annotation corresponds to how each of our field in our view will be displayed or used a selection field when we run this service though a UI application like Fiori.

    Click Next, select our TR and wait for the artifacts generation to be completed.

  • Service Binding: Publishing

    By default, it should redirect to the service binding object, or you can navigate on your package for Business Services>Service Bindings>ZUI_TEST_TRAVEL_A_O4(Binding name)

    Now, we are going to publish our service. Click on the Publish icon and wait for the service to be published.

    After successful publishing, we can see our app listed as below. Select it and click on Preview, if any error occurs, you can Copy Fiori Elements App Preview URL and open it in a browser.

    If all is well, you should get a page like this.

    But hold on, we don’t have any data in our database table. Let’s add some data to our table.

  • Populate Database Table

    In order for us to populate our database table, we are going to need the help of a class. Let us create a new class, right-click the package and select new ABAP class and give it a meaningful name and description.

    Click Next and save it in our TR.

    Now, let add some code to our Class.

    Definition

    Here, we make use of the interface if_oo_adt_classrun, Provides a light-weight solution for executing an ABAP program without launching the integrated SAP GUI. Furthermore, this feature enables you to display any kind of text and/or content of data into the Console View.

    We cannot execute normal class like how we do it in SAP GUI, as ABAP cloud environment has some restrictions.

    CLASS ztest_cl_populate_travel DEFINITION
      PUBLIC
      FINAL
      CREATE PUBLIC .
    
      PUBLIC SECTION.
        INTERFACES if_oo_adt_classrun.
      PROTECTED SECTION.
      PRIVATE SECTION.
    ENDCLASS.
    

    Implementation

    Insert the following logic to help us populate our table. Save, check and activate it.

    CLASS ztest_cl_populate_travel IMPLEMENTATION.
      METHOD if_oo_adt_classrun~main.
        DATA:
          attachment TYPE /dmo/attachment,
          file_name  TYPE /dmo/filename,
          mime_type  TYPE /dmo/mime_type.
    
    *   clear data
        DELETE FROM ztest_travel_a.
    
        "insert travel demo data"
        INSERT ztest_travel_a  FROM (    "give your database name"
            SELECT
              FROM /dmo/travel AS travel
              FIELDS
                travel~travel_id        AS travel_id,
                travel~agency_id        AS agency_id,
                travel~customer_id      AS customer_id,
                travel~begin_date       AS begin_date,
                travel~end_date         AS end_date,
                travel~booking_fee      AS booking_fee,
                travel~total_price      AS total_price,
                travel~currency_code    AS currency_code,
                travel~description      AS description,
                CASE travel~status    "[N(New) | P(Planned) | B(Booked) | X(Cancelled)]"
                  WHEN 'N' THEN 'O'
                  WHEN 'P' THEN 'O'
                  WHEN 'B' THEN 'A'
                  ELSE 'X'
                END                     AS overall_status,
                @attachment             AS attachment,
                @mime_type              AS mime_type,
                @file_name              AS file_name,
                travel~createdby        AS created_by,
                travel~createdat        AS created_at,
                travel~lastchangedby    AS last_changed_by,
                travel~lastchangedat    AS last_changed_at,
                travel~lastchangedat    AS local_last_changed_at
                ORDER BY travel_id UP TO 20 ROWS
          ).
        COMMIT WORK.
        out->write( |{ sy-dbcnt } Demo data generated for table ztest_travel_a. | ).
      ENDMETHOD.
    ENDCLASS.
    

    Next, select the dropdown on the run icon, and select Run as>ABAP Application. Or simply press F9.

    We have successfully populated our table.

    Verify once by executing(F8) our database table.

  • Fiori Preview

    Now go to our Service Binding again, reactivate the object or simply refresh the package.

    Open the preview URL again, and you should be able to see the data being loaded successfully.

Congratulations!

You have now created a SAP RAP application and you’re one step closer to your learning goals. You now have an idea of the process and structure of a RAP application.

There are many more topics that you must explore after this, as those are crucial for you to excel in your knowledge in RAP understanding.

I will list out few of them:

  • Enhance the Business Object Data Model and Enable OData Streams
  • Enhance the Business Object Behavior With Unmanaged Internal Numbering
  • Enhance the Business Object Behavior With Determinations
  • Enhance the Business Object Behavior With Validations
  • Create an SAP Fiori App and Deploy it to SAP BTP, ABAP Environment
  • Integrate List Report into ABAP Fiori Launchpad
  • Create SAP Fiori Launchpad Space and Page Templates
  • Enhance the Business Object Behavior With Instance Action
  • Enhance the Business Object Behavior With Factory Action
  • Enhance the Business Object Behavior With Dynamic Feature Control
  • Write an ABAP Unit Test for the RAP Business Object

Happy Learning!!!✌️✌️✌️