Friday, February 16, 2018

Classical Report Output in Email



In the following example we have created a classical report output and now we shall send it through email. The user will see the output in his mail box. The output format is in HTML format.

Simple Classical Report

REPORT zsr_test NO STANDARD PAGE HEADING.

TYPES: BEGIN OF ty_mara,
         matnr TYPE mara-matnr,
         ersda TYPE mara-ersda,
         ernam TYPE mara-ernam,
         mtart TYPE mara-mtart,
         matkl TYPE mara-matkl,
       END OF ty_mara.
DATA: wa_mara TYPE ty_mara,
      it_mara TYPE TABLE OF ty_mara.

START-OF-SELECTION.
  PERFORM get_mara.
  PERFORM classical_output.

*&---------------------------------------------------------------------*
*&      Form  GET_MARA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM get_mara .

  SELECT matnr ersda ernam mtart matkl
    FROM mara INTO TABLE it_mara
    WHERE mtart = 'FERT'.

  IF sy-subrc = 0.
    SORT it_mara BY ersda DESCENDING.
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  CLASSICAL_OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM classical_output .

  ULINE AT /0(60).
  WRITE: / '|''Material'   COLOR 3, 21 '|',
               22 'Created On' COLOR 3, 34 '|',
               35 'Created By' COLOR 3, 49 '|',
               50 'Group'      COLOR 3, 60 '|'.
  ULINE AT /0(60).

  LOOP AT it_mara INTO wa_mara.
    WRITE: / '|'wa_mara-matnr,            21 '|',
                 22 wa_mara-ersda DD/MM/YYYY, 34 '|',
                 35 wa_mara-ernam,            49 '|',
                 50 wa_mara-matkl,            60 '|'.
  ENDLOOP.
  ULINE AT /0(60).

ENDFORM.

Mail Sending Program

REPORT zsr_test1 NO STANDARD PAGE HEADING.

DATA: document_data  TYPE sodocchgi1,
      wa_content     TYPE solisti1,
      object_content TYPE TABLE OF solisti1,
      wa_rec         TYPE somlrec90,
      receivers      TYPE TABLE OF somlrec90,
      listobject     TYPE TABLE OF abaplist,
      listasci(1024) TYPE OCCURS WITH HEADER LINE,
      wa_html        TYPE w3html,
      html           TYPE TABLE OF w3html,
      v_date         TYPE char10.

INITIALIZATION.
  SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-000.
  SELECT-OPTIONS s_email FOR wa_rec-receiver NO INTERVALS.
  SELECTION-SCREEN END OF BLOCK b1.

START-OF-SELECTION.
  PERFORM report_list_from_memory.
  PERFORM list_to_asci.
  PERFORM list_to_html_format.
  PERFORM mail_subject.
  PERFORM mail_content.
  PERFORM mail_receivers.
  PERFORM send_mail.

*&---------------------------------------------------------------------*
*&      Form  REPORT_LIST_FROM_MEMORY
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM report_list_from_memory .

  SUBMIT zsr_test EXPORTING LIST TO MEMORY AND RETURN.

  CALL FUNCTION 'LIST_FROM_MEMORY'
    TABLES
      listobject = listobject
    EXCEPTIONS
      not_found  = 1
      OTHERS     2.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  LIST_TO_ASCI
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM list_to_asci .

  CALL FUNCTION 'LIST_TO_ASCI'
    TABLES
      listobject         = listobject
      listasci           = listasci
    EXCEPTIONS
      empty_list         = 1
      list_index_invalid = 2
      OTHERS             3.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  LIST_TO_HTML_FORMAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM list_to_html_format .

  CALL FUNCTION 'WWW_HTML_FROM_LISTOBJECT'
    TABLES
      html       = html
      listobject = listobject.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  MAIL_SUBJECT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM mail_subject .

  CLEAR: document_data, wa_content, wa_rec.
  REFRESH: object_content, receivers.

  CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'
    EXPORTING
      date_internal            = sy-datum
    IMPORTING
      date_external            = v_date
    EXCEPTIONS
      date_internal_is_invalid = 1
      OTHERS                   2.

  document_data-obj_name = 'FERT'.
  CONCATENATE 'Finished Goods Materials for' v_date
  INTO document_data-obj_descr SEPARATED BY space.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  MAIL_CONTENT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM mail_content .

  LOOP AT html INTO wa_html.
    wa_content = wa_html.
    APPEND wa_content TO object_content.
    CLEAR: wa_content.
  ENDLOOP.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  MAIL_RECEIVERS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM mail_receivers .

  LOOP AT s_email.
    wa_rec-receiver = s_email-low.
    wa_rec-rec_type = 'U'.
    wa_rec-express  = 'X'..
    APPEND wa_rec TO receivers.
  ENDLOOP.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  SEND_MAIL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM send_mail .

  CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
    EXPORTING
      document_data              = document_data
      document_type              = 'HTM'
      put_in_outbox              = 'X'
    TABLES
      object_content             = object_content
      receivers                  = receivers
    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.
    COMMIT WORK.
  ENDIF.

ENDFORM.


Selection Screen



Mail Output




6 comments:

Unknown said...

it's very help full to share this program.

logistic-solutions said...

Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
sas strategic consulting services

Ganesh said...

ERP Leather nowadays is working well in organizing the workplace and work and carefully all the authoritative information!

APTRON GURGAON said...

informative content
SAP TRAINING IN GURGAON

Anonymous said...

Thankyou for Sharing Great Information. It is Very Helpful Information on sapvideos.

Anonymous said...

It was Nice post and very useful information on sap simple finance videos.