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 Factory Action

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

Overview

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

You will learn

  • How to define factory action
  • How to implement factory action
  • How to preview and test enhanced travel app In the previous exercise, you’ve defined and implemented instance action. In the present exercise, you will learn how to add factory action.
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

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 factory action

Now, you will define, implement, and expose the action copyTravel, an instance-bound factory action used to copy one or more travel instances and creates new instances based on the copied data. A new travel ID is assigned to a new travel instance by the unmanaged internal early numbering.

Define the instance factory action copyTravel in the behavior definition

  1. Go to the behavior definition

    bdef icon
    bdef icon
    ZRAP100_R_TRAVELTP_### and insert the following code snippet after the action defined in the previous step.

  2. Add following:

    ABAP
    factory action copyTravel [1];

    The result should look like this:

    Travel BO Behavior Definition
    Travel BO Behavior Definition

    Short explanation:

    For factory actions, the same rules apply as for instance non-factory actions with the following differences:

    • Instance factory actions are specified with the keyword factory action before its name.
    • Output parameters are not allowed. Factory actions always produce one new BO entity instance with possible child entity instances. It is therefore not necessary to specify the result parameter.
    • The cardinality must always be [1] for factory actions.
    • The result of a factory action is returned in the implicit response parameter mapped by a mapping between the BDEF derived type %cid and the key of the newly created entity instance.

    Further information can be found here: CDS BDL - action, factory

  3. Save

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

Step 2 Implement factory action
+
Step 3 Expose and test factory action
+
Step 4 Add instance actions acceptTravel and rejectTravel (optional)
+
Step 5 Implement action methods (optional)
+
Step 6 Expose and test actions (optional)
+
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 factory action 2. Implement factory action 3. Expose and test factory action 4. Add instance actions acceptTravel and rejectTravel (optional) 5. Implement action methods (optional) 6. Expose and test actions (optional) 7. Test yourself

Learn more →