Automating Direct Activity Allocation in SAP CO using BAPI_ACC_ACTIVITY_ALLOC_POST

 ABAP Tutorial on how to automating KB21N using BAPI_ACC_ACTIVITY_ALLOC_POST


Instead of manually posting direct activity allocation using KB21N, which can be time consuming, we can build a custom ABAP program and integrate it with ALV Grid. This way, users can prepare data in ALV, then directly trigger the posting via BAPI_ACC_ACTIVITY_ALLOC_POST with just one click.

Here’s the step-by-step approach:


1. Prepare Your Data Using ALV

Set up the data that you want to post using alv grid.

👉 For now lets just assume that we already have & processed the data, and is now available in an internal table called (lt_data).

2. Create new GUI Status

Before we set up the BAPI, we need to add a custom button to trigger the posting, click on display object list, and you can add/edit GUI Status, and then add a button that you want


3. Set up the PF-STATUS

Use the following code snippet to activate your PF-STATUS inside the program:
FORM pf_status_set USING ut_extab TYPE slis_t_extab.
  SET PF-STATUS 'ZALV_STAT'.
ENDFORM.

4. Fill the BAPI Structures

Now let’s add the logic to trigger BAPI_ACC_ACTIVITY_ALLOC_POST whenever the button is clicked.

FORM user_command USING u_ucomm TYPE sy-ucomm
      us_selfield TYPE slis_selfield.

  DATA: lr_grid TYPE REF TO cl_gui_alv_grid.

  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = lr_grid.

  IF lr_grid IS BOUND.
    CALL METHOD lr_grid->check_changed_data.
  ENDIF.

  CASE u_ucomm.

    WHEN '&EXE'.

      CLEAR: ld_bulan, doc_header.
      REFRESH: doc_items.

      ld_bulan = s_budat-low+4(2).
      IF ld_bulan IS NOT INITIAL.
        PERFORM conv_date USING ld_bulan CHANGING ld_bulanx.
        CONCATENATE 'Confirmation Waste' ld_bulanx s_budat-low+0(4) INTO lv_headertext SEPARATED BY space.
      ENDIF.

      doc_header-co_area = '1100'.
      doc_header-docdate = s_budat-high.
      doc_header-postgdate = s_budat-high.
      doc_header-doc_hdr_tx = lv_headertext.
      doc_header-username = sy-uname.

      LOOP AT lt_data ASSIGNING FIELD-SYMBOL(<fs_data>) WHERE belnr IS INITIAL.
        CLEAR: wa_doc_items, doc_no, lv_matnr.
        REFRESH: return.

        CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT'
          EXPORTING
            input  = <fs_data>-matnr
          IMPORTING
            output = lv_matnr.

        CONCATENATE lv_matnr <fs_data>-maktx INTO DATA(lv_item_text) SEPARATED BY ' - '.

        wa_doc_items-send_cctr = 'FILL YOUR COST CENTER HERE'.
        wa_doc_items-acttype = 'FILL YOUR ACTIVITY TYPE HERE'.
        wa_doc_items-rec_order = <fs_data>-aufnr.
        wa_doc_items-actvty_qty = <fs_data>-zconfirm.
        wa_doc_items-seg_text = lv_item_text.

        APPEND wa_doc_items TO doc_items.

      ENDLOOP.

      IF doc_items IS NOT INITIAL.
        CALL FUNCTION 'BAPI_ACC_ACTIVITY_ALLOC_POST'
          EXPORTING
            doc_header = doc_header
          IMPORTING
            doc_no     = doc_no
          TABLES
            doc_items  = doc_items
            return     = return.

        READ TABLE return ASSIGNING FIELD-SYMBOL(<fs_ret>) WITH KEY type = 'E'.
        IF sy-subrc NE 0.
          CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
            EXPORTING
              wait = 'X'.

          CONCATENATE 'Document' doc_no 'was created' INTO DATA(v_msg) SEPARATED BY space.
          MESSAGE v_msg TYPE 'I'.

        ELSE.
          CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.

          REFRESH : lt_tab.
          CLEAR: ls_tab.
          LOOP AT return ASSIGNING <fs_ret> WHERE type = 'E'.
            ls_tab-lineno = sy-tabix.
            ls_tab-msgid  = 'E4'.
            ls_tab-msgno  = '000'.
            ls_tab-msgty  = 'E'.
            ls_tab-msgv1  = <fs_ret>-message.
            APPEND ls_tab TO lt_tab.
          ENDLOOP.

          CALL FUNCTION 'C14Z_MESSAGES_SHOW_AS_POPUP'
            TABLES
              i_message_tab = lt_tab.

        ENDIF.

      ELSE.
        MESSAGE 'Order has already been generated' TYPE 'E'.
      ENDIF.

      PERFORM get_data.
      CALL METHOD lr_grid->refresh_table_display.
  ENDCASE.

ENDFORM.

5. Save and Activate

Finally save and activate your program.
Now, with one click of the custom button in your ALV Grid, your Direct Activity Allocation will be posted automatically via the BAPI.



Comments

Popular posts from this blog

SAP ABAP Mass Download Attachment

Learn How to Use STVARV for Dynamic ABAP Programs

Why Can't I Find This Text in SE16N? Mastering the READ_TEXT Function Module