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:
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; }
}
*"* 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