How to register a validation event in SAP RAP for create and update?

At a certain point in our RESTful Application Programming (RAP) model, we want maybe to extend it with our own logic. Mostly, we want to this, when we create or update an entry in our database, as in the following image:

After we continue from this dialog and click save, we want to trigger our logic to verify, if the carried ID and connection ID has been unique. This is also an example from the official SAP Learning Journey, but I try to describe it here a bit clearer, especially when it comes to definition.

If we want to add custom ABAP logic into our RAP project, we need to extend our behavior definition file. For that we open our ZR_CONNECTIONS behavior definition file and doing the following changes, as described in the comments.

//..
{
//...

  draft action Edit;
  draft action Activate optimized;
  draft action Discard;
  draft action Resume;

  // Adding here the action to trigger our validation
  draft determine action Prepare {
    validation CheckSemanticKey;
    validation CheckTotalSeats;
  }

  // Our validation gets registered here for CREATE and UPDATE events.
  validation CheckSemanticKey on save { create; update; }
  
  // Another validation method, which will be extended later in this post.
  // It just listens to create and update events for the field TotalSeats.
  validation CheckTotalSeats on save { create; update; field TotalSeats; }

  mapping for ZCONNECTIONS
  {
    CarrID = carr_id;
    ConnID = conn_id;
    FlightDate = flight_date;
    FlightTime = flight_time;
    TotalSeats = total_seats;
    LocalCreatedBy = local_created_by;
    LocalCreatedAt = local_created_at;
    LocalLastChangedBy = local_last_changed_by;
    LocalLastChangedAt = local_last_changed_at;
    LastChangedAt = last_changed_at;
  }
}

Now we can get a quick fix (Ctrl + 1), where we get a proposal to create an implementation method in our ZBC_CONNECTIONS file. If we apply this proposal, it will create inside the local handler class the following implementation:

CLASS lhc_zr_connections DEFINITION INHERITING FROM cl_abap_behavior_handler.
  PRIVATE SECTION.
    METHODS:
      get_global_authorizations FOR GLOBAL AUTHORIZATION
        IMPORTING
        REQUEST requested_authorizations FOR zr_connections
        RESULT result,
        
      " Our definitions in the local behavior implementation class
      CheckSemanticKey FOR VALIDATE ON SAVE
        IMPORTING keys FOR zr_connections~CheckSemanticKey,
        
      CheckTotalSeats FOR VALIDATE ON SAVE
        IMPORTING keys FOR zr_connections~CheckTotalSeats.
ENDCLASS.

CLASS lhc_zr_connections IMPLEMENTATION.
  METHOD get_global_authorizations.
  ENDMETHOD.
  
  METHOD CheckSemanticKey.
    " Our custom logic can be put here.
  ENDMETHOD.
  METHOD CheckTotalSeats.
    " Our custom logic for later can be put here.
  ENDMETHOD.

ENDCLASS.

For now, we are ready in to continue to build some custom logic inside our local class and method.

Addition CheckTotalSeats

As promised, we want to make an example how to exactly validate the total seats for a connection. We want that the user can not set a number higher than 350 seats for one connection. So, we have to validate this in the following way:

METHOD CheckTotalSeats.
    " Entity Manipulation Language to read the total seats entered.
    READ ENTITIES OF zr_connections IN LOCAL MODE ENTITY zr_connections
    FIELDS ( CarrID TotalSeats ) WITH CORRESPONDING #( keys )
    RESULT DATA(connections).

    " Our maximum of seats per connection.
    CONSTANTS cv_max_seats TYPE i VALUE 350.
    
    DATA lo_message TYPE REF TO if_abap_behv_message.

    LOOP AT connections ASSIGNING FIELD-SYMBOL(<fs_connection>).
    
      " Checking here if the seats entered exceeds the max seats constant.
      IF <fs_connection>-TotalSeats GT cv_max_seats.
      
        " If so we are loading a message from the global message class ZCONNECTION_MESSAGES.
        lo_message = me->new_message(
          id = 'ZCONNECTION_MESSAGES'
          number = 004 " The total seats should not exceed &1 seats. You wanted &2 seats.
          severity = ms-error
          v1 = cv_max_seats
          v2 = <fs_connection>-TotalSeats
        ).

        " Appending the message to the RAP runtime to show it to the user.
        APPEND VALUE #(
          %tky = <fs_connection>-%tky
          %msg = lo_message
          %element-carrid = if_abap_behv=>mk-on
          %element-connid = if_abap_behv=>mk-on
        ) TO reported-zr_connections.

        " Abort the save by adding an entry to the failed component.
        APPEND VALUE #(
          %tky = <fs_connection>-%tky
          CarrId = <fs_connection>-carrid
          ConnID = <fs_connection>-connid
        ) TO failed-zr_connections.
      ENDIF.
    ENDLOOP.
  ENDMETHOD.

When we now check our implementation of the validation and enter for example the number 400, we can see the following error message:

“The total seats should not exceed 350 seats. You wanted 400 seats.” as primary error message. In the background, we see also the failed record “Resolve data inconsistencies to save changes.”

If you want to modify data during the save process, you might find more information in the determination process.


Comments

2 responses to “How to register a validation event in SAP RAP for create and update?”

  1. What if we dont wish to see the error “Resolve data inconsistencies to save changes.” and just the error message “The total seats should not exceed 350 seats. You wanted 400 seats.”

    1. evren Tomrukcu Avatar
      evren Tomrukcu

      What is the solution to this problem?

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ad blocker detected

We have detected that you are using extensions to block ads. Please support us by disabling your ad blocker.

Powered By
100% Free SEO Tools - Tool Kits PRO