SAP BDC – Upload Program for TCode : ME52N


SAP BDC Upload Program for Tcode: ME52N – Purchase Requisition Service line item update through Notepad File.

SAP ABAP Course for 2020

SAP ABAP Fundamentals

SAP ABAP Interview Questions – Real Time

Step 1: Go to SE38 Tcode, type program name & click on Create Button.

Step 2: Type below mentioned code.

*———————————————————–*
*
*———————————————————-*
REPORT  yrmc2027.
TABLES: caufvd, afvgd, eban.
TYPE-POOLS : imis.

DATA:  BEGIN OF i_bdcdata OCCURS 0.
        INCLUDE STRUCTURE bdcdata.
DATA:  END OF i_bdcdata.
DATA: BEGIN OF messtab OCCURS 0.
        INCLUDE STRUCTURE bdcmsgcoll.
DATA: END OF messtab.
DATA:  BEGIN OF i_msg OCCURS 10.
        INCLUDE STRUCTURE bdcmsgcoll.
DATA : END OF i_msg .

DATA : v_ktext1(15), v_menge(14), v_meins(14), v_tbtwr(14),

v_e xtsrvno(17), v_lbnum(14), v_ausgb(14), v_int(17), v_serv(18).

DATA:    BEGIN OF i_data OCCURS 0,
           ktext1   TYPE char40, “esll-ktext1,
           menge    TYPE char14, “esll-menge,
           meins    TYPE char3, “esll-meins,
           tbtwr    TYPE char13, “esll-tbtwr,
           extsrvno TYPE char18, “esll-extsrvno,
           srvpos   TYPE char18, “esll-srvpos,
         END OF i_data.

DATA : w_no(3),
       gs_data  LIKE LINE OF i_data,
       count    TYPE num2,
       act      TYPE num2,
       row_no   TYPE num6,
       lv_banfn TYPE banfn.

DATA : val_menge TYPE char30,
       val_tbtwr TYPE char30.

DATA : BEGIN OF i_error OCCURS 0,
         msg TYPE imis_type_s_message,
       END OF i_error.

DATA : BEGIN OF i_ack_file OCCURS 0 ,
         werk           TYPE qals-werk,
         matnr          TYPE mara-matnr,
         charg          TYPE qals-charg,
         ktextlos       TYPE qals-ktextlos,
         prueflos       LIKE qals-prueflos,
         l_status(1)    TYPE c,
         re1_status(45) TYPE c,
         re2_status(45) TYPE c,
         msgtext        LIKE t100-text,
       END OF i_ack_file .

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
SELECTION-SCREEN SKIP 1.
SELECT-OPTIONS: s_banfn FOR eban-banfn OBLIGATORY NO INTERVALS NO-EXTENSION,
                s_bnfpo FOR eban-bnfpo OBLIGATORY NO INTERVALS NO-EXTENSION.

SELECTION-SCREEN SKIP 1.
PARAMETERS : p_file   LIKE rlgrap-filename DEFAULT
                ‘C:\notes.txt’,
             p_mode   LIKE ctu_params-dismode AS LISTBOX VISIBLE LENGTH
                        25 DEFAULT ‘N’,
             p_update LIKE ctu_params-updmode AS LISTBOX VISIBLE LENGTH
                        20 DEFAULT ‘S’ .

SELECTION-SCREEN SKIP 1.
SELECTION-SCREEN END OF BLOCK b1.

SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-002.

SELECTION-SCREEN COMMENT 1(79) text-003.
SELECTION-SCREEN SKIP.
SELECTION-SCREEN COMMENT /1(79) text-004.
SELECTION-SCREEN SKIP.
SELECTION-SCREEN COMMENT /1(79) text-005.
SELECTION-SCREEN SKIP.
SELECTION-SCREEN COMMENT /1(79) text-006.
SELECTION-SCREEN SKIP.

SELECTION-SCREEN END OF BLOCK b2.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  PERFORM get_f4_file USING p_file.

START-OF-SELECTION.
  REFRESH i_data.

  SELECT SINGLE banfn FROM eban INTO lv_banfn WHERE banfn = s_banfn-low AND bnfpo = s_bnfpo-low.
  IF sy-subrc = 0.

    PERFORM upload_file.

    PERFORM validation.
    PERFORM bdc_upload.

    PERFORM write_error.
  ELSE.
    MESSAGE ‘PR number does not exist’ TYPE ‘W’ DISPLAY LIKE ‘E’.
  ENDIF.

FORM format_msg .
  CALL FUNCTION ‘FORMAT_MESSAGE’
    EXPORTING
      id        = i_msg-msgid
      lang      = sy-langu
      no        = i_msg-msgnr
      v1        = i_msg-msgv1
      v2        = i_msg-msgv2
      v3        = i_msg-msgv3
      v4        = i_msg-msgv4
    IMPORTING
      msg       = i_ack_file-msgtext
    EXCEPTIONS
      not_found = 1
      OTHERS    = 2.
ENDFORM.

FORM bdc_dynpro USING program dynpro.
  CLEAR i_bdcdata.
  i_bdcdata-program  = program.
  i_bdcdata-dynpro   = dynpro.
  i_bdcdata-dynbegin = ‘X’.
  APPEND i_bdcdata.
ENDFORM.

FORM bdc_field USING fnam fval.
  CLEAR i_bdcdata.
  i_bdcdata-fnam = fnam.
  i_bdcdata-fval = fval.
  APPEND i_bdcdata.
ENDFORM.

FORM upload_file .
  DATA: p_file1 TYPE string.

  p_file1 = p_file.

  CALL FUNCTION ‘GUI_UPLOAD’
    EXPORTING
      filename = p_file1
      filetype = ‘DAT’
    TABLES
      data_tab = i_data
*     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.

FORM get_f4_file  USING    p_file.

  CALL FUNCTION ‘F4_FILENAME’
    EXPORTING
      program_name  = syst-cprog
      dynpro_number = syst-dynnr
*     FIELD_NAME    = ‘ ‘
    IMPORTING
      file_name     = p_file.

ENDFORM.                ” UPLOAD_FILE

FORM write_error .
  IF NOT i_error[] IS INITIAL.
    LOOP AT i_error.
      AT FIRST.
        WRITE:/ ‘Data NOT uploaded’.
      ENDAT.
      SKIP 1.
      WRITE:/ i_error-msg.
    ENDLOOP.
  ELSE.
    WRITE:/ ‘Data uploaded Successfully’.
  ENDIF.
ENDFORM.

FORM populate_data .

ENDFORM.
*&———————————————————————*
*&      Form  VALIDATION
*&———————————————————————*
*       text
*———————————————————————-*
*  –>  p1        text
*  <–  p2        text
*———————————————————————-*
FORM validation .

  LOOP AT i_data INTO gs_data.
    IF gs_data-ktext1 IS INITIAL.
      MESSAGE ‘Short Description field is blank’ TYPE ‘W’ DISPLAY LIKE ‘E’.
      EXIT.
    ELSEIF gs_data-menge IS INITIAL.
      MESSAGE ‘Quantity field is blank’ TYPE ‘W’ DISPLAY LIKE ‘E’.
      EXIT.
    ELSEIF gs_data-meins IS INITIAL.
      MESSAGE ‘Quantity field is blank’ TYPE ‘W’ DISPLAY LIKE ‘E’.
      EXIT.
    ELSEIF gs_data-tbtwr IS INITIAL.
      MESSAGE ‘Quantity field is blank’ TYPE ‘W’ DISPLAY LIKE ‘E’.
      EXIT.
    ENDIF.

  ENDLOOP.

ENDFORM.
*&———————————————————————*
*&      Form  VALIDATION1
*&———————————————————————*
*       text
*———————————————————————-*
*  –>  p1        text
*  <–  p2        text
*———————————————————————-*
FORM bdc_upload.
  DATA: lv_ktext1     TYPE char15,
        lv_menge      TYPE char14,
        lv_meins      TYPE char14,
        lv_tbtwr      TYPE char14,
        lv_count      TYPE num2,
        lv_packno     TYPE esll-packno,
        lv_sub_packno TYPE esll-sub_packno,
        lt_esll       TYPE TABLE OF esll,
        ls_esll       TYPE esll,
        lv_flag       TYPE char1.

  BREAK-POINT.

  CLEAR: lv_count, lv_sub_packno, lv_packno.
  REFRESH: lt_esll.
  SELECT SINGLE packno FROM eban INTO lv_packno WHERE banfn = s_banfn-low AND bnfpo = s_bnfpo-low.
  IF sy-subrc = 0.
    SELECT SINGLE sub_packno FROM esll INTO lv_sub_packno WHERE packno = lv_packno.
    IF sy-subrc = 0.
      SELECT * FROM esll INTO TABLE lt_esll WHERE packno = lv_sub_packno.
      DESCRIBE TABLE lt_esll LINES lv_count.
    ENDIF.
  ENDIF.

  LOOP AT i_data INTO gs_data.
    lv_count = lv_count + 1.
    IF lv_count = 11.
      CLEAR lv_count.
      lv_count = 1.
      lv_flag = ‘X’.
    ENDIF.

    PERFORM bdc_dynpro      USING ‘SAPLMEGUI’ ‘0014’.
    PERFORM bdc_field          USING ‘BDC_OKCODE’
                                                 ‘=MECHOB’.
    PERFORM bdc_dynpro      USING ‘SAPLMEGUI’ ‘0002’.
    PERFORM bdc_field          USING ‘BDC_OKCODE’
                                                 ‘=MEOK’.
    PERFORM bdc_field         USING ‘BDC_CURSOR’
                                                ‘MEPO_SELECT-BANFN’.
    PERFORM bdc_field         USING ‘MEPO_SELECT-BANFN’
                                                 s_banfn-low.“‘0020035945’.

**    IF lv_flag IS INITIAL.
      PERFORM bdc_field       USING ‘MEPO_SELECT-BSTYP_B’
                                                ‘X’.
      PERFORM bdc_dynpro   USING ‘SAPLMEGUI’ ‘0014’.
      PERFORM bdc_field       USING ‘BDC_OKCODE’
                                                ‘=MEV4002BUTTON’.
      PERFORM bdc_dynpro    USING ‘SAPLMEGUI’ ‘0014’.
      PERFORM bdc_field        USING ‘BDC_OKCODE’
                                                ‘/00’.
      PERFORM bdc_field       USING ‘DYN_6000-LIST’
                                                ‘   1’.

    CONCATENATE ‘ESLL-KTEXT1(‘ lv_count ‘)’ INTO lv_ktext1.
    CONCATENATE ‘ESLL-MENGE(‘ lv_count ‘)’ INTO lv_menge.
    CONCATENATE ‘ESLL-MEINS(‘ lv_count ‘)’ INTO lv_meins.
    CONCATENATE ‘ESLL-TBTWR(‘ lv_count ‘)’ INTO lv_tbtwr.

    PERFORM bdc_field       USING ‘BDC_CURSOR’
                                              ‘ESLL-TBTWR(09)’.
    PERFORM bdc_field       USING ‘RM11P-NEW_ROW’
                                              ’10’.
    PERFORM bdc_field       USING lv_ktext1“‘ESLL-KTEXT1(09)’
                                              gs_data-ktext1.“‘c02’.
    PERFORM bdc_field       USING lv_menge“‘ESLL-MENGE(09)’
                                              gs_data-menge.“‘1’.
    PERFORM bdc_field       USING lv_meins“‘ESLL-MEINS(09)’
                                              gs_data-meins.“‘EA’.
    PERFORM bdc_field       USING lv_tbtwr“‘ESLL-TBTWR(09)’
                                              gs_data-tbtwr.“‘1’.

    PERFORM bdc_dynpro   USING ‘SAPLMEGUI’ ‘0014’.
    PERFORM bdc_field       USING ‘BDC_OKCODE’
                                              ‘/00’.
    PERFORM bdc_field       USING ‘DYN_6000-LIST’
                                              ‘   1’.
    PERFORM bdc_field       USING ‘BDC_CURSOR’
                                              ‘ESLL-TBTWR(06)’.“lv_TBTWR”
    PERFORM bdc_field       USING ‘RM11P-NEW_ROW’
                                              ‘280’.“’30’.
    PERFORM bdc_dynpro   USING ‘SAPLMEGUI’ ‘0014’.
    PERFORM bdc_field       USING ‘BDC_OKCODE’
                                              ‘=MESAVE’.
    PERFORM bdc_field       USING ‘DYN_6000-LIST’
                                              ‘   1’.
    PERFORM bdc_field       USING ‘BDC_CURSOR’
                                             ‘ESLL-TBTWR(06)’. “lv_TBTWR”
    PERFORM bdc_field       USING ‘RM11P-NEW_ROW’
                                             ’80’.
  ENDLOOP.

  CALL TRANSACTION ‘ME52N’ USING i_bdcdata
                           MODE   p_mode
                           UPDATE p_update
                           MESSAGES INTO messtab.
  REFRESH i_bdcdata.

  READ TABLE messtab WITH KEY msgtyp = ‘E’.
*    read table MESSTAB .

  IF sy-subrc NE 0.

  ELSEIF sy-subrc = 0.
    w_no = messtab-msgnr.
    LOOP AT messtab.
      CALL FUNCTION ‘FORMAT_MESSAGE’
        EXPORTING
          id        = messtab-msgid
          lang      = sy-langu
          no        = w_no
          v1        = messtab-msgv1
          v2        = messtab-msgv2
          v3        = messtab-msgv3
          v4        = messtab-msgv4
        IMPORTING
          msg       = i_error-msg
        EXCEPTIONS
          not_found = 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.

      APPEND i_error.
      CLEAR  i_error.
    ENDLOOP.
  ENDIF.

ENDFORM.

Step 3: Press F8 button & Select .txt file.

Step 4: Enter PR(Purchase Requisition) No. , line item number, BDC Mode & press F8 button to upload a file.

After executing this program will automatically upload the below file’s data into SAP Tcode ME52N.

File Data:


Output:

Data is updated successfully in the SAP system.

Step 1: To check Updated data in Tcode ME52N. Go to Tcode ME52N & Click on other PR No.

Step 2: Now, Enter PR No. 0020035945 as per screenshot.

Step 3: Goto Header & Service Tab & check uploaded data.