0% found this document useful (0 votes)
36 views9 pages

BDC Including Recording

The document outlines the process for extracting data from an Excel file, performing transactions in SAP using BDC programming, and displaying the results. It includes forms for getting the data, looping through records, performing SAP transactions via BDC, and building an ALV report to display the output. Various variables and tables are defined to store the data and transaction messages.

Uploaded by

Haseeb Hassan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views9 pages

BDC Including Recording

The document outlines the process for extracting data from an Excel file, performing transactions in SAP using BDC programming, and displaying the results. It includes forms for getting the data, looping through records, performing SAP transactions via BDC, and building an ALV report to display the output. Various variables and tables are defined to store the data and transaction messages.

Uploaded by

Haseeb Hassan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

BDC including recording

*&---------------------------------------------------------------------*
*& Report ZHH_BDC_RECORDING_PROGRAM
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZHH_BDC_RECORDING_PROGRAM.

 INCLUDE ZHH_INCLUDE_BDC.
TYPES:  BEGIN OF  ty_data,
          col1 TYPE  string,
          col2 TYPE  string,
          col3 TYPE  string,
          col4 TYPE  string,
        END OF  ty_data.

DATA: it_raw TYPE truxs_t_text_data.
  DATA: it_data TYPE  TABLE OF  ty_data.
  DATA: wa_data TYPE ty_data.

*  *&A*********************************************************************
*&A*                          VARIABLES                               
 *
*&A*********************************************************************
DATA:
      opt    TYPE ctu_params,
  chk(1),
  nlines TYPE i.

DATA: fieldcatalog TYPE slis_t_fieldcat_alv WITH HEADER LINE,
      gd_layout    TYPE slis_layout_alv,
      gd_repid     LIKE sy-repid,
      g_save       TYPE c VALUE 'X',
      g_variant    TYPE disvariant,
      gx_variant   TYPE disvariant,
      g_exit       TYPE c.

DATA: BEGIN OF itab OCCURS 0.
        INCLUDE STRUCTURE bdcmsgcoll.
DATA: END OF itab.

DATA:
  BEGIN OF  it_data1  OCCURS  0,
    col1 TYPE  string,
    col2 TYPE  string,
    col3 TYPE  string,
    col4 TYPE  string,
  END OF  it_data1.
*DATA: it_data TYPE  TABLE OF  ty_data.
*Selection Screen for external file
*&A*********************************************************************
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS:
  p_file LIKE rlgrap-filename OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b1.

*&A*                          AT SELECTION SCREEN OUTPUT               
*
*&A*********************************************************************

*&A*********************************************************************
*&A*                          INITIALIZATION                           
*
*&A*********************************************************************
INITIALIZATION.
  CONCATENATE 'MM01' sy-datum '-' sy-timlo INTO group.

  session = 'X'.

  keep = space.

  smalllog = ' '.

  gx_variant-report = sy-repid.
  CALL FUNCTION 'REUSE_ALV_VARIANT_DEFAULT_GET'
    EXPORTING
      i_save     = g_save
    CHANGING
      cs_variant = gx_variant
    EXCEPTIONS
      not_found  = 2.

AT SELECTION-SCREEN OUTPUT.
  LOOP AT SCREEN.
    IF screen-name EQ 'CTU' OR
       screen-name EQ 'SESSION' OR
       screen-name EQ 'GROUP' OR
       screen-name EQ 'CTUMODE' OR
       screen-name EQ 'USER' OR
       screen-name EQ 'CUPDATE' OR
       screen-name EQ 'KEEP' OR
       screen-name EQ 'E_GROUP' OR
       screen-name EQ 'HOLDDATE' OR
       screen-name EQ 'E_USER' OR
       screen-name EQ 'E_KEEP' OR
       screen-name EQ 'E_HDATE' OR
       screen-name EQ 'NODATA' OR
       screen-name EQ 'SMALLLOG' OR
       screen-name CS '%FS'.
      screen-invisible = 1.
      screen-input = 0.
      MODIFY SCREEN.
    ENDIF.
  ENDLOOP.

*  *&A*********************************************************************
*&A*              AT SELECTION SCREEN ON VALUE REQUEST                 *
*&A*********************************************************************
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      field_name = 'P_FILE'
    IMPORTING
      file_name  = p_file.

*  *  *&A********************************************************************
*
*&A*                        START-OF-SELECTION                         
*
*&A*********************************************************************
START-OF-SELECTION.
*DATA:
**       chk(1).
**  chk = 't'.

  PERFORM get_data .
*  BREAK-POINT.
  END-OF-SELECTION.
  PERFORM open_group.
   PERFORM get_records.
     PERFORM build_fieldcatalog.
  PERFORM display_alv_report.
*&---------------------------------------------------------------------*
*& Form get_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM get_data .
*  END-OF-SELECTION.
CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
    EXPORTING
*     I_FIELD_SEPERATOR    =
      i_line_header        = 'X'
      i_tab_raw_data       = it_raw       " WORK TABLE
      i_filename           = p_file
    TABLES
      i_tab_converted_data = it_data[]    "ACTUAL DATA
    EXCEPTIONS
      conversion_failed    = 1
      OTHERS               = 2.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 s
y-msgv3 sy-msgv4.
  ENDIF.
*  BREAK-POINT.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form get_records
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM get_records .
LOOP AT it_data INTO  wa_data.
  PERFORM step_1.
  PERFORM step_2.

  PERFORM step_3.

   opt-dismode = 'N'.

*    opt-dismode = 'A'.

    opt-updmode = 'A'.

    opt-cattmode = space.

    opt-defsize = ''.

      CLEAR:itab, itab[].

    CALL TRANSACTION 'MM01' USING bdcdata OPTIONS FROM opt  MESSAGES INTO itab
.
*    BREAK-POINT.
       DELETE itab WHERE msgtyp = 'I'.

    DELETE itab WHERE msgtyp = 'W'.

    LOOP AT itab.
      IF itab-msgnr = '800'.
        DESCRIBE TABLE  it_data1 LINES nlines.
        IF nlines >= 1.
          nlines = nlines + 1.
        ELSE.
          nlines = 1.
        ENDIF.

        it_data1-col1 = nlines.
        it_data1-col2 = 'S'.
        it_data1-col3 = wa_data-col1.
        it_data1-col4 = 'Cleared Document Has Been Reset Successfully'.
        APPEND it_data1.
      ELSe.
        DESCRIBE TABLE  it_data1 LINES nlines.
        IF nlines >= 1.
          nlines = nlines + 1.
        ELSE.
          nlines = 1.
        ENDIF.

        it_data1-col1 = nlines.
        it_data1-col2 = 'E'.
        it_data1-col3 = wa_data-col1.
        it_data1-col4 = 'Please Check The Entry From File'.
        APPEND it_data1.
      ENDIF.
    ENDLOOP.

    SUBMIT rsbdcsub WITH mappe = group
                    WITH von   = sy-datum
                    WITH bis   = sy-datum
                    WITH fehler = ' '
                        EXPORTING LIST TO MEMORY
                                        AND RETURN
                                        .

    CLEAR bdcdata.

    REFRESH bdcdata.

    REFRESH bdcdata.

    CLEAR:wa_data.
  ENDLOOP.
  PERFORM close_group.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form step_1
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM step_1 .
perform bdc_dynpro      using 'SAPLMGMM' '0060'.
perform bdc_field       using 'BDC_CURSOR'
                              'RMMG1-MTART'.
perform bdc_field       using 'BDC_OKCODE'
                              '=ENTR'.
perform bdc_field       using 'RMMG1-MBRSH'
                              'C'.
perform bdc_field       using 'RMMG1-MTART'
                              'BUND'.
ENDFORM.
*----------------------------------------------------------------------*
*        Start new screen                                             
 *
*----------------------------------------------------------------------*
FORM bdc_dynpro USING program dynpro.
  CLEAR bdcdata.
  bdcdata-program  = program.
  bdcdata-dynpro   = dynpro.
  bdcdata-dynbegin = 'X'.
  APPEND bdcdata.
ENDFORM.                    "BDC_DYNPRO

*----------------------------------------------------------------------*
*        Insert field                                                 
 *
*----------------------------------------------------------------------*
FORM bdc_field USING fnam fval.
  CLEAR bdcdata.
  bdcdata-fnam = fnam.
  bdcdata-fval = fval.
  APPEND bdcdata.
ENDFORM.                    "BDC_FIELD
*&---------------------------------------------------------------------*
*& Form step_2
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM step_2 .
perform bdc_dynpro      using 'SAPLMGMM' '0070'.
perform bdc_field       using 'BDC_CURSOR'
                              'MSICHTAUSW-DYTXT(01)'.
perform bdc_field       using 'BDC_OKCODE'
                              '=ENTR'.
perform bdc_field       using 'MSICHTAUSW-KZSEL(01)'
                              'X'.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form step_3
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM step_3 .
perform bdc_dynpro      using 'SAPLMGMM' '4004'.
perform bdc_field       using 'BDC_OKCODE'
                              '=BU'.
perform bdc_field       using 'MAKT-MAKTX'
*                              'description of material master'.
                            wa_data-COL1.
perform bdc_field       using 'BDC_CURSOR'
                              'MARA-BISMT'.
perform bdc_field       using 'MARA-MEINS'
*                              '002'.
                              wa_data-COL2.
perform bdc_field       using 'MARA-BISMT'
*                              '1234'.
                              wa_data-COL3.
perform bdc_field       using 'MARA-MTPOS_MARA'
                              'SRVP'.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form build_fieldcatalog
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM build_fieldcatalog .
  CLEAR  fieldcatalog.
  fieldcatalog-fieldname   = 'COL1'.
  fieldcatalog-seltext_s   = 'Serial #'.
  fieldcatalog-seltext_m   = 'Serial #'.
  fieldcatalog-seltext_l   = 'Serial #'.
  fieldcatalog-col_pos     = 1.
  fieldcatalog-outputlen   = 18.
  APPEND fieldcatalog TO fieldcatalog.

  CLEAR  fieldcatalog.
  fieldcatalog-fieldname   = 'COL2'.
  fieldcatalog-seltext_s   = 'Company Code'.
  fieldcatalog-seltext_m   = 'Company Code'.
  fieldcatalog-seltext_l   = 'Company Code'.
  fieldcatalog-col_pos     = 2.
  fieldcatalog-outputlen   = 12.
  APPEND fieldcatalog TO fieldcatalog.

  CLEAR  fieldcatalog.
  fieldcatalog-fieldname   = 'COL3'.
  fieldcatalog-seltext_s   = 'Clearing Document #'.
  fieldcatalog-seltext_m   = 'Clearing Document #'.
  fieldcatalog-seltext_l   = 'Clearing Document #'.
  fieldcatalog-col_pos     = 3.
  fieldcatalog-outputlen   = 19.
  APPEND fieldcatalog TO fieldcatalog.

  CLEAR  fieldcatalog.
  fieldcatalog-fieldname   = 'COL4'.
  fieldcatalog-seltext_s   = 'Fiscal Year'.
  fieldcatalog-seltext_m   = 'Fiscal Year'.
  fieldcatalog-seltext_l   = 'Fiscal Year'.
  fieldcatalog-col_pos     = 4.
  fieldcatalog-outputlen   = 11.
  APPEND fieldcatalog TO fieldcatalog.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form display_alv_report
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM display_alv_report .
  gd_repid = sy-repid.

  DATA:is_layout  TYPE slis_layout_alv.

  CLEAR:is_layout.

  is_layout-colwidth_optimize  = 'X'.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program     = gd_repid
      i_callback_top_of_page = 'TOP-OF-PAGE'
      it_fieldcat            = fieldcatalog[]
      i_save                 = 'X'
      is_layout              = is_layout
      is_variant             = g_variant
    TABLES
      t_outtab               = it_data1
    EXCEPTIONS
      program_error          = 1
      OTHERS                 = 2.
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 S
Y-MSGV3 SY-MSGV4.
  ENDIF.
ENDFORM.

You might also like