Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

Code to Send Smart Form as PDF attachment Using CL_BCS class.

Multiple PDF's can be sent as attachments in a single mail.

REPORT ZTEST_PDF_MAIL

*/.. Internal Table Declaration

DATA: li_otf                 type table of itcoo,          

          li_pdf_tab          type table of tline,

          li_content_txt    type soli_tab,         

          li_content_hex  type solix_tab,

          li_objhead         type soli_tab,        

          gi_main_text     type bcsy_text.

*/.. Work Area Declarations

DATA: lw_control_parameters type ssfctrlop,
           lw_output_options        type ssfcompop,
          

           lw_ssfcrescl                type ssfcrescl,

           lw_content                  type soli,

           lw_otf                         type itcoo.

*/..Variables Decalrations   

DATA: gv_fname          type rs38l_fnam,       "Fucntion Module          

          gv_subject         type so_obj_des,
          gv_title              type so_obj_des,

          lv_bin_filesize    type i,          

          lv_transfer_bin   type sx_boolean,
          lv_len                type so_obj_len,

          lv_email            type ad_smtpadr,

          lv_sent_to_all    type os_boolean.

*/.. Class Declarations

DATA: go_send_request   type ref to  cl_bcs,
          go_document         type ref to  cl_document_bcs,

          lo_recipient            type ref to  if_recipient_bcs,
          lo_bcs_exception   type ref to  cx_bcs.

call function 'SSF_FUNCTION_MODULE_NAME'
    exporting
      formname           = 'ZF2_TEST'
   
importing
      fm_name            = gv_fname
    exceptions
      no_form                  = 1
      no_function_module = 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.

*/.. Get OTF data
  lw_control_parameters-getotf        = 'X'.


*/.. To supress preview
  lw_control_parameters-no_dialog  = 'X'.

  lw_control_parameters-langu        = sy-langu.

  lw_output_options-tdnoprev          = 'X'.

*/.. To add multiple attachments write below code till Add attachment method in LOOP and ENDLOOP.

*/.. Call Smart Form FM
    call function gv_fname
      exporting
         control_parameters = lw_control_parameters
         output_options       = lw_output_options
         user_settings         = 'X'
     importing
         job_output_info    = lw_ssfcrescl
      exceptions
         formatting_error   = 1
         internal_error       = 2
         send_error           = 3
         user_canceled     = 4
         others                 = 5.

   if sy-subrc <> 0.
      message id sy-msgid type sy-msgty number sy-msgno
              with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    endif.

*/.. Get OTF data to convert to PDF
   refresh li_otf[].
    li_otf[] = lw_ssfcrescl-otfdata[].

   clear : lv_bin_filesize.


*/.. Convert OTF data to binary
    call function 'CONVERT_OTF'
      exporting
        format                = 'PDF'
      importing
        bin_filesize         = lv_bin_filesize
      tables
       otf                      = li_otf
        lines                   = li_pdf_tab
      exceptions
        err_max_linewidth       = 1
        err_format                   = 2
        err_conv_not_possible = 3
        err_bad_otf                 = 4

        others                        = 5.

    if sy-subrc <> 0.
      message id sy-msgid type sy-msgty number sy-msgno
              with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    endif.

      

    refresh li_content_txt.

    loop at li_otf  into  lw_otf.

      clear lw_content.

      concatenate lw_otf-tdprintcom lw_otf-tdprintpar

             into lw_content.

      append lw_content to li_content_txt.

    endloop.

    refresh : li_content_hex,
                 li_objhead.


    clear : lv_transfer_bin,
              lv_len.

*/.. FM to convert OTF to PDF
    call function 'SX_OBJECT_CONVERT_OTF_PDF'
      exporting
        format_src      = 'OTF'
        format_dst      = 'PDF'
      changing
        transfer_bin     =  lv_transfer_bin
        content_txt      =  li_content_txt
        content_bin     =  li_content_hex
        objhead          =  li_objhead
        len                 =  lv_len
      exceptions
        err_conv_failed = 1
        others             = 2.


    if sy-subrc <> 0.
      message id sy-msgid type sy-msgty number sy-msgno
              with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    endif.

    try.

*/.. Create persistent send request
      go_send_request = cl_bcs=>create_persistent( ).

      go_document = cl_document_bcs=>create_document(

            i_type     = 'RAW'

            i_text      = gi_main_text

            i_subject = gv_subject ).


       if not li_content_hex is initial.
          go_document->add_attachment(
            i_attachment_type      =  'PDF'                   

            i_attachment_subject  =  gv_title                

            i_attachment_size      =   lv_len
            i_att_content_hex       =   li_content_hex ).
        endif.

*/.. Add document object to send request
      go_send_request->set_document( go_document ).

      lv_email = 'abcd@defghijkl.com'.

*/.. Add recipient (e-mail address)
*/.. create recipient object
      lo_recipient = cl_cam_address_bcs=>create_internet_address(
      lv_email ).

*/.. Add recipient object to send request
      go_send_request->add_recipient( lo_recipient ).

*/.. Send document
      lv_sent_to_all = go_send_request->send( i_with_error_screen =
      abap_true ).

  commit work.

      if lv_sent_to_all is initial.
          "Error Message

      else.
          "Success Message
      endif.


*/.. Exception handling
     catch cx_bcs into lo_bcs_exception.
        "Exception Handling

     endtry.

4 Comments