Get Started with ABAP Programming on SAP BTP

Adding ABAP logic to a RAP Application

Objectives
After completing this lesson, you will be able to:

After completing this lesson, you will be able to:

  • Implement the behavior of a RAP Business Object

Creating Validations

Checking the Semantic Key

As shown in the figure, in an RAP data model, the key of a database table is often made up of the client field and a UUID field, whose value is assigned automatically by the RAP runtime when you create a new instance of the business object. This field combination is sufficient to ensure that the system can identify each record in the table uniquely. However, as well as this technical key, each object also has a semantic key - in this case the combination of airline and flight number, which must also be unique according to the business logic. In order to ensure the uniqueness of this field combination, you must implement your own check in the form of a validation.

You declare validations in the behavior definition of the CDS view entity, and implement them in the behavior implementation class.

Input Checks in the App

As well as checking the semantic key, there are other checks that you need to perform. For example, although the generated app allows you to create, read, update and delete data, it does not yet contain any consistency checks. Consequently, you can create flight connections for airlines that don't exist, or where the departure and destination airports are the same.

To prevent this from happening, you define further validations in the behavior definition, and implement them in the behavior implementation class.

Creating Message Texts

Before you create the validation, you must create the texts that you want to display. You do this using a message class. A message class is a collection of up to 1000 messages that belong to a particular application area. As shown in the figure, each text has a number which identifies the message uniquely within the message class.

To create a new message class, choose FileNewOther… and type message into the filter field. Double-click the Message Class entry in the hit list, then enter a package, name, and description for the new message class. Choose Next.

Assign the message class to a transport request and choose Finish.

Messages can also contain placeholders, which are replaced with concrete values when the message is displayed. Placeholders are denoted by the ampersand symbol followed by a number. You can use up to four placeholders in each message.

Defining the Validation

To define a validation, you add a validation declaration to the behavior definition of your business object. In this example, the validation should be performed whenever the user saves a data record, and this could be either when they create the record or if they subsequently change it.

When you define the validation in the behavior definition, a warning tells you that the corresponding method does not exist. Use a quick fix (key combination Ctrl + 1) to add the method to the behavior implementation. The behavior implementation is a local class within your behavior pool. The method definition contains the addition FOR VALIDATE ON SAVE, which identifies it as the implementation of the validation. It has an importing parameter KEYS. This is an internal table containing the keys of the created or changed objects. You use these to read the actual data that the user has entered.

The addition FOR Connection~CheckSemanticKey links the method with the validation CheckSemanticKey from the behavior definition. Here, Connection is the alias name of the view entity Z_I_CONNECTION.

The Validation Process

How to Validate the Semantic Key

Validate the Airline

Steps

  1. Define a validation CheckAirline to check that the airline that the user entered exists.

    1. In the Project Explorer, navigate to the behavior definition Z##_I_CONN and add the following line: validation CheckAirline on save { create; update }.

    2. Activate the behavior definition.

  2. Create the implementation of the validation using a Quick Fix.

    1. Position the cursor on the name of the validation and choose Ctrl + 1.

    2. Double-click the entry Add validation....

  3. Use the EML READ ENTITIES statement to read the data that the user entered. Ensure that the field CARRID is read. Use an inline declaration for the result set.

    1. Enter the following code in the method implementation. Replace ## with your group number:

      Code snippet
      
      READ ENTITIES OF z##_I_conn IN LOCAL MODE
      ENTITY Connection FIELDS (  carrid  )
      WITH CORRESPONDING #( keys )
      RESULT DATA(connections).
      Copy code
  4. In a loop over the data that you just read, check that the airline exists. Use a SELECT statement that returns the literal abap_trueif the airline exists. Use the CDS View /dmo/i_carrieras the data source of the statement.

    1. Enter the following code in the method implementation. Replace ## with your group number:

      Code snippet
      
      SELECT SINGLE FROM /dmo/i_carrier
      FIELDS @abap_true
      WHERE AirlineID = @carrier-carrID
      INTO @DATA(exists).
      Copy code
  5. If the value of exists is abap_false, create a message object with message ID ZS4D400, number 002, severity ms-errorand parameter v1 carrier-carrid.

    1. Enter the following code in the method implementation:

      Code snippet
      
      IF exists = abap_false.
      DATA(message) = me->new_message(
      id = 'ZS4D400'
      number = '002'
      severity = ms-error
      v1 = carrier-carrid ).
      Copy code
  6. Declare the structure reported_record with the type LIKE LINE OF reported-connection. Add the record to the reported structure.

    1. Enter the following code in the method implementation:

      Code snippet
      
      DATA reported_record LIKE LINE OF reported-connection. 
      
      reported_record-%tky = carrier-%tky.
      reported_record-%msg = message.
      reported_record-%element-carrid = if_abap_behv=>mk-on.
      
      
      APPEND reported_Record TO reported-connection.
      Copy code
  7. Declare the structure failed_record with the type LIKE LINE OF failed-connection. Add the record to the failed structure and close the open IF and LOOP control structures.

    1. Enter the following code in the method implementation:

      Code snippet
      
      DATA failed_record LIKE LINE OF failed-connection. 
      
      failed_record-%tky = carrier-%tky.
      APPEND failed_Record TO failed-connection.
      ENDIF.
      ENDLOOP.
      Copy code

Validate the Airport Codes

Steps

  1. Define a validation CheckOriginDestination to check that the departure and arrival airports of the flight connection are different.

    1. In the Project Explorer, navigate to the behavior definition Z##_I_CONN and add the following line: validation CheckOriginDestination on save { create; update }.

    2. Activate the behavior definition.

  2. Create the implementation of the validation using a Quick Fix.

    1. Position the cursor on the name of the validation and choose Ctrl + 1.

    2. Double-click the entry Add validation....

  3. Use the EML READ ENTITIES statement to read the data that the user entered. Ensure that the fields AIRPORTFROM and AIRPORTTO are read. Use an inline declaration for the result set.

    1. Enter the following code in the method implementation. Replace ## with your group number:

      Code snippet
      
      READ ENTITIES OF z##_i_conn IN LOCAL MODE 
      ENTITY Connection 
      FIELDS ( AirportFrom AirportTo ) 
      WITH CORRESPONDING #( keys ) 
      RESULT DATA(connections).
      Copy code
  4. In a loop over the data that you just read, check whether AIRPORTFROMand AIRPORTTO are the same. If they are, create a message with id ZSD4D00, number 003, and severity ms-error.

    1. Enter the following code in the method implementation. Replace ## with your group number:

      Code snippet
      
      LOOP AT connections INTO DATA(connection).
      IF connection-cityfrom = connection-cityto.
      DATA(message) = me->new_message(
      id = 'ZS4D400'
      number = '003'
      severity = ms-error ).
      
      
      Copy code
  5. Declare the structure reported_record with the type LIKE LINE OF reported-connection. Add the record to the reported structure.

    1. Enter the following code in the method implementation:

      Code snippet
      
      DATA reported_record LIKE LINE OF reported-connection. 
      
      reported_record-%tky = connection-%tky.
      reported_record-%msg = message.
      reported_record-%element-carrid = if_abap_behv=>mk-on.
      reported_record-%element-connid = if_abap_behv=>mk-on.
      APPEND reported_Record TO reported-connection.
      Copy code
  6. Declare the structure failed_record with the type LIKE LINE OF failed-connection. Add the record to the failed structure and close the open IF and LOOP control structures.

    1. Enter the following code in the method implementation:

      Code snippet
      
      DATA failed_record LIKE LINE OF failed-connection. 
      
      failed_record-%tky = connection-%tky.
      APPEND failed_Record TO failed-connection.
      
      ENDIF.
      ENDLOOP.
      Copy code

Creating Determinations

In this section, you will learn how to complete the data using determinations.

Determine Cities Based on Airport Codes

In the example app, the flight connection entity contains a departure airport, city, and country, and an arrival airport, city, and country. While it would be possible to force the user to enter all of this information, it is better in terms of user experience and data consistency to have the user enter the airport code and for the app to read the corresponding city and country from the database. You can perform this kind of task in a RAP application using a determination.

You will first implement the determination. Afterwards, you will learn how to deactivate input for the fields that have been filled automatically.

Defining the Determination

You define a determination in the behavior definition of a business object. The determination here is called getCities, it will be called whenever the business object is saved and at least one of the AirportFrom and AirportTo fields has changed. You can use a quick fix in the behavior definition to create the corresponding method in the behavior implementation.

The Determination Process

Let's explore each step of the determination process.

How to Determine the Cities and Countries

Save progress to your learning plan by logging in or creating an account