RAP - Usage of Static Default Factory Action

Hello Everyone! 👋

The Usage of Static Default Factory Action in RAP

Have you ever encountered a scenario in a RAP managed where you need to execute custom logic when the Create button is pressed?

This is possible using a static default factory action, which is automatically triggered when the Create button is clicked in the SAP Fiori Elements UI.

This approach can be applied in various situations, such as:

  • Setting default values from different sources
  • Performing parameterized create operations

The Use of Factory Actions and Types:

Factory actions allow you to create entity instances:

  • Static factory actions are not bound to any specific instance but apply to the entire entity. They are ideal for creating instances with predefined default values.
  • Instance factory actions can be used when you need to copy certain values from an existing instance.

When defined with the default option, a static factory action is automatically called by the SAP Fiori Elements UI instead of the standard create operation.

In the following example, I have implemented a new static default factory action to create customer data, which is triggered when I press the Create button in the UI.

Step 1 – Define static default factory action in R_* BDEF

define behavior for ZR_ROOT alias ROOT
{
  create;
  update;
  delete;

  static default factory action createCustomer parameter ZD_CustomerData [1];
}

Here, createCustomer will be triggered instead of the standard create.

Step 2 – Create abstract entity for action parameters

@EndUserText.label: 'Customer Data'
define abstract entity ZD_CustomerData
{
    @EndUserText.label: 'Customer ID'
    CustomerId : int4;

    @EndUserText.label: 'First Name'
    FirstName  : abap.char(20);

    @EndUserText.label: 'Last Name'
    LastName   : abap.char(20);
}

This entity defines the parameters passed to the static action.

Step 3 – Implement custom logic in the action handler

METHOD createCustomer.
  CHECK keys IS NOT INITIAL.

  MODIFY ENTITIES OF ZR_Root IN LOCAL MODE
    ENTITY Root
    CREATE FIELDS ( id Fname Lname Age )
    WITH VALUE #(
      FOR key IN keys (
        %cid       = key-%cid
        %is_draft  = key-%param-%is_draft
        Id         = key-%param-CustomerId
        Fname      = key-%param-FirstName
        Lname      = key-%param-LastName
        Age        = 25
      )
    )
    MAPPED   mapped
    FAILED   failed
    REPORTED reported.
ENDMETHOD.

Here, the incoming parameters are mapped to the entity fields before creation.

Step 4 – Expose the action in ZC_* (projection) BDEF

define behavior for ZC_Root alias Root
{ 
  use create;
  use update;
  use delete;

  use action createCustomer;
}

This makes the action available in the projection layer.

Result

🎥 Demo: Static Default Factory Action

When the Create button is clicked in the SAP Fiori Elements UI:

  • The createCustomer static default factory action is triggered automatically.
  • Custom parameter handling and default value logic are executed before entity creation.
  • The standard create operation is bypassed (even if enabled).

Note:
This action is available from S/4 HANA 2023 and the latest version in the Public Cloud.

#RAP #ABAP #SAPFiori #S4HANA #ABAPCloud #FactoryAction

Comments

Popular posts from this blog

Fiori URL Generation and Navigation Using CL_LSAPI_MANAGER

RAP - Real Time UI Updates Using Event-Driven Side Effects

ABAP - Create CSV Files Easily Using CL_CSV_FACTORY