8

Throw-away code to test BAPI_RESERVATION_CREATE1

 1 year ago
source link: https://blogs.sap.com/2023/02/15/short-snippet-bapi_reservation_create1/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
February 15, 2023 3 minute read

Throw-away code to test BAPI_RESERVATION_CREATE1

No blabla. Not a code for production. Quick and dirty. Just in case you ask, if you want the code to be improved, you can copy the code and post your own post with fixed code, but consider the SAP copyright notice, and that would be nice of you if you add a link to my post.

This is just an executable program to help developers quickly duplicate an existing reservation (transaction codes MB21, MB22, MB23), and see how parameters are filled. Just used it in ECC 6.0.

EDIT: by default the custom fields are not considered (EXTENSIONOUT and EXTENSIONIN), I have added a chapter at the end to explain how to consider them.

REPORT.

PARAMETERS p_rsnum TYPE rkpf-rsnum.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_rsnum.
  CALL FUNCTION 'MB_SELECT_RESERVATION'
    EXPORTING
      hilfe = 'HLPR'
    IMPORTING
      rsnum = p_rsnum.

START-OF-SELECTION.

  DATA: sl_header_detail TYPE bapi2093_res_head_detail,
        tl_postes_detail TYPE TABLE OF bapi2093_res_item_detail,
        tl_return        TYPE TABLE OF bapiret2,
        tl_extensionout  TYPE TABLE OF bapiparex.

  CALL FUNCTION 'BAPI_RESERVATION_GETDETAIL1'
    EXPORTING
      reservation        = p_rsnum
    IMPORTING
      reservation_header = sl_header_detail
    TABLES
      reservation_items  = tl_postes_detail
      return             = tl_return
      extensionout       = tl_extensionout.

  IF 0 = REDUCE i( INIT i = 0 FOR <m> IN tl_return WHERE ( type CA 'AEX' ) NEXT i = i + 1 ).

    DATA: sl_header      TYPE bapi2093_res_head,
          tl_postes      TYPE TABLE OF bapi2093_res_item,
          tl_segments    TYPE TABLE OF bapi_profitability_segment,
          tl_extensionin TYPE TABLE OF bapiparex,
          vl_reservation TYPE  bapi2093_res_key-reserv_no.

    sl_header = CORRESPONDING #( sl_header_detail ).
    tl_postes = CORRESPONDING #( tl_postes_detail MAPPING entry_qnt = quantity ).
    tl_extensionin = tl_extensionout.
    CALL FUNCTION 'BAPI_RESERVATION_CREATE1'
      EXPORTING
        reservationheader    = sl_header
      IMPORTING
        reservation          = vl_reservation
      TABLES
        reservationitems     = tl_postes
        profitabilitysegment = tl_segments " <======= ?
        return               = tl_return
        extensionin          = tl_extensionin.
    IF 0 = REDUCE i( INIT i = 0 FOR <m> IN tl_return WHERE ( type CA 'AEX' ) NEXT i = i + 1 ).

      DATA: sl_return TYPE bapiret2.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait   = 'X'
        IMPORTING
          return = sl_return.

    ENDIF.
  ENDIF.

  DATA(result) = COND string( WHEN 0 = REDUCE i( INIT i = 0 FOR <m> IN tl_return WHERE ( type CA 'AEX' ) NEXT i = i + 1 )
                        THEN |Created: { vl_reservation }|
                        ELSE 'Failed' ).

  cl_demo_output=>new( )->write_data( result )->write_data( tl_return )->write_data( sl_return )->display( ).

Custom fields (EXTENSIONOUT and EXTENSIONIN)

I’m talking about the custom fields you may add in the tables RKPF and RESB.

So that BAPI_RESERVATION_GETDETAIL1 returns the custom fields, I think that the BAdI MB_RES_BAPI_DETAIL1 was to do that but it doesn’t work in my ECC version, so I did an enhancement at the end of BAPI_RESERVATION_GETDETAIL1.

So that BAPI_RESERVATION_CREATE1 transfers the custom fields to the table, you must implement the BAdI MB_RES_BAPI_CREATE1.

The code below requires that you create the DDIC structures ZBAPI_TE_RKPF and ZBAPI_TE_RESB, with the custom columns that you have defined in the tables RKPF and RESB, and they should have the same names. ZBAPI_TE_RESB must also contain the RSPOS column (of type RSPOS) at the position you want (the first one is nice).

You can adapt the code if you want to work only with either RKPF or RESB.

Code at the end of BAPI_RESERVATION_GETDETAIL1:

TYPES: BEGIN OF zz_ty_valueparts,
  structure TYPE te_struc,
  BEGIN OF parts,
    _1 TYPE valuepart,
    _2 TYPE valuepart,
    _3 TYPE valuepart,
    _4 TYPE valuepart,
  END OF parts,
END OF zz_ty_valueparts.
DATA: zz_zbapi_te_rkpf       TYPE zbapi_te_rkpf,
      zz_table_zbapi_te_resb TYPE TABLE OF zbapi_te_resb.

SELECT SINGLE *
    FROM rkpf
    WHERE rsnum = @reservation
    INTO CORRESPONDING FIELDS OF @zz_zbapi_te_rkpf.
IF sy-subrc = 0.
  SELECT *
      FROM resb
      WHERE rsnum = @reservation
      INTO CORRESPONDING FIELDS OF TABLE @zz_table_zbapi_te_resb.
  IF sy-subrc = 0.
    extensionout[] = VALUE #(
        ( CONV #( VALUE zz_ty_valueparts(
          structure  = 'ZBAPI_TE_RKPF'
          parts      = CONV #( CORRESPONDING zbapi_te_rkpf( zz_zbapi_te_rkpf ) ) ) ) )
        ( LINES OF VALUE bapiparextab(
          FOR <zz_zbapi_te_resb> IN zz_table_zbapi_te_resb
          ( CONV #( VALUE zz_ty_valueparts(
            structure = 'ZBAPI_TE_RESB'
            parts     = CONV #( CORRESPONDING zbapi_te_resb( <zz_zbapi_te_resb> ) ) ) ) ) ) ) ).
  ENDIF.
ENDIF.

Implement the method EXTENSIONIN_TO_RESB of BAdI BAPI_RESERVATION_CREATE1 with this code:

TYPES: BEGIN OF zz_ty_valueparts,
         structure TYPE te_struc,
         BEGIN OF parts,
           _1 TYPE valuepart,
           _2 TYPE valuepart,
           _3 TYPE valuepart,
           _4 TYPE valuepart,
         END OF parts,
       END OF zz_ty_valueparts.
LOOP AT it_extension_in ASSIGNING FIELD-SYMBOL(<extensionin>).
  CASE <extensionin>-structure.
    WHEN 'ZBAPI_TE_RKPF'.
      DATA(zbapi_te_rkpf) = CONV zbapi_te_rkpf( 
          LET aux_valueparts = CONV zz_ty_valueparts( <extensionin> ) IN aux_valueparts-parts ).
      MOVE-CORRESPONDING zbapi_te_rkpf TO cs_rkpf.
    WHEN 'ZBAPI_TE_RESB'.
      DATA(zbapi_te_resb) = CONV zbapi_te_resb( 
          LET aux_valueparts = CONV zz_ty_valueparts( <extensionin> ) IN aux_valueparts-parts ).
      ASSIGN resb[ rspos = zbapi_te_resb-rspos ] TO FIELD-SYMBOL(<resb>).
      IF sy-subrc = 0.
        MOVE-CORRESPONDING zbapi_te_resb TO <resb>.
      ENDIF.
  ENDCASE.
ENDLOOP.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK