Friday, April 10, 2020

Code for sending Smartforms to E-Mail from SAP

  Code for sending Smartforms to  E-Mail from SAP
                                (Salary Slip)
 "Local Object References
DATAlo_bcs         TYPE REF TO cl_bcs,
      lo_doc_bcs     TYPE REF TO cl_document_bcs,
      lo_recep       TYPE REF TO if_recipient_bcs,
      lo_sapuser_bcs TYPE REF TO cl_sapuser_bcs,
      lo_cx_bcx      TYPE REF TO cx_bcs.

"Local Internal Tables.
DATAlt_otfdata        TYPE ssfcrescl,
      lt_binary_content TYPE solix_tab,
      lt_text           TYPE bcsy_text,
      lt_pdf_tab TYPE STANDARD TABLE OF tline,
      lt_otf     TYPE STANDARD TABLE OF itcoo.

"Local Structures
DATAls_ctrlop TYPE ssfctrlop,
      ls_outopt TYPE ssfcompop.

"Local Variables
DATAlv_bin_filesize TYPE so_obj_len,
      lv_sent_to_all  TYPE os_boolean,
      lv_bin_xstr     TYPE xstring,
      lv_fname        TYPE rs38l_fnam,
      lv_string_text  TYPE string.

*-----------------------------------------------------------------------------------*
*
*-----------------------------------------------------------------------------------*
DATA WA_OUTOPT  TYPE SSFCOMPOP.
  DATA L_VALID    TYPE C.
  DATA LT_ROW_NO  TYPE LVC_T_ROID WITH HEADER LINE.
  DATA T_OTFDATA  TYPE SSFCRESCL.
  DATA LF_FM_NAME TYPE RS38L_FNAM.
  DATA OTF_TABLE  TYPE TABLE OF ITCOO.
  DATA CONTROL_PARAMETERS TYPE SSFCTRLOP.


  CALL METHOD GR_ALVGRID->CHECK_CHANGED_DATA
    IMPORTING
      E_VALID L_VALID.


  CALL METHOD GR_ALVGRID->GET_SELECTED_ROWS "getting the selected row numbers
    IMPORTING
      ET_ROW_NO LT_ROW_NO[].



DATA itt_final TYPE TABLE OF ty_final,
       waa_final TYPE          ty_final.
  LOOP AT LT_ROW_NO.
    READ  TABLE gt_final into DATA(gsk_finalINDEX  LT_ROW_NO-ROW_ID.
    WAA_FINAL-PERNR GSK_FINAL-PERNR.
    WAA_FINAL-WERKS GSK_FINAL-WERKS.
    APPEND WAA_FINAL TO ITT_FINAL.
    CLEARWAA_FINAL,GSK_FINAL.
  ENDLOOP.

  SORT ITT_FINAL ASCENDING.
  DELETE ADJACENT DUPLICATES FROM ITT_FINAL COMPARING ALL FIELDS.

  LOOP AT ITT_FINAL INTO WAA_FINAL.
      LOOP AT GT_FINAL INTO DATA(GSS_FINALWHERE PERNR EQ WAA_FINAL-PERNR.
    gs_salary-emp_code              gss_final-pernr.
    gs_salary-emp_name              gss_final-name.
    gs_salary-designation           gss_final-designation.
    CONCATENATE gss_final-begda gss_final-gender INTO DATA(gsss_final-dojSEPARATED BY space.
    gs_salary-doj                   gsss_final-doj.
    gs_salary-pan_no                gss_final-pernr.
    gs_salary-pf_val                gss_final-pf.
    gs_salary-pf_no                 gss_final-eepfm.
    gs_salary-esic_no               gss_final-esinm.
*    gs_salary-esic_no               = gss_final-esi.
*    gs_salary-gender                = gss_final-gender.

    CONCATENATE gss_final-banka gss_final-bankn INTO DATA(gsss_final-bankn).
    gs_salary-bank_name             gsss_final-bankn.
    gs_salary-location              gss_final-orgeh.
    gs_salary-department            gss_final-org_desc.
    gs_salary-pd                    gss_final-pd.
    gs_salary-lwp                   gss_final-lwp.
    gs_salary-basic                 gss_final-bET01.
    gs_salary-earn_basic            gss_final-basic.
    gs_salary-hra                   gss_final-BET02.
    gs_salary-EARN_hra              gss_final-hra.
    gs_salary-total_deduction       gss_final-total_deduction.
    gs_salary-esi                   gss_final-esi.
    gs_salary-pf_val                gss_final-pf.
    gs_salary-mess                  gss_final-mess.
    gs_salary-tds                   gss_final-tds.
    gs_salary-loan                  gss_final-loan.
    gs_salary-advance               gss_final-advance.
    gs_salary-transport_val         gss_final-transport.
    gs_salary-arr_pf                gss_final-arrear_pf.
    gs_salary-loan_monthly          gss_final-loan_monthly.
    gs_salary-net_pay               gss_final-net_pay.
    gs_salary-egross                gss_final-egross.
    gs_salary-gross                 gss_final-gross.
    gs_salary-MEDICAL               gss_final-BET04.
    GS_SALARY-EARN_MEDICAL          GSS_FINAL-MEDICAL.
    GS_SALARY-PHONE                 GSS_FINAL-PHONE.
    GS_SALARY-CONV                  GSS_FINAL-BET03.
    GS_SALARY-EARN_CONV             GSS_FINAL-CONV.
    GS_SALARY-EARN_FIX              GSS_FINAL-FIX.
    GS_SALARY-OTHERS                GSS_FINAL-OTHER.
    GS_SALARY-SALES_INCENTIVE       GSS_FINAL-SALES_INCENTIVE.
    GS_SALARY-ARR_BASIC             GSS_FINAL-ARREAR_BASIC.
    GS_SALARY-ARR_HRA               GSS_FINAL-ARREAR_HRA.
    GS_SALARY-ARR_CONV              GSS_FINAL-ARREAR_CONV.
    GS_SALARY-ARR_SP_ALLOW          GSS_FINAL-ARREAR_SALLOW.
    GS_SALARY-ARR_MEDICAL           =  GSS_FINAL-ARREAR_MEDICAL.
    GS_SALARY-ARR_FIX               GSS_FINAL-ARREAR_FIX.
    GS_SALARY-ARR_OTHER             GSS_FINAL-ARREAR_OTHER.
    GS_SALARY-ARR_ANNUAL            GSS_FINAL-ARREAR_ANNUAL.
    GS_SALARY-PROJECT_BONUS         GSS_FINAL-PROJECT_BONUS.
    GS_SALARY-EXGRATIA              GSS_FINAL-EXGRATIA.


    DATA :  fm_name TYPE rs38l_fnam.
*    do 3 times.
    wa_outopt-tdcopies 003.

*    do 3 times.
*    wa_outopt-tdcopies = 003.
    CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
      EXPORTING
        formname           'ZHR_SALARY_SLIP1'
*       VARIANT            = ' '
*       DIRECT_CALL        = ' '
      IMPORTING
        fm_name            fm_name
      EXCEPTIONS
        no_form            1
        no_function_module 2
        OTHERS             3.
    IF sy-subrc 0.
       control_parameters-device    space.
      control_parameters-getotf    'X'.
      control_parameters-no_dialog 'X'.
*      control_parameters-preview   = ' '.

    ENDIF.

    CALL FUNCTION fm_name
      EXPORTING
*       ARCHIVE_INDEX      =
*       ARCHIVE_INDEX_TAB  =
*       ARCHIVE_PARAMETERS =
*       CONTROL_PARAMETERS =
*       MAIL_APPL_OBJ      =
*       MAIL_RECIPIENT     =
*       MAIL_SENDER        =
*       OUTPUT_OPTIONS     =
        control_parameters control_parameters
        output_options     wa_outopt
        user_settings      'X'
        im_salary          gs_salary
        im_BUKRS           TEXT1
         Im_month          GV_PABRP_DES  "Added on 09.04.2020
      IMPORTING
*       DOCUMENT_OUTPUT_INFO       =
        job_output_info    t_otfdata
*       JOB_OUTPUT_OPTIONS =
      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.
    ELSE.
      APPEND LINES OF t_otfdata-otfdata TO otf_table.
    ENDIF.


CALL FUNCTION 'CONVERT_OTF'
  EXPORTING
    format                'PDF'   "PDF to get pdf output
  IMPORTING
    bin_filesize          lv_bin_filesize
    bin_file              lv_bin_xstr
  TABLES
    otf                   otf_table[]
    lines                 lt_pdf_tab[]
  EXCEPTIONS
    err_max_linewidth     1
    err_format            2
    err_conv_not_possible 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.


*<code>***Xstring to binary
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
  EXPORTING
    buffer     lv_bin_xstr
  TABLES
    binary_tab lt_binary_content.


TRY.
*     -------- create persistent send request ------------------------
    lo_bcs cl_bcs=>create_persistent).

     CONCATENATE 'Dear Colleague' cl_abap_char_utilities=>newline INTO lv_string_text.
    APPEND lv_string_text TO lt_text.
    CLEAR lv_string_text.
    "Line-2
    CONCATENATE 'Please find attached a Salary Slip.'
     cl_abap_char_utilities=>newline INTO lv_string_text.
    APPEND lv_string_text TO lt_text.
    CLEAR lv_string_text.
    "Line-3
    APPEND 'Best Regards,' TO lt_text.
    "Line-4
    APPEND 'Systems Administrator.' TO lt_text.


      lo_doc_bcs cl_document_bcs=>create_document(
                    i_type    'RAW'
                    i_text    lt_text[]
                    i_length  '12'
                    i_subject 'Salary Slips' ).   "Subject of the Email</code>

*BCS expects document content here e.g. from document upload
*     binary_content = ...
    CALL METHOD lo_doc_bcs->add_attachment
      EXPORTING
        i_attachment_type    'PDF'
        i_attachment_size    lv_bin_filesize
        i_attachment_subject 'Test Email'
        i_att_content_hex    lt_binary_content.

*     add document to send request
    CALL METHOD lo_bcs->set_documentlo_doc_bcs ).



  lo_recep cl_cam_address_bcs=>create_internet_address(
                                                'soouravrai@gmail.com' ).

"Add recipient with its respective attributes to send request
    CALL METHOD lo_bcs->add_recipient
      EXPORTING
        i_recipient lo_recep
        i_express   'X'.

 CALL METHOD lo_bcs->set_send_immediately
      EXPORTING
        i_send_immediately 'X'.


* *-----------------Send the email    *-----------------------------
*---------------------------------------------------------------------
    CALL METHOD lo_bcs->send(
      EXPORTING
        i_with_error_screen 'X'
      RECEIVING
        result              lv_sent_to_all ).

    IF lv_sent_to_all IS NOT INITIAL.
      COMMIT WORK.
    ENDIF.


*---------------------------------------------------------------------
*----------------------   Exception Handling *------------------------
*---------------------------------------------------------------------
  CATCH cx_bcs INTO lo_cx_bcx.
    "Appropriate Exception Handling
    WRITE'Exception:'lo_cx_bcx->error_type.
ENDTRY.

* Implement suitable error handling here



  ENDLOOP.
  ENDLOOP.