Aug 16, 2024

How to Change Cost Center in Accounting Document

Changing the KOSTL (Cost Center) field in the BSEG table using a BAPI is not directly possible since BSEG is a line-item table and is managed by SAP standard transactions. The BSEG table is updated indirectly through various financial postings and cannot be directly modified using a standard BAPI.

Alternative Approach:

If you need to change the cost center in financial documents, the correct approach would involve reversing the original document and posting a new document with the correct cost center. However, if your goal is to correct or change the cost center without reversing and reposting the document, consider using the following methods:

1. Use BAPI for Document Reversal and Reposting

  • BAPI_ACC_DOCUMENT_REV_POST: This BAPI is used to reverse the original accounting document.
  • BAPI_ACC_DOCUMENT_POST: This BAPI allows you to repost the document with the correct cost center.

2. Use a Direct Update via a Custom ABAP Program (Not Recommended)

  • Directly updating the BSEG-KOSTL field is not advisable since it can lead to inconsistencies in the database and violates SAP’s data integrity principles.

3. Use Special Purpose BAPIs

  • In specific cases, you may use specialized BAPIs related to the type of posting, such as:
    • BAPI_ACC_GL_POSTING_REV_POST: If dealing with General Ledger documents.
    • BAPI_ACC_INVOICE_RECEIPT_POST: For invoices and vendor-related postings.

Conclusion

Direct modification of BSEG-KOSTL via a standard BAPI is not supported. The best approach involves reversing the original document and posting a corrected version using standard BAPIs, ensuring that data integrity is maintained within the SAP system.

Aug 14, 2024

How to Find an Enhancement from a Function Module?

 To find the enhancement name associated with a specific function module in SAP, you can follow these steps:

1. Using Transaction Code SMOD

  • Step 1: Go to transaction code SMOD.
  • Step 2: In the initial screen of SMOD, click on the "Find" button (or press Ctrl + F).
  • Step 3: In the search screen, enter the function module name (e.g., EXIT_SAPF110S_001) in the "Function Module" field.
  • Step 4: Execute the search.
  • Result: The system will display the enhancement(s) that include this function module.

2. Using Transaction Code SE80 (Object Navigator)

  • Step 1: Go to transaction code SE80.
  • Step 2: In the Object Navigator, select "Repository Browser."
  • Step 3: Enter the function module name (e.g., EXIT_SAPF110S_001) in the field and press Enter.
  • Step 4: Once the function module is displayed, double-click to open it.
  • Step 5: Scroll down to the "Attributes" tab where the "Enhancement" field is displayed.
  • Result: The enhancement name related to the function module will be shown in this field.

3. Using Transaction Code SE37

  • Step 1: Go to transaction code SE37 to open the Function Builder.
  • Step 2: Enter the function module name (e.g., EXIT_SAPF110S_001) and click "Display."
  • Step 3: Navigate to the "Attributes" tab to view the enhancement name under the "Package" information.
  • Result: The enhancement name will be linked to the function module in the "Enhancement" field.

4. Using Transaction Code SE93 (Transaction Code Search)

  • Step 1: Go to transaction code SE93.
  • Step 2: Search for the transaction associated with the function module.
  • Step 3: Once you find the transaction, use the transaction code to determine the enhancement related to the transaction and function module.

5. Using ABAP Code (Advanced)

  • Step 1: You can write a custom ABAP report to search for the enhancement name linked to a function module by querying the TADIR table (which contains development objects) and MODSAP (which contains modifications).
  • Example Code:
    SELECT SINGLE * FROM MODSAP WHERE FUNCNAME = 'EXIT_SAPF110S_001'.
  • Step 2: Run the report to get the enhancement name.

Conclusion

The enhancement name related to a function module can be found through SAP's standard transactions like SMOD, SE80, SE37, or by writing a small ABAP program. Each method provides a way to trace the connection between function modules and their corresponding enhancements, allowing you to extend or modify standard SAP functionality as needed.

Aug 13, 2024

BTE, BADI or Eser-exit for Transaction F110

When working with transaction F110 (Automatic Payment Program) in SAP, you have several options to enhance or customize its functionality, including BTEs, BADIs, and User Exits. Each has its use case depending on the specific requirement. Here's an overview of when and how to use each:

1. BTE (Business Transaction Events)

  • Use Case: BTEs are suitable for situations where you need to extend SAP standard processes without modifying the core code. For F110, BTEs can be used to enhance the payment process by adding custom validations or processing steps.
  • Relevant BTEs:
    • BTE 1810: Can be used for adding additional validations during the payment proposal run.
    • BTE 00002040: Useful for custom modifications during the payment run.
  • Configuration: Use transaction FIBF to search, implement, and register BTEs.

2. BADI (Business Add-In)

  • Use Case: BADIs are more flexible and powerful than BTEs, offering object-oriented enhancements. They are ideal when you need to add custom business logic or integrate additional processing steps during the payment run.
  • Relevant BADIs:
    • BADI_F110_SCHEDULE_JOB: Allows for enhancements when scheduling the payment job.
    • BADI_F110_PRINT: Used to enhance the print program for the payment advice or other documents.
  • Configuration: Use transaction SE18 to explore and implement BADIs. You can define and implement multiple instances of a BADI.

3. User Exits

  • Use Case: User Exits are more traditional and can be used when you need to make specific adjustments at predefined points in the program. They are often used when dealing with older versions of SAP.
  • Relevant User Exits:
    • EXIT_SAPF110S_001: This exit allows you to include additional data or processing before the payment run starts.
    • EXIT_SAPF110V_001: It can be used to influence the selection of items for payment.
  • Configuration: User Exits require code to be added to include programs. You can explore these using transactions CMOD and SMOD.

Which One to Use?

  • For Simple Enhancements: Use BTEs if your enhancement is straightforward and falls within the scope of what a BTE can handle.
  • For Complex Enhancements: If you need more complex processing or object-oriented enhancements, BADIs are generally more powerful and flexible.
  • For Specific Adjustments: If you need to adjust specific points within the F110 process that are covered by User Exits, they can be a suitable option, especially in older SAP environments.

Conclusion

  • BTE: Best for simple, event-driven customizations.
  • BADI: Ideal for complex, object-oriented enhancements.
  • User Exit: Suitable for targeted modifications within predefined areas of the program.

Choose based on your specific needs, the complexity of the enhancement, and the SAP environment you're working in.

Jun 28, 2024

How to send messages to other users in the SAP system using the SO_NEW_DOCUMENT_SEND_API1 function module?

Requirements:

How to send messages to other users in the SAP system using the SO_NEW_DOCUMENT_SEND_API1 function module?
 

Program Codes:

REPORT zsend_chat.

DATAlv_sender     TYPE sy-uname,
      lv_recipient  TYPE sy-uname,
      lw_docdata    TYPE sodocchgi1,
      lv_subject    TYPE so_obj_des,
      lv_message    TYPE solisti1,
      lt_message    TYPE TABLE OF solisti1,
      lt_recipients TYPE TABLE OF somlreci1,
      ls_recipients TYPE somlreci1,
      lv_obj_id     TYPE sofolenti1,
      lv_commit     TYPE LENGTH 1.

PARAMETERSp_recp TYPE sy-uname   OBLIGATORY DEFAULT 'DC001',
            p_subj TYPE so_obj_des OBLIGATORY DEFAULT 'Msg Title',
            p_mesg TYPE string     OBLIGATORY DEFAULT 'Test message to user DC001'.

START-OF-SELECTION.
  " Initialize sender and recipient
  lv_sender            sy-uname.
  lv_recipient         p_recp.
  lv_subject           p_subj.
  lv_message-line      p_mesg.
  lw_docdata-OBJ_NAME  =
  lw_docdata-OBJ_DESCR p_subj.

  " Prepare message content
  APPEND lv_message TO lt_message.

  " Prepare recipient
  ls_recipients-receiver lv_recipient.
  ls_recipients-rec_type 'B'" 'B' for user in the system
  APPEND ls_recipients TO lt_recipients.

  " Send message
  CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
    EXPORTING
      document_data              lw_docdata
      put_in_outbox              'X'
      commit_work                'X'
    TABLES
      object_content             lt_message
      receivers                  lt_recipients
    EXCEPTIONS
      too_many_receivers         1
      document_not_sent          2
      document_type_not_exist    3
      operation_no_authorization 4
      parameter_error            5
      x_error                    6
      enqueue_error              7
      OTHERS                     8.

  IF sy-subrc 0.
    WRITE'Message sent successfully to'lv_recipient.
  ELSE.
    WRITE'Failed to send message. Error code:'sy-subrc.
  ENDIF.


Code Explanation

  1. Parameter Declarations:

    • p_recipient: The username of the recipient.
    • p_subject: The subject of the message.
    • p_message: The message content.
  2. Data Declarations:

    • lv_sender: The sender's username (current user).
    • lv_recipient: The recipient's username.
    • lv_subject: The subject of the message.
    • lv_message: The message line.
    • lt_message: Table to hold the message content.
    • lt_recipients: Table to hold the recipients.
    • ls_recipients: Structure to hold recipient information.
  3. Message Preparation:

    • The message content is appended to lt_message.
    • The recipient information is appended to lt_recipients.
  4. Send Message:

    • The SO_NEW_DOCUMENT_SEND_API1 function module is called to send the message.
    • put_in_outbox ensures the message is put in the outbox.
    • commit_work ensures the changes are committed.
  5. Result Handling:

    • The program checks the return code sy-subrc to determine if the message was sent successfully or if there was an error.

This sample program sends a simple message to another user in the SAP system. Adjust the parameters and content as needed for your specific use case.


Jun 22, 2024

How to Generate a Dynamic Internal Table and Display it in ALV (Program Sample)

Generate a Dynamic Internal Table and Display it in ALV (Program Sample)

Requirements:

Create an ALV list to display the number of allocated seats for airlines daily. 
The daily columns should be dynamic according to the number of days in a month.

ABAP Codes:

*&---------------------------------------------------------------------*
*& Report ZDI_DINTAB2
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zdi_dintab2.

*&---------------------------------------------------------------------*
*& Data Declaration
*&---------------------------------------------------------------------*
TABLESsflights002.

DATAt_sflight TYPE TABLE OF sflight.
DATAv_last_day(2).

* Dynamic Table Declarations
DATA t_table TYPE REF TO data,
       w_line  TYPE REF TO data,
       w_line1 TYPE REF TO data,
       w_fcat  TYPE lvc_s_fcat,
       t_fcat  TYPE lvc_t_fcat.

* Field Symbols Declarations
FIELD-SYMBOLS<dyn_table> TYPE STANDARD TABLE,
               <dyn_line>  TYPE any,
               <dyn_line1> TYPE any,
               <fs1>       TYPE any.


*&---------------------------------------------------------------------*
*& Seletion Screens
*&---------------------------------------------------------------------*
PARAMETERS:    p_carrid TYPE sflight-carrid DEFAULT 'AZ'.
PARAMETERS:    p_spmon  TYPE s002-spmon DEFAULT '202403'"sy-datum(6).
SELECT-OPTIONS s_connid FOR  sflight-connid.
SELECT-OPTIONS s_fldate FOR  sflight-fldate NO-DISPLAY.

*&---------------------------------------------------------------------*
*& PAI
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN.
  PERFORM pai.

*&---------------------------------------------------------------------*
*& Start of Selection
*&---------------------------------------------------------------------*
START-OF-SELECTION.
  PERFORM get_data.
  PERFORM create_dyn_tab.

*&---------------------------------------------------------------------*
*& End of Selection
*&---------------------------------------------------------------------*
END-OF-SELECTION.
  PERFORM dispaly_alv.


*&---------------------------------------------------------------------*
*& Form pai
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM pai .

  DATA lv_pos    TYPE i,
         lv_day(2TYPE n,
         lv_fname  TYPE string.

  IF sy-ucomm 'ONLI'.

    APPEND INITIAL LINE TO s_fldate ASSIGNING FIELD-SYMBOL(<ls_fldate>).
    <ls_fldate>-sign   'I'.
    <ls_fldate>-option 'BT'.
    <ls_fldate>-low    p_spmon && '01'.

    CALL FUNCTION 'RE_LAST_DAY_OF_MONTH'
      EXPORTING
        i_datum <ls_fldate>-low
      IMPORTING
        e_tt    v_last_day.

    <ls_fldate>-high   p_spmon && v_last_day.

    "Build the columns: CARRID, CONNID, DAY01, DAY02, DAY03, etc
    CLEAR w_fcat.
    ADD TO lv_pos.
    w_fcat-fieldname 'CARRID'.
    w_fcat-outputlen 7.
    w_fcat-tabname   'IT_SEATS'.
    w_fcat-coltext   'Airline'(001).
    w_fcat-col_pos   lv_pos.
    w_fcat-key       'X'.
    w_fcat-key_sel   'X'.
    APPEND w_fcat TO t_fcat.

    CLEAR w_fcat.
    ADD TO lv_pos.
    w_fcat-fieldname 'CONNID'.
    w_fcat-outputlen 4.
    w_fcat-tabname   'IT_SEATS'.
    w_fcat-coltext   'Connect'(002).
    w_fcat-col_pos   lv_pos.
    w_fcat-key       'X'.
    w_fcat-key_sel   'X'.
    APPEND w_fcat TO t_fcat.

    DO v_last_day TIMES.
      CLEAR w_fcat.
      ADD TOlv_poslv_day.
      lv_fname |DAY| && lv_day.
      READ TABLE t_fcat INTO w_fcat WITH KEY fieldname lv_fname.
      IF sy-subrc NE 0.
        w_fcat-fieldname lv_fname.
        w_fcat-tabname   'IT_SEATS'.
        w_fcat-coltext   lv_day.
        w_fcat-col_pos   lv_pos.
        w_fcat-outputlen 8.
        APPEND w_fcat TO t_fcat.
      ENDIF.
    ENDDO.

  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form get_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM get_data .

  DATAlw_new TYPE sflight.

  SELECT FROM sflight
    INTO TABLE t_sflight
    WHERE carrid EQ p_carrid
      AND connid IN s_connid
      AND fldate IN s_fldate.
  IF sy-subrc IS NOT INITIAL.
    MESSAGE e398(00WITH 'Data not found'(e01'' '' ''.

  ELSE.
    "Add more data
    READ TABLE t_sflight INTO DATA(lw_sflINDEX 1.
    lw_new lw_sfl.

    lw_new-connid '0017'.
    lw_new-fldate p_spmon && |01|lw_new-seatsocc 180.
    APPEND lw_new TO t_sflight.
    lw_new-fldate p_spmon && |03|lw_new-seatsocc 355.
    APPEND lw_new TO t_sflight.
    lw_new-fldate p_spmon && |08|lw_new-seatsocc 222.
    APPEND lw_new TO t_sflight.
    lw_new-fldate p_spmon && |12|lw_new-seatsocc 213.
    APPEND lw_new TO t_sflight.

    lw_new-connid '0064'.
    lw_new-fldate p_spmon && |02|lw_new-seatsocc 301.
    APPEND lw_new TO t_sflight.
    lw_new-fldate p_spmon && |08|lw_new-seatsocc 335.
    APPEND lw_new TO t_sflight.
    lw_new-fldate p_spmon && |19|lw_new-seatsocc 89.
    APPEND lw_new TO t_sflight.

    lw_new-connid '0055'.
    lw_new-fldate p_spmon && |20|lw_new-seatsocc 189.
    APPEND lw_new TO t_sflight.

    lw_new-connid '0108'.
    lw_new-fldate p_spmon && |04|lw_new-seatsocc 370.
    APPEND lw_new TO t_sflight.
    lw_new-fldate p_spmon && |31|lw_new-seatsocc 323.
    APPEND lw_new TO t_sflight.

    lw_new-connid '0300'.
    lw_new-fldate p_spmon && |15|lw_new-seatsocc 300.
    APPEND lw_new TO t_sflight.
    lw_new-fldate p_spmon && |16|lw_new-seatsocc 299.
    APPEND lw_new TO t_sflight.

    SORT t_sflight.
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form create_dyn_tab
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM create_dyn_tab .

  DATA lv_fldate TYPE sy-datum.

  "Create a dynamic internal table with this structure.
  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      i_style_table             'X'
      it_fieldcatalog           t_fcat
    IMPORTING
      ep_table                  t_table
    EXCEPTIONS
      generate_subpool_dir_full 1
      OTHERS                    2.

  IF sy-subrc EQ 0.

    "Assign the new table to field symbol
    ASSIGN t_table->TO <dyn_table>.
    "Create dynamic work area for the dynamic table
    CREATE DATA w_line  LIKE LINE OF <dyn_table>ASSIGN w_line->*  TO <dyn_line>.
    CREATE DATA w_line1 LIKE LINE OF <dyn_table>ASSIGN w_line1->TO <dyn_line1>.

    "Populate the dynamic table
    LOOP AT t_sflight INTO DATA(lw_sflight).
      "Avoid duplicate entries for key field PART.
      READ TABLE <dyn_table> INTO <dyn_line1>
        WITH KEY ('CARRID'lw_sflight-carrid
                 ('CONNID'lw_sflight-connid.
      IF sy-subrc 0.
        CONTINUE.
      ENDIF.

      "Airline Code
      ASSIGN COMPONENT 'CARRID' OF STRUCTURE <dyn_line> TO <fs1>.
      CHECK <fs1> IS ASSIGNED.
      <fs1> lw_sflight-carrid.
      UNASSIGN <fs1>.

      "Flight Connection
      ASSIGN COMPONENT 'CONNID' OF STRUCTURE <dyn_line> TO <fs1>.
      CHECK <fs1> IS ASSIGNED.
      <fs1> lw_sflight-connid.
      UNASSIGN <fs1>.

      LOOP AT t_fcat INTO w_fcat.
        IF w_fcat-fieldname 'CARRID' OR
           w_fcat-fieldname 'CONNID'.
          CONTINUE.
        ENDIF.

        TRY.
            lv_fldate p_spmon && w_fcat-coltext.
            DATA(lw_sflight1t_sflight[ carrid lw_sflight-carrid
                                           connid lw_sflight-connid
                                           fldate lv_fldate ].
            ASSIGN COMPONENT w_fcat-fieldname OF STRUCTURE <dyn_line> TO <fs1>.
            IF <fs1> IS ASSIGNED.
              <fs1> lw_sflight1-seatsocc.
              UNASSIGN <fs1>.
            ENDIF.
          CATCH cx_sy_itab_line_not_found.
            "next data
        ENDTRY.

        CLEAR lw_sflight1.
      ENDLOOP.

      APPEND <dyn_line> TO <dyn_table>.
      CLEAR<dyn_line>.
      CLEARlw_sflightlw_sflight1.
    ENDLOOP.

  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form dispaly_alv
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM dispaly_alv .

  DATA lw_alv_fieldcat TYPE slis_fieldcat_alv,
         lt_alv_fieldcat TYPE slis_t_fieldcat_alv,
         lw_layout       TYPE slis_layout_alv,
         lv_pos          TYPE i.

  lw_layout-colwidth_optimize =
  lw_layout-zebra             abap_true.

  LOOP AT t_fcat INTO w_fcat.
    lv_pos lv_pos + 1.
    lw_alv_fieldcat-fieldname     w_fcat-fieldname.
    lw_alv_fieldcat-tabname       w_fcat-tabname.
    lw_alv_fieldcat-seltext_l     w_fcat-coltext.
    lw_alv_fieldcat-outputlen     w_fcat-outputlen.
    lw_alv_fieldcat-col_pos       lv_pos.
    lw_alv_fieldcat-do_sum        w_fcat-do_sum.
    lw_alv_fieldcat-emphasize     w_fcat-emphasize.
    lw_alv_fieldcat-key           w_fcat-key.
    lw_alv_fieldcat-no_out        w_fcat-no_out.
    APPEND lw_alv_fieldcat TO lt_alv_fieldcat.
  ENDLOOP.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program sy-repid
      it_fieldcat        lt_alv_fieldcat
      is_layout          lw_layout
      i_default          'X'
      i_save             'A'
    TABLES
      t_outtab           <dyn_table>.

ENDFORM.


Testing Result:

Period 03.2024 (March) 
Expectation: Daily Columns = 31 


Period 02.2024 (February) 
Expectation: Daily Columns = 29