Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

Back to Sending Mails - Home Page

The following code is not standalone: it can't compile. Use it as a template.

SEND ALV GRID AS PDF ATTACHMENT TO SAP INBOX

USING CLASSESWith This Code you will be able to Schedule ALV Grid That has

Width more than 255 in the Background Then convert it into

Spool and then Send the Spool as PDF Attachment using classes.

Error rendering macro 'code': Invalid value specified for parameter 'lang'
CONSTANTS c_container TYPE c LENGTH 30 VALUE 'CC'.
CONSTANTS c_col1 TYPE c LENGTH 2 VALUE ' ~'.
CONSTANTS c_col2 TYPE c LENGTH 2 VALUE '~ '.
CONSTANTS c_255 TYPE i VALUE 255.
CONSTANTS p_recpnt TYPE syuname VALUE 'GEORGE'.

DATA gt_final TYPE TABLE OF t000.
DATA: lv_buffer TYPE string,
      lv_spool_nr TYPE tsp01-rqident,
      lt_mess_att TYPE TABLE OF solisti1 ,
      ls_mess_att TYPE solisti1.
TYPES: ty_t_pdf TYPE STANDARD TABLE OF tline.
DATA: lt_pdf_output TYPE ty_t_pdf,
ls_pdf_output TYPE tline,
l_ref_bcs TYPE REF TO cl_bcs,
l_ref_document TYPE REF TO cl_document_bcs,
l_ref_recipient TYPE REF TO if_recipient_bcs,
l_ref_bcs_exception TYPE REF TO cx_bcs,
lv_sent_to_all TYPE os_boolean,
lv_filesize TYPE so_obj_len,
lv_doclines TYPE i,
lv_last_line TYPE i,
lv_text TYPE so_obj_des,
lv_data TYPE bcsy_text,
lv_size TYPE i,
gt_fcat TYPE lvc_t_fcat.
DATA:g_ref_container TYPE REF TO cl_gui_custom_container,
      g_ref_grid TYPE REF TO cl_gui_alv_grid,
      gv_off TYPE int4,
      gs_print TYPE lvc_s_prnt,
*      g_ref_rec TYPE REF TO lcl_event_receiver,
      gs_variant TYPE disvariant,
      gs_layo TYPE lvc_s_layo.
* Create alv grid along WITH printer settings for background execution purpose.
** custom container
IF g_ref_container IS NOT BOUND.
** offline method check required for background execution
  CALL METHOD cl_gui_alv_grid=>offline
    RECEIVING
      e_offline = gv_off.
  IF gv_off IS INITIAL.
    CREATE OBJECT g_ref_container
      EXPORTING
        container_name = c_container.
  ENDIF.
** instance of alv grid.
  IF g_ref_grid IS NOT BOUND.
    CREATE OBJECT g_ref_grid
      EXPORTING
        i_parent = g_ref_container.
  ENDIF.
** build field catalog
  PERFORM build_fieldcat1
        CHANGING gt_fcat.
** layout
  gs_layo-cwidth_opt = 'X'. "OPTIMUM WIDTH OF THE COLUMN
  gs_layo-zebra = 'X'. "Alternate line coloring

** Set Print Parameters (Required If The ALV Output has Length more than 255)
  CALL FUNCTION 'SET_PRINT_PARAMETERS'
    EXPORTING
      destination = 'LOCL' " Printer
      layout      = 'X_65_512/2' "Format
      line_count  = '65' "Line Count
      line_size   = '1024'. "Line Size
  gs_print-print = 'X'.
  CALL METHOD g_ref_grid->set_table_for_first_display
    EXPORTING
      i_save                        = 'A'
      is_layout                     = gs_layo "Layout
      is_print                      = gs_print "For Creating Spool
    CHANGING
      it_outtab                     = gt_final "Final Internal Table Containing Data to Display
      it_fieldcatalog               = gt_fcat "Fieldcatalog
    EXCEPTIONS
      invalid_parameter_combination = 1
      program_error                 = 2
      too_many_lines                = 3
      OTHERS                        = 4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
*convert spool to pdf and send as attachment to sap inbox.
  MOVE sy-spono TO lv_spool_nr.
  CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'
    EXPORTING
      src_spoolid                    = lv_spool_nr "Spool Number
      no_dialog                      = space
      dst_device                     = 'LOCL' "Printer Name
      importingpdf_bytecount         = lv_size "Output Size
      tablespdf                      = lt_pdf_output "Spool data in PDF Format
      exceptionserr_no_abap_spooljob = 1
      err_no_spooljob                = 2
      err_no_permission              = 3
      err_conv_not_possible          = 4
      err_bad_destdevice             = 5
      user_cancelled                 = 6
      err_spoolerror                 = 7
      err_temseerror                 = 8
      err_btcjob_open_failed         = 9.
  IF sy-subrc EQ 0.
    LOOP AT lt_pdf_output INTO ls_pdf_output.
      TRANSLATE ls_pdf_output USING c_col1.
      CONCATENATE lv_buffer ls_pdf_output INTO lv_buffer.
      CLEAR ls_pdf_output.
    ENDLOOP.
    TRANSLATE lv_buffer USING c_col2.
    DO.
      ls_mess_att = lv_buffer.
      APPEND ls_mess_att TO lt_mess_att.
      SHIFT lv_buffer LEFT BY c_255 PLACES.
      IF lv_buffer IS INITIAL.
        EXIT.
      ENDIF.
      CLEAR ls_mess_att.
    ENDDO.
  ENDIF.
*--create reference object
  TRY.
      l_ref_bcs = cl_bcs=>create_persistent( ).
      lv_text = 'Mail Subject' .
      APPEND 'Mail Content' TO lv_data.
      l_ref_document = cl_document_bcs=>create_document(
      i_type = 'RAW'
      i_text = lv_data
      i_subject = lv_text ).
* *--create document reference
      lv_filesize = lv_size.
      CALL METHOD l_ref_document->add_attachment
        EXPORTING
          i_attachment_type    = 'PDF'
          i_attachment_size    = lv_filesize
          i_attachment_subject = lv_text
          i_att_content_text   = lt_mess_att[].
* set the document
      l_ref_bcs->set_document( l_ref_document ).
* create recipient
*      p_recpnt = sap INBOX user ID to WHICH the ATTACHMENT is SENT.
      l_ref_recipient = cl_sapuser_bcs=>create( p_recpnt ).
*adding recipient in to section
      l_ref_bcs->add_recipient( l_ref_recipient ).

* send mail
      CALL METHOD l_ref_bcs->send(
      EXPORTING i_with_error_screen = 'X'
      RECEIVING
      result = lv_sent_to_all ).
      IF lv_sent_to_all = 'X'.
      ENDIF.
    CATCH cx_bcs INTO l_ref_bcs_exception.
      IF l_ref_bcs_exception IS NOT INITIAL.
        CLEAR l_ref_bcs_exception.
      ENDIF.
  ENDTRY.

  COMMIT WORK.
ENDIF.

  • No labels