Register Login

Send mail from background job

Updated May 18, 2018

Hi All,

If my job fails, (ie sy-subrc <> 0) I need to inform the user that there was a problem. I want to send a string to the calling user. It seems to me that the most appropriate thing to do would be to send SAPoffice mail to the user. Is there an easy way to do this? I would expect there to be a function that would export the user name and a text string. Then it would send the user that message. I have seen SO_SEND_OBJECT. It is much more complex than what I wanted. Is there anything else closer to what I have described?


 


Comments

  • 30 Nov 2011 5:25 am rekha
    I always use SO_NEW_DOCUMENT_SEND_API1, but SO_OBJECT_SEND is really not

    any harder to use. Maybe this code sample will help:

    data: error_msg(80),
    subject like sodocchgi1,
    msg_body like solisti1 occurs 0 with header line,
    receivers like somlreci1 occurs 0 with header line.

    * Fill recipient list
    receivers-receiver = sy-uname. " or some other user ID
    append receivers.

    * Fill subject
    subject-obj_descr = 'SAP Program XXX Failure'.

    * Fill message body
    msg_body-line = 'Program XXX has failed with the following error:'.
    append msg_body.

    clear msg_body. append msg_body.

    msg_body-line = error_msg. " assuming error_msg has been filled

    append msg_body.

    * Send message
    call function 'SO_NEW_DOCUMENT_SEND_API1'
    exporting
    document_data = subject
    put_in_outbox = 'X'
    tables
    object_content = msg_body
    receivers = receivers.


    If you want to send the message to an external user, you can do that as
    well. There's good on-line documentation for SO_NEW_DOCUMENT_SEND_API1 in
    German (in case that helps). Good luck.
  • 30 Nov 2011 5:27 am rekha
    Hi,

    It is not as difficult as it sounds. Check this program. Just copy
    and execute and you will understand how it works.

    You can send any message at the end of a program (Success or Failure).

    There is one more advantage to this routine. Look at the lines that I have
    specified as 'Special-Code'. When the user open this message in the inbox,
    there is an execute button. This code currently takes them to SP01
    transaction and the parameter ID is set as sy-spono. With this code, when
    you run this job in the background, the user can directly go to the spool
    using this execute button. They do not have to search in the spool. You can
    set this to any transaction and it will take them there. Just set the
    parameter IDs and values.

    If the send to userid is a parameter (from the screen), one user can run
    this and send the mail to another user. (ideal for background/scheduled
    jobs). This also sends mail to internet. In the itab_receivers, set the
    mail type as 'U' and specify the email address with domain name. Ex:

    userid@mail.company.com. Your basis team should also do some configuration
    for this to work. Send me an email if you have any questions.

    REPORT YVENKATESH_SEND_EXPRESS_MAIL .

    *---------------------------------------------------------------------*
    * Internal Tables *
    *---------------------------------------------------------------------*
    * For Sending the express mail
    DATA: ITAB_OBJHEAD LIKE SOLI OCCURS 0 WITH HEADER LINE,
    ITAB_OBJCONT LIKE SOLI OCCURS 0 WITH HEADER LINE,
    ITAB_OBJPARA LIKE SELC OCCURS 0 WITH HEADER LINE,
    ITAB_OBJPARB LIKE SOOP1 OCCURS 0 WITH HEADER LINE,
    ITAB_RECEIVERS LIKE SOOS1 OCCURS 0 WITH HEADER LINE.

    *---------------------------------------------------------------------*
    * Work Areas *

    *---------------------------------------------------------------------*
    * For Sending the express mail
    DATA: W_OBJECT_HD_CHANGE LIKE SOOD1.

    * counters
    DATA: W_OK_COUNT(10) TYPE N.
    DATA: W_ERR_COUNT(10) TYPE N.
    DATA: W_TOT_COUNT(10) TYPE N.

    *---------------------------------------------------------------------*
    * start-of-selection *
    *---------------------------------------------------------------------*
    START-OF-SELECTION.

    W_OK_COUNT = 200.
    W_ERR_COUNT = 300.
    W_TOT_COUNT = 500.

    *---------------------------------------------------------------------*
    * End-of-selection *
    *---------------------------------------------------------------------*
    END-OF-SELECTION.

    * Initialize the fields
    CLEAR: W_OBJECT_HD_CHANGE.
    CLEAR: ITAB_OBJCONT, ITAB_OBJHEAD, ITAB_OBJPARA,
    ITAB_OBJPARB, ITAB_RECEIVERS.

    REFRESH: ITAB_OBJCONT, ITAB_OBJHEAD, ITAB_OBJPARA,
    ITAB_OBJPARB, ITAB_RECEIVERS.

    * Fill the message text
    CONCATENATE 'Number of records processed:' W_TOT_COUNT
    INTO ITAB_OBJCONT-LINE SEPARATED BY SPACE.
    APPEND ITAB_OBJCONT.

    CONCATENATE 'Number of records processed Correctly:' W_OK_COUNT
    INTO ITAB_OBJCONT-LINE SEPARATED BY SPACE.
    APPEND ITAB_OBJCONT.

    CONCATENATE 'Number of records Errored Out:' W_ERR_COUNT
    INTO ITAB_OBJCONT-LINE SEPARATED BY SPACE.
    APPEND ITAB_OBJCONT.

    * Fill w_object_hd_change structure
    W_OBJECT_HD_CHANGE-OBJLA = SY-LANGU.
    W_OBJECT_HD_CHANGE-OBJNAM = SY-REPID.
    W_OBJECT_HD_CHANGE-OBJDES = 'Job ABC results'.
    W_OBJECT_HD_CHANGE-OBJSNS = 'F'.
    W_OBJECT_HD_CHANGE-VMTYP = 'T'.
    W_OBJECT_HD_CHANGE-SKIPS = 'X'.
    W_OBJECT_HD_CHANGE-ACNAM = 'SP01'. "Transaction to execute Special-Code

    W_OBJECT_HD_CHANGE-OBJCP = 'X'.

    * Specify the receivers
    ITAB_RECEIVERS-RCDAT = SY-DATUM.
    ITAB_RECEIVERS-RCTIM = SY-UZEIT.
    ITAB_RECEIVERS-RECNAM = SY-UNAME. "Send to UserId"
    ITAB_RECEIVERS-RTUNAM = SY-UNAME.
    ITAB_RECEIVERS-SNDEX = 'X'. "Express mail message.
    APPEND ITAB_RECEIVERS.

    * Specify the spool info. "Special-Code
    ITAB_OBJPARA-NAME = 'SPI'. "Special-Code
    ITAB_OBJPARA-LOW = SY-SPONO. "Special-Code
    APPEND ITAB_OBJPARA. "Special-Code

    CALL FUNCTION 'SO_OBJECT_SEND'
    EXPORTING
    OBJECT_HD_CHANGE = W_OBJECT_HD_CHANGE
    OBJECT_TYPE = 'RAW'
    OWNER = SY-UNAME
    TABLES
    OBJCONT = ITAB_OBJCONT

    OBJHEAD = ITAB_OBJHEAD
    OBJPARA = ITAB_OBJPARA
    OBJPARB = ITAB_OBJPARB
    RECEIVERS = ITAB_RECEIVERS
    EXCEPTIONS
    OTHERS = 01.

×