Working with Event Parameters

Objectives

After completing this lesson, you will be able to:
  • Define CDS Abstract Entities.
  • Raise business events with parameters.
  • Handle business events with parameters.

CDS Abstract Entities

Event parameters and abstract entities of a RAP business event

When you define a RAP business event, you can optionally define a parameter to specify the information that is passed to the event consumers. This information is called the payload of the event.

To specify the type of an event parameter, you have to use a CDS abstract entity.

A CDS abstract entity defines the type properties of a CDS entity without defining a database object. It means that an abstract entity does not build on a data source and you cannot select data from it. As a consequence, the definition of an abstract entity has no as select from or as projection on clause and the element list consist of element names and related data types, preferably data elements but built-in types are also supported.

Note

You can consider CDS abstract entities as a further development of dictionary structure types. They also define a global data type, but in addition they support CDS features like annotations, exposed associations, importing parameters, and so on.

Steps to create a projection view

To create the data definition for a CDS abstract entity, proceed as follows:

  1. Open the context menu on the package or its subnode Core Data ServicesData Definitions.

  2. When prompted for the template, choose Abstract Entity (creation)defineAbstractEntityWithParameters.

  3. Remove the parameter, if you don't need it, and replace the placeholders in the element list with element names and related element types.

Note

There is currently no template for abstract entities without parameters.

Parameters in RAISE ENTITY EVENT

RAISE ENTITY EVENT with parameters

Defining a parameter for a business event, immediately changes the FOR EVENT data type for this event. In addition to the key elements of the BO entity, it now contains all elements of the parameter type, that is, of the abstract entity.

It allows you to supply values for the parameter components when raising the business event.

Business Event Handling with Parameters

Business event handling with parameters

On the business event consumer side, we see the same effect because the importing parameter is typed with the same derived type TABLE FOR EVENT <entity_name>~<event_name>. If the event is defined with a parameter, the row type of this table type contains two component groups: %key with the primary key of the BO entity and %param with the entity elements of the parameter type, that is, of the CDS abstract entity.

Add a Parameter to the Business Event

In this exercise, you add a parameter to the business event that you defined in the previous exercise. For that, you define a CDS abstract entity and use it as parameter type.

Note

In this exercise, replace ## with your group number.

Solution

Repository Object TypeRepository Object ID
CDS Data Definition (Abstract Entity)/LRN/437G_A_EVENT
CDS Behavior Definition (Model)/LRN/437G_R_TRAVEL
ABAP Class (Behavior Pool)/LRN/BP_437G_R_TRAVEL
ABAP Class (Event Handler)/LRN/CL_437G_HANDLER

Task 1: Define a CDS Abstract Entity

Define a CDS abstract entity (suggested name: Z##_A_Event) with a single element of type abp_root_entity_name (suggested name: origin).

Steps

  1. Create a new data definition (suggested name: Z##_A_Event) that defines a CDS abstract entity. Assign it to your own package and use the same transport request as before.

    1. In the Project Explorer view, open the context menu for the Data Definitions folder that is located under the Core Data Services folder in your package ZS4D437_##.

    2. From the context menu, select New Data Definition.

    3. Enter the name of the new data definition and a description and choose Next.

    4. Select the same transport request as before and choose Next.

    5. From the list of templates, choose Abstract Entity (creation)defineAbstractEntityWithParameters and choose Finish.

  2. In the editor that opens, remove the with parameters addition, because the abstract entity does not need parameters.

    1. Adjust the code as follows:

      Code Snippet
      12
      define abstract entity Z##_A_Event {
  3. In the element list, replace placeholder element_name with origin and placeholder element_type with abp_root_entity_name.

    1. Adjust the code as follows:

      Code Snippet
      1234
      define abstract entity Z##_A_Event { origin : abp_root_entity_name; }
  4. Activate the abstract entity.

    1. Press Ctrl + F3 to activate the development object.

Task 2: Add an Event Parameter

Add a parameter to your event and use the abstract entity Z##_A_Event as parameter type. Adjust the RAISE ENTITY EVENT statement and populate the parameter with the name of your root entity in upper case (Z##_R_TRAVEL). Finally, remove the hard-coded value for Origin from the event handler implementation.

Steps

  1. Edit your behavior definition on data model level Z##_R_TRAVEL and locate the definition of the TravelCreated event.

    1. For example, you can press Ctrl + F to invoke a search dialog where you search for EVENT.

  2. Add an event parameter that is typed with your abstract entity.

    1. Adjust the code as follows:

      Code Snippet
      1
      event TravelCreated parameter Z##_A_Event;
  3. Activate the behavior definition and use the Where-Used List to navigate to the RAISE ENTITY EVENT statement for the event.

    1. Press Ctrl + F3 to activate the development object.

    2. In the EVENT statement, place the cursor on the event name and press Ctrl + Shift + G.

    3. On the Get Where-Used List dialog that appears, choose Finish.

    4. From the Where-Used List in the Search view below the editor, choose ZBP_##_R_TRAVEL (Class)Local Class ImplementationRAISE ENTITY EVENT Z##_R_Travel~TravelCreated.

  4. Adjust the RAISE ENTITY EVENT statement. Make sure the data object after the FROM addition contains not only the key values (columns AgencyId and TravelId), but also the name of your root view entity in upper case in the origin column.

    Note

    Either define a data object of type TABLE FOR EVENT Z##_R_Travel~TravelCreated and fill it in a LOOP-ENDLOOP structure before the RAISE ENTITY EVENT statement, or use a VALUE expression after FROM with a FOR iteration inside.
    1. Adjust the code as follows:

      Code Snippet
      12345678910111213
      IF create-travel IS NOT INITIAL. DATA event_in TYPE TABLE FOR EVENT Z##_R_Travel~TravelCreated. LOOP AT create-travel ASSIGNING FIELD-SYMBOL(<new_travel>). APPEND VALUE #( AgencyId = <new_travel>-AgencyId TravelId = <new_travel>-TravelId origin = 'Z##_R_TRAVEL' ) TO event_in. ENDLOOP. RAISE ENTITY EVENT Z##_R_Travel~TravelCreated FROM event_in. ENDIF.

      Alternative implementation using a VALUE expression:

      Code Snippet
      12345678910
      IF create-travel IS NOT INITIAL. RAISE ENTITY EVENT Z##_R_Travel~TravelCreated FROM VALUE #( FOR <new_travel> IN create-travel ( AgencyId = <new_travel>-AgencyId TravelId = <new_travel>-TravelId origin = 'Z##_R_TRAVEL' ) ). ENDIF.
  5. Activate the behavior implementation and use the Where-Used List for the event to navigate to the handler method.

    1. Press Ctrl + F3 to activate the development object.

    2. If the Where-Used List for the event is no longer visible, place the cursor on the event name in the RAISE ENTITY EVENT statement and press Ctrl + Shift + G.

    3. On the Get Where-Used List dialog that appears, choose Finish.

    4. From the Where-Used List in the Search view below the editor, choose ZCL_##_HANDLER (Class)Local Class ImplementationFOR Travel~TravelCreated.

  6. In the MODIFY ENTITIES statement, remove the data object after WITH and replace it with a CORRESPONDING expression, having the event parameter as input.

    Note

    This is sufficient because the event parameter now contains the origin component.
    1. Adjust the code as follows:

      Code Snippet
      12345
      MODIFY ENTITIES OF /LRN/437_I_TravelLog ENTITY TravelLog CREATE AUTO FILL CID FIELDS ( AgencyID TravelID Origin ) WITH CORRESPONDING #( new_travels ).
  7. Remove or comment the superfluous data declaration and the loop.

    1. Select the DATA statement, the ENDLOOP statement, and all code rows between them and press Ctrl + < to insert a comment sign at the beginning of each selected code row.

  8. Activate the handler class and retest the event handling.

    1. Press Ctrl + F3 to activate the development object.

    2. Reopen the preview of your OData UI service and choose Create to create a new flight travel.

    3. Enter all mandatory fields for the flight travel and choose Create.

    4. Take note of the values displayed in the Agency ID field and the Travel ID field.

    5. Open the preview for OData UI service /LRN/UI_437_TRAVELLOG_O2.

    6. Choose Go. (If there are too many entries, enter Z##_R_TRAVEL under Root Entity Name and choose Go again.)

      Result

      You see an entry that corresponds to the flight travel that you created.