Sending an email when Background Job Cancelled

 Sending an email to group of people when Background Job Cancelled:

*&---------------------------------------------------------------------*
*& Report ZVB_BGJ
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zvb_bgj.
TABLES:tbtco,tbtc5.
TYPES:BEGIN OF ty_tbtco,
        jobname   TYPE tbtco-jobname,
        jobcount  TYPE tbtco-jobcount,
        status    TYPE tbtco-status,
        sdlstrtdt TYPE tbtco-sdlstrtdt,
      END OF ty_tbtco.

DATA:it_tbtco TYPE TABLE OF ty_tbtco,
     iw_tbtco TYPE ty_tbtco.

DATA:it_log TYPE TABLE OF tbtc5,
     iw_log TYPE tbtc5.

DATAmaildata LIKE sodocchgi1,
      mailtxt  LIKE solisti1 OCCURS 10 WITH HEADER LINE,
      mailrec  LIKE somlrec90 OCCURS WITH HEADER LINE.

SELECT-OPTIONSs_job FOR tbtco-jobname,
                s_date FOR tbtco-sdlstrtdt.

CLEAR:maildata,mailtxt,mailrec.
REFRESH:mailtxt,mailrec.

START-OF-SELECTION.
  SELECT jobname
         jobcount
         status
         sdlstrtdt
    FROM tbtco
    INTO TABLE it_tbtco
    WHERE jobname IN s_job AND
          sdlstrtdt IN s_date AND
          status 'A'.

  LOOP AT it_tbtco INTO iw_tbtco.
    CALL FUNCTION 'BP_JOBLOG_READ'
      EXPORTING
        client    sy-mandt
        jobcount  iw_tbtco-jobcount
*       JOBLOG    = ' '
        jobname   iw_tbtco-jobname
*       LINES     =
*       DIRECTION =
      TABLES
        joblogtbl it_log
*   EXCEPTIONS
*       CANT_READ_JOBLOG            = 1
*       JOBCOUNT_MISSING            = 2
*       JOBLOG_DOES_NOT_EXIST       = 3
*       JOBLOG_IS_EMPTY             = 4
*       JOBLOG_NAME_MISSING         = 5
*       JOBNAME_MISSING             = 6
*       JOB_DOES_NOT_EXIST          = 7
*       OTHERS    = 8
      .
    IF sy-subrc <> 0.
* Implement suitable error handling here
    ENDIF.

CLEAR:maildata,mailtxt,mailrec.
REFRESH:mailtxt,mailrec.

    maildata-obj_descr | Failed Background job Detais{ iw_tbtco-jobname } / { iw_tbtco-jobcount } |.

    mailtxt '<!DOCTYPE html>'.
    APPEND mailtxt.
    mailtxt '<html style="background-color:red">'.
    APPEND mailtxt.
    mailtxt '<style>table, th, td {border:1px solid black; border-collapse:collapse}</style>'.
    APPEND mailtxt.
    mailtxt '<body style="background-color:blue">'.
    APPEND mailtxt.
    mailtxt '<p>Dear User,</p>'.
    APPEND mailtxt.
    mailtxt '<p>Please find the below failed job details.</p>'.
    APPEND mailtxt.
    mailtxt-line |<p> Failed Job details { iw_tbtco-jobname } / { iw_tbtco-jobcount }</p> |.
    APPEND mailtxt.
    mailtxt '<table>'." style="width:100%".</p>'.
    APPEND mailtxt.
    mailtxt '<tr>'.
    APPEND mailtxt.
    mailtxt '<th style = "border:1px solid black">Date</th>'.
    APPEND mailtxt.
    mailtxt '<th style = "border:1px solid black">Time</th>'.
    APPEND mailtxt.
    mailtxt '<th style = "border:1px solid black">Message Text</th>'.
    APPEND mailtxt.
    mailtxt '<th style = "border:1px solid black">Message Class</th>'.
    APPEND mailtxt.
    mailtxt '<th style = "border:1px solid black">Message No</th>'.
    APPEND mailtxt.
    mailtxt '<th style = "border:1px solid black">Message Type</th>'.
    APPEND mailtxt.
    mailtxt '</tr>'.
    APPEND mailtxt.

    LOOP AT it_log INTO iw_log.
      mailtxt '<tr>'.
      APPEND mailtxt.
      mailtxt-line | <td style "border:1px solid black">{ iw_log-enterdate }</td> |.
      APPEND mailtxt.
      mailtxt-line | <td style "border:1px solid black">{ iw_log-enterdate }</td> |.
      APPEND mailtxt.
      mailtxt-line | <td style "border:1px solid black">{ iw_log-text }</td>|.
      APPEND mailtxt.
      mailtxt-line | <td style "border:1px solid black">{ iw_log-msgid }</td> |.
      APPEND mailtxt.
      mailtxt-line | <td style "border:1px solid black">{ iw_log-msgno }</td> |.
      APPEND mailtxt.
      mailtxt-line | <td style "border:1px solid black">{ iw_log-msgtype }</td> |.
      APPEND mailtxt.
      mailtxt '</tr>'.
      APPEND mailtxt.
    ENDLOOP.

    mailtxt '</table>'.
    APPEND mailtxt.
    mailtxt '</body>'.
    APPEND mailtxt.
    mailtxt '<p style = "color:red"><b>This is a system generated mail.Please do not reply</b></p>'.
    APPEND mailtxt.
    mailtxt '</html>'.
    APPEND mailtxt.

  mailrec-receiver 'user1@gmail.com'.
  mailrec-rec_type 'U'.
  APPEND mailrec.
  mailrec-receiver 'user2@gmail.com'.
  mailrec-rec_type 'U'.
  APPEND mailrec.

  CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
    EXPORTING
      document_data              maildata
      document_type              'HTM'
*     PUT_IN_OUTBOX              = ' '
*     COMMIT_WORK                = ' '
*     IP_ENCRYPT                 =
*     IP_SIGN                    =
* IMPORTING
*     SENT_TO_ALL                =
*     NEW_OBJECT_ID              =
    TABLES
      object_header              mailtxt
      object_content             mailtxt
*     CONTENTS_HEX               =
*     OBJECT_PARA                =
*     OBJECT_PARB                =
      receivers                  mailrec
    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.
  COMMIT WORK.
  ENDLOOP.
  IF sy-subrc 0.
    WRITE:'Mail sent successfully'.
  ENDIF.

 

Output: 

 

 

Comments

Popular posts from this blog

CDS Views: Table Functions with Example

Passing Dynamic Variables to SO10 TEXT

Adobe Form/PDF Form Introduction