Monday, February 17, 2020

XK01 BDC Program in SAP ABAP.


XK01 BDC Program in SAP ABAP.

REPORT  zrk_bdc_xk01_calltra NO STANDARD PAGE HEADING.
include zrk_bdc_xk01_calltra_top.
include zrk_bdc_xk01_calltra_sel.
include zrk_bdc_xk01_calltra_f01.

*----------------------------------------------------------------------*
* Initialization Event
*----------------------------------------------------------------------*
INITIALIZATION.
PERFORM f_defualt_values.

*----------------------------------------------------------------------*
* At Selection Screen Event
*----------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
PERFORM f_get_file.

START-OF-SELECTION.
PERFORM data_upload.
PERFORM get_bdc.


======================================================================================
TOP 
======================================================================================
*&---------------------------------------------------------------------*
*&  Include           ZRK_BDC_XK01_CALLTRA_TOP
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Data Declaration
*&---------------------------------------------------------------------*
DATA: bdcdata LIKE bdcdata    OCCURS 0 WITH HEADER LINE.
*&---------------------------------------------------------------------*
*& Internal Table Declaration
*&---------------------------------------------------------------------*
DATA:BEGIN OF i_tab OCCURS 0,
  bukrs TYPE  rf02k-bukrs,
  ekorg TYPE  rf02k-ekorg,
  ktokk TYPE  rf02k-ktokk,
  anred TYPE  lfa1-anred,
  name1 TYPE  lfa1-name1,
  sortl TYPE  lfa1-sortl,
  land1 TYPE  lfa1-land1,
  akont TYPE  lfb1-akont,
  fdgrv TYPE  lfb1-fdgrv,
  waers TYPE  lfm1-waers,
END OF i_tab.

*Structure for error message
TYPES : BEGIN OF ty_s_error,
  msg_err(60) TYPE C,
END OF ty_s_error.

DATA:
*      wa_path TYPE string ,
*      wa_error TYPE string,
*      wa_cnt   TYPE I,
*      w_mode    TYPE C,
*      wa_cnt1(2) TYPE n,
      it_output TYPE TABLE OF ty_s_error,
      wa_output LIKE LINE OF it_output.

=====================================================================
Selection Screen
=====================================================================
*&---------------------------------------------------------------------*
*&  Include           ZRK_BDC_XK01_CALLTRA_SEL
*&---------------------------------------------------------------------*
*----------------------------------------------------------------------*
* Selection Screen
*----------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text_001.
  SELECTION-SCREEN BEGIN OF LINE.
    SELECTION-SCREEN COMMENT 1(20) text_1_1.
    PARAMETERS: p_file LIKE rlgrap-filename OBLIGATORY.
  SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK b1.
====================================================================================
SUB Page
=====================================================================================
*&---------------------------------------------------------------------*
*&  Include           ZRK_BDC_XK01_CALLTRA_F01
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&      Form  F_DEFUALT_VALUES
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f_defualt_values .
  text_001 = 'File path selection'.
  text_1_1 = 'File Path'.
ENDFORM.                    " F_DEFUALT_VALUES
*&---------------------------------------------------------------------*
*&      Form  F_GET_FILE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f_get_file .
  CALL FUNCTION 'F4_FILENAME'
  IMPORTING
    file_name = p_file.

ENDFORM.                    " F_GET_FILE
*&---------------------------------------------------------------------*
*&      Form  DATA_UPLOAD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM data_upload .
  DATA: loc_filename TYPE string.
  loc_filename = p_file.

  CALL FUNCTION 'GUI_UPLOAD'
  EXPORTING
    filename                = loc_filename
    filetype                = 'DAT'
    has_field_separator     = 'X'
  TABLES
    data_tab                = i_tab
  EXCEPTIONS
    file_open_error         = 1
    file_read_error         = 2
    no_batch                = 3
    gui_refuse_filetransfer = 4
    invalid_type            = 5
    no_authority            = 6
    unknown_error           = 7
    bad_data_format         = 8
    header_not_allowed      = 9
    separator_not_allowed   = 10
    header_too_long         = 11
    unknown_dp_error        = 12
    access_denied           = 13
    dp_out_of_memory        = 14
    disk_full               = 15
    dp_timeout              = 16
    OTHERS                  = 17.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

ENDFORM.                    " DATA_UPLOAD
*&---------------------------------------------------------------------*
*&      Form  GET_BDC
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_bdc .
  IF i_tab[] IS NOT INITIAL.
    PERFORM open_group.
    LOOP AT i_tab.
      REFRESH bdcdata.
      PERFORM bdcdata.
    ENDLOOP.
    PERFORM close_group.
  ENDIF.
ENDFORM.                    " GET_BDC
*&---------------------------------------------------------------------*
*&      Form  OPEN_GROUP
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM open_group .

ENDFORM.                    " OPEN_GROUP
*&---------------------------------------------------------------------*
*&      Form  BDCDATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM bdcdata .
*---->>> Insert Recording Here
  PERFORM bdc_dynpro      USING 'SAPMF02K' '0100'.
  PERFORM bdc_field       USING 'BDC_CURSOR'      'RF02K-KTOKK'.
  PERFORM bdc_field       USING 'BDC_OKCODE'      '/00'.
  PERFORM bdc_field       USING 'RF02K-BUKRS' i_tab-bukrs.      "     '1000'.
  PERFORM bdc_field       USING 'RF02K-EKORG' i_tab-ekorg.     "    '1000'.
  PERFORM bdc_field       USING 'RF02K-KTOKK' i_tab-ktokk.     "   '0001'.
  PERFORM bdc_dynpro      USING 'SAPMF02K'        '0110'.
  PERFORM bdc_field       USING 'BDC_CURSOR'      'LFA1-LAND1'.
  PERFORM bdc_field       USING 'BDC_OKCODE'      '/00'.
  PERFORM bdc_field       USING 'LFA1-ANRED' i_tab-anred.       "      'Mr'.
  PERFORM bdc_field       USING 'LFA1-NAME1' i_tab-name1.       "      'nageshpur'.
  PERFORM bdc_field       USING 'LFA1-SORTL' i_tab-sortl.       "      'NAGESH'.
  PERFORM bdc_field       USING 'LFA1-LAND1' i_tab-land1.       "     'IN'.
  PERFORM bdc_dynpro      USING 'SAPMF02K'        '0120'.
  PERFORM bdc_field       USING 'BDC_CURSOR'      'LFA1-KUNNR'.
  PERFORM bdc_field       USING 'BDC_OKCODE'      '/00'.
  PERFORM bdc_dynpro      USING 'SAPMF02K'       '0130'.
  PERFORM bdc_field       USING 'BDC_CURSOR'     'LFBK-BANKS(01)'.
  PERFORM bdc_field       USING 'BDC_OKCODE'     '=ENTR'.
  PERFORM bdc_dynpro      USING 'SAPMF02K'      '0380'.
  PERFORM bdc_field       USING 'BDC_CURSOR'    'KNVK-NAMEV(01)'.
  PERFORM bdc_field       USING 'BDC_OKCODE'    '=ENTR'.
  PERFORM bdc_dynpro      USING 'SAPMF02K'     '0210'.
  PERFORM bdc_field       USING 'BDC_CURSOR'   'LFB1-FDGRV'.
  PERFORM bdc_field       USING 'BDC_OKCODE'   '/00'.
  PERFORM bdc_field       USING 'LFB1-AKONT' i_tab-akont.          "   '31000'.
  PERFORM bdc_field       USING 'LFB1-FDGRV' i_tab-fdgrv.          "  'A1'.
  PERFORM bdc_dynpro      USING 'SAPMF02K'    '0215'.
  PERFORM bdc_field       USING 'BDC_CURSOR'  'LFB1-ZTERM'.
  PERFORM bdc_field       USING 'BDC_OKCODE'  '/00'.
  PERFORM bdc_dynpro      USING 'SAPMF02K'    '0220'.
  PERFORM bdc_field       USING 'BDC_CURSOR'  'LFB5-MAHNA'.
  PERFORM bdc_field       USING 'BDC_OKCODE'  '/00'.
  PERFORM bdc_dynpro      USING 'SAPMF02K'    '0310'.
  PERFORM bdc_field       USING 'BDC_CURSOR'  'LFM1-WAERS'.
  PERFORM bdc_field       USING 'BDC_OKCODE'  '=UPDA'.
  PERFORM bdc_field       USING 'LFM1-WAERS'  i_tab-waers.        " 'INR'.

  PERFORM bdc_insert.

ENDFORM.                    " BDCDATA
*&---------------------------------------------------------------------*
*&      Form  CLOSE_GROUP
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM close_group .

ENDFORM.                    " CLOSE_GROUP
*&---------------------------------------------------------------------*
*&      Form  BDC_INSERT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM bdc_insert .

*Data decleration for Error Message
  DATA:
        t_msg TYPE TABLE OF bdcmsgcoll,   " Collecting Error messages
        w_msg TYPE bdcmsgcoll,
        w_msg1(51).

* Call transaction 'ME51'
  CALL TRANSACTION 'XK01' USING bdcdata
        MODE   'N'
        UPDATE 'S'
        MESSAGES INTO t_msg.

  IF sy-subrc EQ 0.
    LOOP AT t_msg INTO w_msg.
*    Uploaded into the database
      WRITE :/ 'Customer created successfully :', w_msg-msgv1.
    ENDLOOP.
  ELSE.
*    Error Found
    LOOP AT t_msg INTO w_msg WHERE msgtyp EQ 'E'.
*     Format Message
      CALL FUNCTION 'MESSAGE_TEXT_BUILD'
      EXPORTING
        msgid               = w_msg-msgid
        msgnr               = w_msg-msgnr
        msgv1               = w_msg-msgv1
        msgv2               = w_msg-msgv2
        msgv3               = w_msg-msgv3
        msgv4               = w_msg-msgv4
      IMPORTING
        message_text_output = w_msg1.


      wa_output-msg_err = w_msg1.

*Error message in downloaded file
      DATA:
            wa_string(10) TYPE C.
      CONCATENATE wa_string wa_output-msg_err INTO wa_output-msg_err SEPARATED BY space.
      APPEND wa_output-msg_err TO it_output.
      WRITE :/ 'Error in records', wa_output-msg_err.

    ENDLOOP.

  ENDIF.
ENDFORM.                    " BDC_INSERT
*&---------------------------------------------------------------------*
*&      Form  BDC_DYNPRO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_0145   text
*      -->P_0146   text
*----------------------------------------------------------------------*
FORM bdc_dynpro USING PROGRAM DYNPRO.
  CLEAR bdcdata.
  bdcdata-PROGRAM  = PROGRAM.
  bdcdata-DYNPRO   = DYNPRO.
  bdcdata-dynbegin = 'X'.
  APPEND bdcdata.
ENDFORM.                    " BDC_DYNPRO
*&---------------------------------------------------------------------*
*&      Form  BDC_FIELD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_0150   text
*      -->P_0151   text
*----------------------------------------------------------------------*
FORM bdc_field  USING  fnam fval.
  CLEAR bdcdata.
  bdcdata-fnam = fnam.
  bdcdata-fval = fval.
  APPEND bdcdata.
ENDFORM.                    " BDC_FIELD

No comments:

Post a Comment