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