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.


No comments :