Behavior Extension – an example of developer extensibility
2023-11-24 21:48:49 Author: blogs.sap.com(查看原文) 阅读量:6 收藏

Recently I got to apply one of the developer extensibility use cases, i.e., RAP behavior extension in a standard app. In this blog post I am sharing the example of such an ABAP extension implementation.

My use case was to do a validation and provide a warning message at reservation document item level when creating or editing an reservation document in ‘Manage Manual Reservations’ app (F4839).

The app Manage Manual Reservations uses a RAP BO interface I_ReservationDocument. So our target interface BO would be this which will be extended.

Steps:

  1. Before creating the actual development objects, there are some pre-requisites. Since developer extensions are cloud-ready, it is a best practice to create them in a separate package if you are working on an on-premise systems. Please follow this blog post by Vijay Sharma to set up the packages and software component in an on-premise SAP S/4HANA system.
  2. I extended the standard behavior as below:
    extension using interface i_reservationdocumenttp
    implementation in class zbp_e_reservationdocumentheade unique;
    
    extend behavior for ReservationDocument
    {
      extend draft determine action Prepare {
        validation ReservationDocumentItem~validateStockItem;
      }
    }
    
    extend behavior for ReservationDocumentItem
    {
      validation validateStockItem on save { field Reservation; create; update; }
    }
  3.  The behavior implementation class was implemented. This is no different than the usual behavior implementation.
*"* use this source file for the definition and implementation of
*"* local helper classes, interface definitions and type
*"* declarations
CLASS lhc_reservationdocumentheader DEFINITION INHERITING FROM cl_abap_behavior_handler.
  PUBLIC SECTION.

    CONSTANTS:
      validate_stock_item TYPE string VALUE 'VALIDATE_STOCK_ITEM',
      serial_num          TYPE  zser_num     VALUE  '001'.

  PRIVATE SECTION.
    METHODS validatestockitem FOR VALIDATE ON SAVE
      IMPORTING keys FOR reservationdocumentitem~validatestockitem.

ENDCLASS.

CLASS lhc_reservationdocumentheader IMPLEMENTATION.

  METHOD validatestockitem.

*Get the reservation document items
    READ ENTITIES OF i_reservationdocumenttp IN LOCAL MODE
      ENTITY reservationdocumentitem
       FIELDS ( product plant storagelocation )
      WITH CORRESPONDING #( keys )
      RESULT DATA(lt_items).

    LOOP AT lt_items ASSIGNING FIELD-SYMBOL(<ls_res_item>)
      WHERE reservationitmismarkedfordeltn EQ abap_false
        AND reservationitemisfinallyissued EQ abap_false
        AND product IS NOT INITIAL
        AND plant IS NOT INITIAL.  "#EC CI_STDSEQ
*If stock is zero for the material and plant combination,
*issue a warning message
      SELECT SUM( labst ) FROM nsdm_e_mard
        INTO @DATA(quantity_in_stock)
          WHERE matnr EQ  @<ls_res_item>-product
            AND werks EQ @<ls_res_item>-plant.
      IF sy-subrc EQ 0.
        IF quantity_in_stock EQ 0.
          APPEND VALUE #( %tky = <ls_res_item>-%tky
                %state_area = validate_stock_item
                %path-reservationdocument-%is_draft = <ls_res_item>-%is_draft
                %path-reservationdocument-reservation = <ls_res_item>-reservation
               )
      TO reported-reservationdocumentitem.
          APPEND VALUE #( %tky        = <ls_res_item>-%tky
                              %state_area = validate_stock_item
                              %msg        = NEW zcm_reservation_message(
                                                  textid   = zcm_reservation_message=>stock_unavailable
                                                  severity = if_abap_behv_message=>severity-warning
                                                  product = <ls_res_item>-product
                                                  plant = <ls_res_item>-plant
                                                   )
                              %element-resvnitmrequiredqtyinentryunit = if_abap_behv=>mk-on
                              %path-reservationdocument-%is_draft   = <ls_res_item>-%is_draft
                              %path-reservationdocument-reservation = <ls_res_item>-reservation
                            )
              TO reported-reservationdocumentitem.
        ENDIF.
      ENDIF.

    ENDLOOP.

  ENDMETHOD.


ENDCLASS.

Test:

Now when I try to create or update the reservation document item, the error message is thrown as expected.

Reference:

https://blogs.sap.com/2019/07/25/sap-s4hana-extensibility-a-learning-journey/

https://blogs.sap.com/2022/10/25/new-extenisbility-guide-for-s-4hana-is-available/

SAP Official Documentation on Developer Extensibility on ABAP Platform


文章来源: https://blogs.sap.com/2023/11/24/behavior-extension-an-example-of-developer-extensibility/
如有侵权请联系:admin#unsafe.sh