Tuesday, December 6, 2016

Report Output via Email

In the following program we are sending the report output via email. To achieve this we are using a standard Function Module - EFG_GEN_SEND_EMAIL

REPORT zsr_test NO STANDARD PAGE HEADING.

TABLESekko.
TYPES:
  BEGIN OF ty_ekko,
    ebeln TYPE ekko-ebeln,
  END OF ty_ekko.

DATA:
  wa_ekko       TYPE ty_ekko,
  it_ekko       TYPE TABLE OF ty_ekko,
  lv_date       TYPE sy-datum,
  date_external TYPE char12,

  i_title       TYPE char100,
  i_recipient   TYPE char50.

START-OF-SELECTION.
  PERFORM fetch_data.
  PERFORM convert_date.
  PERFORM send_email.

*&---------------------------------------------------------------------*
*&      Form  FETCH_DATA
*&---------------------------------------------------------------------*
*       Fetch data of PO from EKKO
*----------------------------------------------------------------------*
FORM fetch_data .

  CLEAR lv_date.
  lv_date sy-datum 30.

  SELECT ebeln FROM ekko
    INTO TABLE it_ekko
    WHERE aedat GT lv_date.

  IF sy-subrc 0.
    SORT it_ekko.
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  CONVERT_DATE
*&---------------------------------------------------------------------*
*       Convert date to External format
*----------------------------------------------------------------------*
FORM convert_date .

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

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  SEND_EMAIL
*&---------------------------------------------------------------------*
*       Sending mail
*----------------------------------------------------------------------*
FORM send_email .

  IF it_ekko IS NOT INITIAL.

    CONCATENATE 'Purchase Order List on : '
                date_external
    INTO i_title SEPARATED BY ' '.
    i_recipient 'sandip.aim@gmail.com'.

    CALL FUNCTION 'EFG_GEN_SEND_EMAIL'
      EXPORTING
        i_title                i_title
        i_sender               sy-uname
        i_recipient            i_recipient
        i_flg_commit           'X'
        i_flg_send_immediately 'X'
      TABLES
        i_tab_lines            it_ekko
*       I_TAB_RECIPIENTS       =
      EXCEPTIONS
        not_qualified          1
        failed                 2
        OTHERS                 3.
  ENDIF.

ENDFORM.


In this example the output is sent to only one email ID. We can use multiple email IDs by passing a table I_TAB_RECIPIENTS. We can maintain a custom table which contains mail IDs. The email will come as follows.




Monday, November 28, 2016

Currency Table update Automatically

In the following example we are updating the currency table TCURR (standard one) by a custom program. The purpose of this program is to update the TCURR table automatically by a background job so that user doesn’t need to enter manually in OB08. Hence the custom program will fetch data from an excel sheet which is located in the application server. The excel file is updated one and it is downloaded time to time from the RBI website. To generate this excel file we can use any web development technique.


Now our custom program will fetch that excel file data into its own internal table. We are using a BAPI (BAPI_EXCHANGERATE_CREATE) here. This BAPI passes a work area exch_rate which contains all information for required currency. The excel file looks like following.























REPORT zsr_test NO STANDARD PAGE HEADING.

DATA:
  it_intern     TYPE TABLE OF alsmex_tabline,
  wa_intern     TYPE alsmex_tabline,
  return        LIKE bapiret2,
  commit_return LIKE bapiret2,
  exch_rate     LIKE bapi1093_0,
  currency_file TYPE string VALUE 'D:\SUM\SUM\abap\currency.xls'.

START-OF-SELECTION.
  PERFORM fetch_data.
  PERFORM call_bapi_to_update_tcurr.

*&---------------------------------------------------------------------*
*&      Form  FETCH_DATA
*&---------------------------------------------------------------------*
*       Fetching Excel data from App Server into Internal Table
*----------------------------------------------------------------------*
FORM fetch_data .

  OPEN DATASET currency_file FOR INPUT IN TEXT MODE ENCODING DEFAULT.

  DO.
    READ DATASET currency_file INTO wa_intern-value.
    IF sy-subrc NE 0.
      EXIT.
    ENDIF.
    APPEND wa_intern TO it_intern.
  ENDDO.

  CLOSE DATASET currency_file.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  CALL_BAPI_TO_UPDATE_TCURR
*&---------------------------------------------------------------------*
*       Calling BAPI to Update TCURR Directly
*----------------------------------------------------------------------*
FORM call_bapi_to_update_tcurr .

  IF it_intern IS NOT INITIAL.
    LOOP AT it_intern INTO wa_intern.
      exch_rate-rate_type 'M'.

      CASE sy-tabix.
          "First record is for US Dollar
        WHEN '1'.
          exch_rate-from_curr 'USD'.

          "Second record is for Euro
        WHEN '2'.
          exch_rate-from_curr 'EUR'.

          "Third record is for Yen
        WHEN '3'.
          exch_rate-from_curr 'JPY'.

          "Fourth record is for Pound
        WHEN '4'.
          exch_rate-from_curr 'GBP'.

          "Any further data will not be entered
        WHEN OTHERS.
          EXIT.
      ENDCASE.

      exch_rate-to_currncy 'INR'.
      exch_rate-valid_from sy-datum.
      exch_rate-exch_rate wa_intern-value.

      IF exch_rate-from_curr 'JPY'.
        "For YEN the ratio is different
        exch_rate-from_factor 100.
      ELSE.
        exch_rate-from_factor 1.
      ENDIF.

      exch_rate-to_factor 1.

      CALL FUNCTION 'BAPI_EXCHANGERATE_CREATE'
        EXPORTING
          exch_rate exch_rate
          upd_allow 'X'
          chg_fixed 'X'
        IMPORTING
          return    return.

      IF   return-type NE 'E'
        OR return-type NE 'A'.

        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
            wait   'X'
          IMPORTING
            return commit_return.
      ENDIF.
      CLEARwa_internreturnexch_rate.
    ENDLOOP.
  ENDIF.

ENDFORM.

The report will not generate any output. We need to check TCURR table based on the following data.












































Friday, November 25, 2016

Indian Currency Amount to Word

In the following program we are converting an amount into words. The amount is Indian currency which is converted into text. Here we are calling a standard function module HR_IN_CHG_INR_WRDS  and after that we are converting the case of words.

REPORT zsr_test NO STANDARD PAGE HEADING.

PARAMETERS p_amt TYPE pc207-betrg.
DATA v_amt TYPE char100.

CALL FUNCTION 'HR_IN_CHG_INR_WRDS'
  EXPORTING
    amt_in_num         p_amt
  IMPORTING
    amt_in_words       v_amt
  EXCEPTIONS
    data_type_mismatch 1
    OTHERS             2.

CALL FUNCTION 'ISP_CONVERT_FIRSTCHARS_TOUPPER'
  EXPORTING
    input_string  v_amt
    separators    ' '
  IMPORTING
    output_string v_amt.

WRITE'Amount in Words: 'v_amt.




Monday, November 21, 2016

Download ALV Output into Application Server

In the following example we have created a program which displays an ALV grid output on the foreground and generates a text file into application server. We must have access to the specific application server path. Otherwise the report will get dump. 

If we create a background job for this report then the text file will be generated into the application server without having any user interaction at foreground. This report generates Material list based on the material type.

If there is no data based on the material type then the text file will only contain the headers. Every time the file will be overwritten in the application server.

REPORT zsr_test NO STANDARD PAGE HEADING.

TABLESmara.
TYPE-POOLS slis.
TYPESBEGIN OF ty_mara,
         matnr TYPE mara-matnr,
         mtart TYPE mara-mtart,
       END OF ty_mara.

DATA:
  "Internal Tables & Work Area
  wa_mara TYPE ty_mara,
  it_mara TYPE TABLE OF ty_mara.

DATA:
  "Field Catalogue
  wa_fcat   TYPE slis_fieldcat_alv,
  it_fcat   TYPE slis_t_fieldcat_alv,

  "Layout
  wa_layout TYPE slis_layout_alv,

  "Top of Page
  wa_top    TYPE slis_listheader,
  it_top    TYPE slis_t_listheader.

INITIALIZATION.
  PARAMETERSp_mtart TYPE mara-mtart.

START-OF-SELECTION.
  PERFORM get_mara"--------------------------------------Get Materials

END-OF-SELECTION.
  PERFORM field_catalog"-------------------------------Field Catalogue
  PERFORM layout"-------------------------------------------Get Layout
  PERFORM transfer_output"----------------------Transferring into Text
  PERFORM alv_output"---------------------------------------ALV Output

TOP-OF-PAGE.
  PERFORM top_of_page"-------------------------------------Top of Page

*&---------------------------------------------------------------------*
*&      Form  GET_MARA
*&---------------------------------------------------------------------*
*       Get data from MARA
*----------------------------------------------------------------------*
FORM get_mara .

  IF p_mtart IS NOT INITIAL.
    SELECT matnr mtart
      FROM mara INTO TABLE it_mara
      WHERE mtart p_mtart.

    IF sy-subrc 0.
      SORT it_mara.
    ENDIF.
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  FIELD_CATALOG
*&---------------------------------------------------------------------*
*       Field Catalogue
*----------------------------------------------------------------------*
FORM field_catalog .

  DATAlv_col TYPE VALUE 0.

  IF it_mara IS NOT INITIAL.
    lv_col            + lv_col.
    wa_fcat-col_pos   lv_col.
    wa_fcat-fieldname 'MATNR'.
    wa_fcat-seltext_l 'Material No'.
    APPEND wa_fcat TO it_fcat.
    CLEAR  wa_fcat.

    lv_col            + lv_col.
    wa_fcat-col_pos   lv_col.
    wa_fcat-fieldname 'MTART'.
    wa_fcat-seltext_l 'Material Type'.
    APPEND wa_fcat TO it_fcat.
    CLEAR  wa_fcat.

  ELSE.
    MESSAGE 'No Material Found' TYPE 'I'.
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  LAYOUT
*&---------------------------------------------------------------------*
*       Prepare Layout
*----------------------------------------------------------------------*
FORM layout .

  wa_layout-zebra             'X'.
  wa_layout-colwidth_optimize 'X'.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  TRANSFER_OUTPUT
*&---------------------------------------------------------------------*
*       Transferring into Text
*----------------------------------------------------------------------*
FORM transfer_output .

  DATAlv_out   TYPE string,
        lv_fname TYPE string.

  "It will store the text file into Application Server Path
  lv_fname 'D:\SUM\SUM\abap\MATERIALS.TXT'.

  OPEN DATASET lv_fname FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

  "Header in Text file
  CONCATENATE 'Material_No.'
              'Material_Type'
              INTO lv_out SEPARATED BY ' '.
  TRANSFER lv_out TO lv_fname.

  "Data in Text file
  LOOP AT it_mara INTO wa_mara.
    CONCATENATE wa_mara-matnr
                wa_mara-mtart
                INTO lv_out SEPARATED BY ' '.
    TRANSFER lv_out TO lv_fname.
    CLEARwa_maralv_out.
  ENDLOOP.
  CLOSE DATASET lv_fname.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  ALV_OUTPUT
*&---------------------------------------------------------------------*
*       ALV Output
*----------------------------------------------------------------------*
FORM alv_output .

  IF it_fcat IS NOT INITIAL.
    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
      EXPORTING
        i_callback_program     sy-repid
        i_callback_top_of_page 'TOP_OF_PAGE'
        is_layout              wa_layout
        it_fieldcat            it_fcat
      TABLES
        t_outtab               it_mara
      EXCEPTIONS
        program_error          1
        OTHERS                 2.
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  TOP_OF_PAGE
*&---------------------------------------------------------------------*
*       Top of Page
*----------------------------------------------------------------------*
FORM top_of_page .

  CLEAR wa_top.
  REFRESH it_top.

  wa_top-typ  'H'.
  wa_top-info 'Material List'.
  APPEND wa_top TO it_top.
  CLEAR  wa_top.

  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary it_top.

ENDFORM.
















































Now go to AL11 from where we can see the directories of application server.









Double click on the file.




































If the file is blank then it will look like following.


Thursday, November 17, 2016

Multiple Reports from One Single Report

One report can call multiple reports without any intervention. In the following example we have a program which calls ten different reports at single time. Those ten reports will be executed one by one without any interaction of users. The output of those reports execute when click on back button on the output pages.

REPORT zsr_test NO STANDARD PAGE HEADING.

DATAnumber           TYPE tbtcjob-jobcount,
      name             TYPE tbtcjob-jobname VALUE 'JOB_TEST',
      print_parameters TYPE pri_params.

START-OF-SELECTION.

  CALL FUNCTION 'JOB_OPEN'
    EXPORTING
      jobname          name
    IMPORTING
      jobcount         number
    EXCEPTIONS
      cant_create_job  1
      invalid_job_data 2
      jobname_missing  3
      OTHERS           4.

  IF sy-subrc 0.
    SUBMIT zreport1 TO SAP-SPOOL
           SPOOL PARAMETERS print_parameters
           WITHOUT SPOOL DYNPRO
           VIA JOB name NUMBER number AND RETURN.

    SUBMIT zreport2 TO SAP-SPOOL
           SPOOL PARAMETERS print_parameters
           WITHOUT SPOOL DYNPRO
           VIA JOB name NUMBER number AND RETURN.

    SUBMIT zreport3 TO SAP-SPOOL
           SPOOL PARAMETERS print_parameters
           WITHOUT SPOOL DYNPRO
           VIA JOB name NUMBER number AND RETURN.

    SUBMIT zreport4 TO SAP-SPOOL
           SPOOL PARAMETERS print_parameters
           WITHOUT SPOOL DYNPRO
           VIA JOB name NUMBER number AND RETURN.

    SUBMIT zreport5 TO SAP-SPOOL
           SPOOL PARAMETERS print_parameters
           WITHOUT SPOOL DYNPRO
           VIA JOB name NUMBER number AND RETURN.

    SUBMIT zreport6 TO SAP-SPOOL
           SPOOL PARAMETERS print_parameters
           WITHOUT SPOOL DYNPRO
           VIA JOB name NUMBER number AND RETURN.

    SUBMIT zreport7 TO SAP-SPOOL
           SPOOL PARAMETERS print_parameters
           WITHOUT SPOOL DYNPRO
           VIA JOB name NUMBER number AND RETURN.

    SUBMIT zreport8 TO SAP-SPOOL
           SPOOL PARAMETERS print_parameters
           WITHOUT SPOOL DYNPRO
           VIA JOB name NUMBER number AND RETURN.

    SUBMIT zreport9 TO SAP-SPOOL
           SPOOL PARAMETERS print_parameters
           WITHOUT SPOOL DYNPRO
           VIA JOB name NUMBER number AND RETURN.

    SUBMIT zreport10 TO SAP-SPOOL
           SPOOL PARAMETERS print_parameters
           WITHOUT SPOOL DYNPRO
           VIA JOB name NUMBER number AND RETURN.

    IF sy-subrc 0.
      CALL FUNCTION 'JOB_CLOSE'
        EXPORTING
          jobcount             number
          jobname              name
          strtimmed            'X'
        EXCEPTIONS
          cant_start_immediate 1
          invalid_startdate    2
          jobname_missing      3
          job_close_failed     4
          job_nosteps          5
          job_notex            6
          lock_failed          7
          OTHERS               8.

      IF sy-subrc <> 0.
        MESSAGE 'Report not Generated' TYPE 'I'.
      ENDIF.
    ENDIF.
  ENDIF.

Monday, May 30, 2016

User Exit - Explicit Enhancement Point

When an enhancement is used inside a custom program without disturbing the source code, it is called Explicit enhancement. Here the program is not SAP standard one. That is why it is called explicit. If it is standard one then it will be called implicit. We already have covered the implicit enhancement.

Explicit enhancement is saved in a package and transport in a similar way of implicit enhancement. Explicit enhancement has two types.
A.      Enhancement Point – when we use point, we can add additional functionality to the original program. Here the original program executes with enhanced function.
B.      Enhancement Section – when we use section, the original program is not executed. The enhanced program is executed.

In the following example we have demonstrated an explicit enhancement point. In this program originally we have printed the output of a program with no header information. Now we create an explicit enhancement point where the header information has been implemented. Hence the final output shows the complete output of the Airline information.

1.    We must have an activated custom program to implement explicit enhancement.


2.    Now keep the cursor where we need an enhancement. Right click > Enhancements > Create.

3.    Create Enhancement Point with Code option. Enter the name, description & package as follows.

4.    Now after saving the original program will look as follows. Click on the Enhance (Coin) button.

5.    Now keep the cursor on ENHANCEMENT key word. Right click > Enhancement Implementation > Create.

6.    Enter details and OK.

7.    Now write your enhanced code under Enhancement – Endenhancement. Then activate this.

8.    Finally activate the program and execute.

9.    We have the complete header information.

Similar kind of approach can be achieved by using Enhancement Section of explicit enhancement. In the following example we have used enhancement section and proceed the following output.


REPORT  zsr_test1 NO STANDARD PAGE HEADING.

TABLES sflight.
DATA: wa_sflight TYPE sflight,
      it_sflight TYPE TABLE OF sflight.

INITIALIZATION.
  SELECT-OPTIONS s_carrid FOR sflight-carrid.

START-OF-SELECTION.
  SELECT * FROM sflight INTO TABLE it_sflight
    WHERE carrid IN s_carrid.

ENHANCEMENT-SECTION     zsr_enh_s SPOTS zsr_enh2.

END-ENHANCEMENT-SECTION.
*$*$-Start: ZSR_ENH_S---------------------------------------------------------------------------$*$*
ENHANCEMENT 1  ZSR_ENH2.    "active version
  IF it_sflight IS NOT INITIAL.
    WRITE: / 'Airline Code',
          15 'Flight No.',
          27 'Flight Date',
          45 'Maximum Seat',
          60 'Occupied Seat'.
    ULINE.
    SKIP.
  ENDIF.
ENDENHANCEMENT.
*$*$-End:   ZSR_ENH_S---------------------------------------------------------------------------$*$*

  IF it_sflight IS NOT INITIAL.
    SORT it_sflight.
    LOOP AT it_sflight INTO wa_sflight.
      WRITE: /3 wa_sflight-carrid,
             15 wa_sflight-connid,
             27 wa_sflight-fldate DD/MM/YYYY,
             45 wa_sflight-seatsmax,
             60 wa_sflight-seatsocc.
    ENDLOOP.
  ENDIF.