Skip to Content

In this blog I would like to talk about some common subroutines which can be used to display data from internal tables as a document.These subroutines provide the functionalities to create the document object,add tables to the document object,display the document and finally send the document as an email to specified recipients.

INCLUDE ZDOCUMENT_ROUTINES

&—-


*&  Include           ZDOCUMENT_ROUTINES

&—-


TYPE-POOLS : sdydo.

&—-


*&      Form  zf_create_document

&—-


FORM zf_create_document USING pv_ref_dd TYPE REF TO cl_dd_document.

  CREATE OBJECT pv_ref_dd

    EXPORTING

      style  = ‘ALV_GRID’.

  CALL METHOD pv_ref_dd->initialize_document.

ENDFORM.                    “zf_create_document

&—-


*&      Form  zf_document_display

&—-


FORM zf_add_table

             TABLES pt_fieldcat TYPE lvc_t_fcat

             USING pv_outtab           TYPE REF TO data

                   pv_ref_dd           TYPE REF TO cl_dd_document

                   pv_title            TYPE sdydo_text_element.

  DATA : lwa_fieldcatalog TYPE lvc_s_fcat,

         lv_column_count   TYPE i,

         lv_heading        TYPE sdydo_text_element,

         lv_text           TYPE sdydo_text_element,

         lv_ref_dd_table   TYPE REF TO cl_dd_table_element,

         lv_ref_column     TYPE REF TO cl_dd_area,

         lv_form           TYPE REF TO cl_dd_form_area.

  FIELD-SYMBOLS: add_text

        EXPORTING

          text = lv_text.

    ENDDO.

  ENDLOOP.

ENDFORM.                  “zf_document_display

&—-


*&      Form  zf_display_document

&—-


FORM zf_display_document USING pv_ref_dd    TYPE REF TO cl_dd_document.

  DATA : lv_html  TYPE REF TO cl_gui_html_viewer.

  IF lv_html IS INITIAL.

    CREATE OBJECT lv_html

         EXPORTING

              parent    = cl_gui_custom_container=>screen0.

*ref_container.

    CALL METHOD pv_ref_dd->merge_document.

    pv_ref_dd->html_control = lv_html.

  • Display document

    CALL METHOD pv_ref_dd->display_document

      EXPORTING

        reuse_control      = ‘X’

        parent             = cl_gui_custom_container=>screen0

*ref_container

      EXCEPTIONS

        html_display_error = 1.

  ENDIF.

ENDFORM.                    “zf_display_document

&—-


*&      Form  zf_send_document

&—-


FORM zf_send_document TABLES pt_receivers STRUCTURE  somlreci1

                      USING    pv_ref_dd    TYPE REF TO cl_dd_document

                      CHANGING ps_doc_data  TYPE sodocchgi1

                               pv_subrc     TYPE sysubrc

                               pv_all       TYPE sonv-flag

                           pv_new_object_id TYPE sofolenti1-object_id.

  DATA :

    lt_objpack   TYPE STANDARD TABLE OF sopcklsti1 ,

    lwa_objpack  TYPE sopcklsti1,

    lt_objhead   TYPE STANDARD TABLE OF solisti1,

    lwa_objhead  TYPE solisti1,

    lt_objbin    TYPE STANDARD TABLE OF solisti1 ,

    lwa_objbin   TYPE solisti1,

    lt_objtxt    TYPE STANDARD TABLE OF solisti1,

    lwa_objtxt   TYPE solisti1,

    lv_tab_lines TYPE  sytabix,

    lv_all       TYPE sonv-flag,

    lwa_html     TYPE w3html.

*Fill the object header table

  lwa_objhead-line = ps_doc_data-obj_descr.

  APPEND lwa_objhead TO lt_objhead.

*Fill the contents to be displayed in the mail

  LOOP AT pv_ref_dd->html_table INTO lwa_html.

    lwa_objtxt = lwa_html.

    APPEND lwa_objtxt TO lt_objtxt.

  ENDLOOP.

  lv_tab_lines = LINES( lt_objtxt ).

  READ TABLE lt_objtxt INTO lwa_objtxt INDEX lv_tab_lines.

  ps_doc_data-doc_size =

  ( lv_tab_lines – 1 ) * 255 + STRLEN( lwa_objtxt ).

*Fill the objpack table

  CLEAR lwa_objpack-transf_bin.

  lwa_objpack-head_start = 1.

  lwa_objpack-head_num   = 0.

  lwa_objpack-body_start = 1.

  lwa_objpack-body_num   = lv_tab_lines.

  lwa_objpack-doc_type   = ‘HTM’.

  APPEND lwa_objpack TO lt_objpack.

*Call the FM to send the mail

  CALL FUNCTION ‘SO_NEW_DOCUMENT_ATT_SEND_API1’

    EXPORTING

      document_data              = ps_doc_data

      put_in_outbox              = ‘X’

    IMPORTING

      sent_to_all                = pv_all

      new_object_id              = pv_new_object_id

    TABLES

      packing_list               = lt_objpack

      object_header              = lt_objhead

      contents_bin               = lt_objbin

      contents_txt               = lt_objtxt

      receivers                  = pt_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.

  pv_subrc = sy-subrc.”Return code after execution of function

ENDFORM.                    “zf_send_document

&—-


*&      Form  zf_express_flag_set

&—-


FORM zf_express_flag_set TABLES pt_receivers STRUCTURE  somlreci1

                         USING  pv_doc  TYPE sodocchgi1-obj_descr.

  DATA :

  lwa_text       TYPE sotxtinfo,

  lt_rec         TYPE STANDARD TABLE OF soos7,

  lwa_rec        TYPE soos7,

  lwa_receivers  TYPE somlreci1,

  lv_popup_title TYPE pop_title.

*Fill the text to be sent

  lwa_text-msgid = ‘OK’.

  lwa_text-msgno = ‘000’.

  lwa_text-msgv1 = ‘Document ‘.

  lwa_text-msgv2 = pv_doc.

  lwa_text-msgv3 = ‘sent to all recipients’.

*Fill the receivers table

  LOOP AT pt_receivers INTO lwa_receivers.

    lwa_rec-recnam = lwa_receivers-receiver.

    lwa_rec-recesc = lwa_receivers-rec_type.

    APPEND lwa_rec TO lt_rec.

  ENDLOOP.

*Send the express message

  lv_popup_title = pv_doc.

  CALL FUNCTION ‘SO_EXPRESS_FLAG_SET’

    EXPORTING

      text_info         = lwa_text

      procdirect        = ‘X’

      inbox             = ‘X’

      popup_title       = lv_popup_title

      CALL_AT_ONCE      = ‘X’

    TABLES

      rec_tab           = lt_rec

    EXCEPTIONS

      no_receiver_exist = 1

      office_name_error = 2

      OTHERS            = 3.

ENDFORM.                    “zf_express_flag_set

Test Program : ZDOCUMENT_ROUTINES_TEST

REPORT  zdocument_routines_test.

TABLES : somlreci1.

INCLUDE zdocument_routines.

SELECT-OPTIONS : so_rec FOR somlreci1-receiver NO INTERVALS.

DATA :

gt_sflight    TYPE TABLE OF sflight,

gt_spfli      TYPE TABLE OF spfli,

gt_fcat       TYPE lvc_t_fcat,

gt_fcat1      TYPE lvc_t_fcat,

fcode         TYPE syucomm,

ok_code       TYPE syucomm,

ref_dd        TYPE REF TO cl_dd_document,

ref_form      TYPE REF TO cl_dd_form_area,

gt_outtab     TYPE REF TO data.

START-OF-SELECTION.

  PERFORM zf_set_fieldcatalog.

  CALL SCREEN 100.

&—-


*&      Module  STATUS_0100  OUTPUT

&—-


  •       text

—-


MODULE status_0100 OUTPUT.

  DATA :

  lv_title         TYPE sdydo_text_element,

  lwa_receivers    TYPE somlreci1,

  lt_receivers     TYPE STANDARD TABLE OF somlreci1,

  lwa_doc_data     TYPE sodocchgi1,

  lv_subrc         TYPE sysubrc,

  lv_all           TYPE sonv-flag,

  lv_new_object_id TYPE sofolenti1-object_id.

  lv_title = ‘Flight Information’.

  SET PF-STATUS ‘BASIC’.

  SET TITLEBAR ‘TEST’.

*Create the document object

  PERFORM zf_create_document USING ref_dd.

*Add document header

  PERFORM zf_add_header.

*Add the tables

*Get the internal table into data area

  GET REFERENCE OF gt_sflight INTO gt_outtab.

  PERFORM zf_add_table TABLES  gt_fcat

                       USING   gt_outtab

                               ref_dd

                               lv_title.

  CLEAR: gt_outtab,lv_title.

  GET REFERENCE OF gt_spfli INTO gt_outtab.

  lv_title = ‘Flight Schedule’.

*Get the internal table into data area

  PERFORM zf_add_table TABLES  gt_fcat1

                       USING   gt_outtab

                               ref_dd

                               lv_title.

*Display the document

  PERFORM zf_display_document USING ref_dd.

*Fill the receivers

  LOOP AT so_rec.

    lwa_receivers-receiver = so_rec-low.

    lwa_receivers-rec_type = ‘U’.

    APPEND lwa_receivers TO lt_receivers.

  ENDLOOP.

*Fill the document information

  lwa_doc_data-obj_name = ‘Flight Info’.

  lwa_doc_data-obj_descr = ‘Flight Information on the fly’.

*Send the document as a mail

  PERFORM zf_send_document TABLES lt_receivers

                           USING  ref_dd

                        CHANGING  lwa_doc_data

                                  lv_subrc

                                  lv_all

                                  lv_new_object_id.

  IF lv_subrc EQ 0.

    PERFORM zf_express_flag_set TABLES lt_receivers

                                USING lwa_doc_data-obj_descr.

  ENDIF.

ENDMODULE.                 ” STATUS_0100  OUTPUT

&—-


*&      Module  EXIT_PROCESSING  INPUT

&—-


  •       text

—-


MODULE exit_processing INPUT.

  fcode = ok_code.

  CLEAR ok_code.

  CASE fcode.

    WHEN ‘BACK’ OR ‘EXIT’ OR ‘CANC’.

      SET SCREEN 0.

      LEAVE SCREEN.

  ENDCASE.

ENDMODULE.                 ” EXIT_PROCESSING  INPUT

&—-


*&      Form  zf_set_fieldcatalog

&—-


FORM zf_set_fieldcatalog .

  CALL FUNCTION ‘LVC_FIELDCATALOG_MERGE’

    EXPORTING

      i_structure_name       = ‘SFLIGHT’

    CHANGING

      ct_fieldcat            = gt_fcat[]

    EXCEPTIONS

      inconsistent_interface = 1

      program_error          = 2

      OTHERS                 = 3.

  IF sy-subrc <> 0.

    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

  ENDIF.

  CALL FUNCTION ‘LVC_FIELDCATALOG_MERGE’

    EXPORTING

      i_structure_name       = ‘SPFLI’

    CHANGING

      ct_fieldcat            = gt_fcat1[]

    EXCEPTIONS

      inconsistent_interface = 1

      program_error          = 2

      OTHERS                 = 3.

  IF sy-subrc <> 0.

    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

  ENDIF.

  SELECT * FROM sflight INTO TABLE gt_sflight UP TO 15 ROWS.

  SELECT * FROM spfli INTO TABLE gt_spfli UP TO 15 ROWS.

ENDFORM.                    ” zf_set_fieldcatalog

&—-


*&      Form  zf_add_header

&—-


FORM zf_add_header .

*Add header

  • program ID

  DATA : dl_text(255) TYPE c.  “Text

  dl_text = ‘Program Name :’.

  CALL METHOD ref_dd->add_gap.

  CALL METHOD ref_dd->add_text

    EXPORTING

      text         = dl_text

      sap_emphasis = cl_dd_area=>heading

      sap_color    = cl_dd_area=>list_heading_int.

  CLEAR dl_text.

  dl_text = sy-repid.

  CALL METHOD ref_dd->add_text

    EXPORTING

      text         = dl_text

      sap_emphasis = cl_dd_area=>heading

      sap_color    = cl_dd_area=>list_negative_inv.

  • Add new-line

  CALL METHOD ref_dd->new_line.

  CLEAR : dl_text.

  • program ID

  dl_text = ‘User Name :’.

  CALL METHOD ref_dd->add_gap.

  CALL METHOD ref_dd->add_text

    EXPORTING

      text         = dl_text

      sap_emphasis = cl_dd_area=>heading

      sap_color    = cl_dd_area=>list_heading_int.

  CLEAR dl_text.

  dl_text = sy-uname.

  CALL METHOD ref_dd->add_text

    EXPORTING

      text         = dl_text

      sap_emphasis = cl_dd_area=>heading

      sap_color    = cl_dd_area=>list_negative_inv.

  • Add new-line

  CALL METHOD ref_dd->new_line.

  CLEAR : dl_text.

  • Run Date

  dl_text = ‘Run Date :’.

  CALL METHOD ref_dd->add_gap.

  CALL METHOD ref_dd->add_text

    EXPORTING

      text         = dl_text

      sap_emphasis = cl_dd_area=>heading

      sap_color    = cl_dd_area=>list_heading_int.

  CLEAR dl_text.

  • Move date

  WRITE sy-datum TO dl_text.

  CALL METHOD ref_dd->add_text

    EXPORTING

      text         = dl_text

      sap_emphasis = cl_dd_area=>heading

      sap_color    = cl_dd_area=>list_negative_inv.

  • Add new-line

  CALL METHOD ref_dd->new_line.

  CLEAR : dl_text.

*Time

  dl_text = ‘Time :’.

  CALL METHOD ref_dd->add_gap.

  CALL METHOD ref_dd->add_text

    EXPORTING

      text         = dl_text

      sap_emphasis = cl_dd_area=>heading

      sap_color    = cl_dd_area=>list_heading_int.

  CLEAR dl_text.

  • Move time

  WRITE sy-uzeit TO dl_text.

  CALL METHOD ref_dd->add_text

    EXPORTING

      text         = dl_text

      sap_emphasis = cl_dd_area=>heading

      sap_color    = cl_dd_area=>list_negative_inv.

  • Add new-line

  CALL METHOD ref_dd->new_line.

ENDFORM.                    ” zf_add_header

image

To report this post you need to login first.

3 Comments

You must be Logged on to comment or reply to a post.

  1. Antonio Esteve Guill
    During the migration process problems have been reported for this blog. The blog content may look corrupt due to not supported HTML code on this platform. Please adjust the blog content manually before moving it to an official community.
    (0) 
    1. Former Member Post author
      You can use a dynpro.you can create an empty screen with a container or you can use the attribute cl_gui_custom_container=>screen0 also.
      (0) 

Leave a Reply