SAP Home Learn Build Integrate Model Operate Extend with AI ConnectTutorial navigator Knowledge Graph API Devtoberfest Developer Advocates App Space

Manage my Account SAP Devs YouTube ↗ Learnings ↗ Community ↗ Provide Feedback ↗
Logout
⤢ Open full site

Enhance the Business Object Behavior With Instance Action

Enhance the business object behavior using instance action with SAP BTP ABAP environment.

Overview

🎓 beginner 20 min. SAP BTP ABAP EnvironmentBeginnerABAP DevelopmentSAP Business Technology Platform

You will learn

  • How to define actions
  • How to implement actions
  • How to preview and test enhanced travel app In the previous exercise, you’ve defined and implemented two validations. In the present exercise, you will learn how to add different instance-bound actions (aka instance actions) to your application: instance non-factory actions w/o input parameters, instance non-factory action with input parameters, and instance-bound factory actions. For simplification reasons, _non-factory actions_ will be called _actions_ in the exercise description.
Merve Temel M Merve Temel April 17, 2024
Created by March 9, 2023
Contributors

Prerequisites

Prerequisites

  • You need to have access to an SAP BTP, ABAP environment, or SAP S/4HANA Cloud, ABAP environment or SAP S/4HANA (release 2022 or higher) system. For example, you can create free trial user on SAP BTP, ABAP environment.
  • You have downloaded and installed the [latest ABAP Development Tools (ADT)] (https://tools.hana.ondemand.com/#abap) on the latest Eclipse© platform.
  • You have created an ABAP Cloud Project.
  • Make sure, your system has the ABAP flight reference scenario. If your system hasn’t this scenario. You can download it here. The trial systems have the flight scenario included.

Steps

Intro

Please note: The purpose of the different exercises is to show you how to implement the different instance action types - and less on having the perfect business scenario.


Reminder: Do not forget to replace the suffix placeholder ### with your chosen or assigned group ID in the exercise steps below.

About: Actions

In the RAP context, an action is a non-standard operation that change the data of a BO instance. They are self-implemented operations.
Two main categories of actions can be implemented in RAP:

  • Non-factory actions: Defines a RAP action which offers non-standard behavior. The custom logic must be implemented in the RAP handler method FOR MODIFY. An action per default relates to a RAP BO entity instance and changes the state of the instance. An action is related to an instance by default. If the optional keyword static is used, the action is defined as static action. Static actions are not bound to any instance of a RAP BO entity but relate to the complete entity.

  • Factory actions: Factory actions are used to create RAP BO entity instances. Factory actions can be instance-bound (default) or static. Instance-bound factory actions can copy specific values of an instance. Static factory actions can be used to create instances with prefilled default values.

Further reading: Actions | CDS BDL - non-standard operations | Implicit Response Parameters | ABAP EML - response_param

Step 1 Define an instance action

You will now define, implement, and expose the action deductDiscount, a non-factory instance-bound action returning itself. The action offers the possibility to deduct a certain percentage from the booking fee ( BookingFee ) of a travel instance.

The discount percentage can either be fix (30% in the present exercise) in the action implementation or be freely specified by the end-user or the calling APIs by offering an action with input parameters.

You will get familiar with both action implementations, i.e. action without and action with input parameters in the present exercise.

Hint: First, define the non-factory, instance action deductDiscount in the behavior definition of the Travel entity. This action has no input parameter.

  1. Go to your behavior definition
    bdef icon
    bdef icon
    ZRAP100_R_TRAVELTP_### define the instance action without input parameter. For that, insert the following code snippet after the defined validations as shown on the screenshot below.
```ABAP
action deductDiscount result [1] $self; 
```      

 ![Travel BO Behavior Definition](https://raw.githubusercontent.com/sap-tutorials/abap-core-development/main/tutorials/abap-environment-rap100-instance-action/pp.png)      

 **Short explanation**:  
 - The name of the instance action is specified after the keyword **action**

 - The keyword **result** defines the output parameter of the action.

    - Its cardinality is specified between the square brackets (`[cardinality]`). It is a mandatory addition.  

    - **`$self`** specifies that the type of the result parameter is the same type as the entity for which the action or function is defined - i.e. the **Travel** entity type in the present exercise. The return type of the result parameter can be an entity or a structure.     

  - **Note**: The output parameter **result** can be used to store the result of an action or function in an internal table. However, it does not affect the result of an action or function that is committed to the database.   

  **Further reading**: [Action Definition](https://help.sap.com/viewer/923180ddb98240829d935862025004d6/Cloud/en-US/14ddc6b2442b4b97842af9158a1c9c44.html)
  1. Save

    save icon
    save icon
    and activate
    activate icon
    activate icon
    the changes.

  2. Now, declare the required method in behavior implementation class with the ADT Quick Fix. Set the cursor on the deductDiscount, and press Ctrl+1 to open the Quick Assist view.

    Select the entry Add method for action deductDiscount of entity zrap100_r_traveltp_### … in the view to add the required method to the local handler class.

    The result should look like this:

    Travel BO Behavior Pool
    Travel BO Behavior Pool

  3. Save

    save icon
    save icon
    the changes.

  4. Set the cursor on the method name, deductDiscount, press F3 to navigate to the declaration part of the local handler class of the behavior pool ZRAP100_BP_TRAVELTP_###.

    Travel BO Behavior Pool
    Travel BO Behavior Pool

  5. In the declaration part set the cursor on the method name deductDiscount, press F2, and examine the full method interface.

    Travel BO Behavior Pool
    Travel BO Behavior Pool

    Short explanation:

    The addition FOR MODIFY after the method name, together with the addition FOR ACTION after the importing parameter, indicates that this method provides the implementation of an action. Method signature for the non-factory instance action deductDiscount:

    • IMPORTING parameter keys - a table containing the keys of the instances on which the action must be executed
    • Implicit CHANGING parameters (aka implicit response parameters):
    • result - used to store the result of the performed action.
    • mapped - table providing the consumer with ID mapping information.
    • failed - table with information for identifying the data set where an error occurred.
    • reported - table with data for instance-specific messages.

    Please note: An action is implemented in a FOR MODIFY method with the addition FOR ACTION. The signature of an action method always depends on the type of action: factory or non-factory, and instance or static. The rules for implementing an action operation in a RAP business object are explained in the respective Implementation Contract.

    Hint: Further reading: Action Implementation| Implementation Contract: Action

    Go ahead with the implementation of the action method.

Step 2 Implement the action method
+
Step 3 Expose and test action
+
Step 4 Add an input parameter
+
Step 5 Adjust the action method
+
Step 6 Test the action with parameter
+
Step 7 Test yourself
+

Resources

Discussion

Share feedback on this tutorial or join the conversation in SAP Community.

Submit detailed feedback Discuss in Community
Steps
Step 1 of 7
1. Define an instance action 2. Implement the action method 3. Expose and test action 4. Add an input parameter 5. Adjust the action method 6. Test the action with parameter 7. Test yourself

Learn more →